Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class MainActivity extends AppCompatActivity {
- public static final String TAG = "TAG";
- private ListView listView;
- private View parentView;
- private ArrayList<Contact> contactList;
- private ContactAdapter adapter;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- contactList = new ArrayList<>();
- listView = (ListView) findViewById(R.id.listView);
- parentView = findViewById(R.id.parentLayout);
- FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
- assert fab != null;
- fab.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(@NonNull final View view) {
- /**
- * Checking Internet Connection
- */
- if (InternetConnection.checkConnection(getApplicationContext())) {
- final ProgressDialog dialog;
- /**
- * Progress Dialog for User Interaction
- */
- dialog = new ProgressDialog(MainActivity.this);
- dialog.setTitle(getString(R.string.string_getting_json_title));
- dialog.setMessage(getString(R.string.string_getting_json_message));
- dialog.show();
- //Creating an object of our api interface
- ApiService api = RetroClient.getApiService();
- /**
- * Calling JSON
- */
- Call<List<Contact>> call = api.getMyJSON();
- /**
- * Enqueue Callback will be call when get response...
- */
- call.enqueue(new Callback<List<Contact>>() {
- @Override
- public void onResponse(Call<List<Contact>> call, Response<List<Contact>> response) {
- Log.d(TAG, "Я в onResponse ");
- //Dismiss Dialog
- dialog.dismiss();
- if (response.isSuccessful()) {
- /**
- * Got Successfully
- */
- contactList.addAll(response.body());
- Log.d(TAG, "Я в блоке if (response.isSuccessful())");
- /**
- * Binding that List to Adapter
- */
- adapter = new ContactAdapter(MainActivity.this, contactList);
- listView.setAdapter(adapter);
- } else {
- Snackbar.make(parentView, R.string.string_some_thing_wrong, Snackbar.LENGTH_LONG).show();
- }
- }
- @Override
- public void onFailure(Call<List<Contact>> call, Throwable t) {
- dialog.dismiss();
- }
- });
- } else {
- Snackbar.make(parentView, R.string.string_internet_connection_not_available, Snackbar.LENGTH_LONG).show();
- }
- }
- });
- }
- }
- public class ContactAdapter extends ArrayAdapter<Contact> {
- List<Contact> contactList;
- Context context;
- private LayoutInflater mInflater;
- // Constructors
- public ContactAdapter(Context context, List<Contact> objects) {
- super(context, 0, objects);
- this.context = context;
- this.mInflater = LayoutInflater.from(context);
- contactList = objects;
- }
- @Override
- public Contact getItem(int position) {
- return contactList.get(position);
- }
- @Override
- public View getView(int position, View convertView, ViewGroup parent) {
- final ViewHolder vh;
- if (convertView == null) {
- View view = mInflater.inflate(R.layout.layout_row_view, parent, false);
- vh = ViewHolder.create((RelativeLayout) view);
- view.setTag(vh);
- } else {
- vh = (ViewHolder) convertView.getTag();
- }
- Contact item = getItem(position);
- vh.textViewTitleRu.setText(item.getTitleRu());
- vh.textViewTitleEng.setText(item.getTitleEng());
- return vh.rootView;
- }
- private static class ViewHolder {
- public final RelativeLayout rootView;
- public final TextView textViewTitleRu;
- public final TextView textViewTitleEng;
- private ViewHolder(RelativeLayout rootView, TextView textViewTitleRu, TextView textViewTitleEng) {
- this.rootView = rootView;
- this.textViewTitleRu = textViewTitleRu;
- this.textViewTitleEng = textViewTitleEng;
- }
- public static ViewHolder create(RelativeLayout rootView) {
- TextView textViewTitleRu = (TextView) rootView.findViewById(R.id.textViewTitleRu);
- TextView textViewTitleEng = (TextView) rootView.findViewById(R.id.textViewTitleEng);
- return new ViewHolder(rootView, textViewTitleRu, textViewTitleEng);
- }
- }
- }
- public interface ApiService {
- @GET("menuserver5/categories")
- Call<List<Contact>> getMyJSON();
- }
- public class RetroClient {
- private static final String ROOT_URL = "http://139.59.164.239:8080/";
- private static Retrofit getRetrofitInstance() {
- return new Retrofit.Builder()
- .baseUrl(ROOT_URL)
- .addConverterFactory(GsonConverterFactory.create())
- .build();
- }
- public static ApiService getApiService() {
- return getRetrofitInstance().create(ApiService.class);
- }
- }
- public class Contact {
- @SerializedName("id")
- @Expose
- private String id;
- @SerializedName("title_ru")
- @Expose
- private String titleRu;
- @SerializedName("title_eng")
- @Expose
- private String titleEng;
- @SerializedName("url")
- @Expose
- private String url;
- @SerializedName("img_url")
- @Expose
- private String imageUrl;
- @SerializedName("weight")
- @Expose
- private int weight;
- @SerializedName("menu_id")
- @Expose
- private int menuId;
- @SerializedName("status")
- @Expose
- private int status;
- /**
- * @return The id
- */
- public String getId() {
- return id;
- }
- /**
- * @return The name
- */
- public String getTitleRu() {
- return titleRu;
- }
- /**
- * @return The email
- */
- public String getTitleEng() {
- return titleEng;
- }
- public String getUrl() {
- return url;
- }
- public String getImageUrl() {
- return imageUrl;
- }
- public int getWeight() {
- return weight;
- }
- public int getMenuId() {
- return menuId;
- }
- public int getStatus() {
- return status;
- }
- }
- public Contact() {}
- /**
- *
- * @param id
- * @param titleRu
- * @param weight
- * @param status
- * @param imageUrl
- * @param titleEng
- * @param menuId
- * @param url
- */
- public Contact(Integer id, String titleRu, String titleEng, String url, String imageUrl, Integer weight, Integer menuId, Integer status) {
- super();
- this.id = id;
- this.titleRu = titleRu;
- this.titleEng = titleEng;
- this.url = url;
- this.imageUrl = imageUrl;
- this.weight = weight;
- this.menuId = menuId;
- this.status = status;
- }
Add Comment
Please, Sign In to add comment