Guest User

Untitled

a guest
Oct 21st, 2017
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.48 KB | None | 0 0
  1. public class MainActivity extends AppCompatActivity {
  2.  
  3. public static final String TAG = "TAG";
  4.  
  5. private ListView listView;
  6. private View parentView;
  7.  
  8. private ArrayList<Contact> contactList;
  9. private ContactAdapter adapter;
  10.  
  11. @Override
  12. protected void onCreate(Bundle savedInstanceState) {
  13. super.onCreate(savedInstanceState);
  14. setContentView(R.layout.activity_main);
  15.  
  16. contactList = new ArrayList<>();
  17.  
  18. listView = (ListView) findViewById(R.id.listView);
  19. parentView = findViewById(R.id.parentLayout);
  20.  
  21. FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
  22. assert fab != null;
  23. fab.setOnClickListener(new View.OnClickListener() {
  24. @Override
  25. public void onClick(@NonNull final View view) {
  26.  
  27. /**
  28. * Checking Internet Connection
  29. */
  30. if (InternetConnection.checkConnection(getApplicationContext())) {
  31. final ProgressDialog dialog;
  32. /**
  33. * Progress Dialog for User Interaction
  34. */
  35. dialog = new ProgressDialog(MainActivity.this);
  36. dialog.setTitle(getString(R.string.string_getting_json_title));
  37. dialog.setMessage(getString(R.string.string_getting_json_message));
  38. dialog.show();
  39.  
  40. //Creating an object of our api interface
  41. ApiService api = RetroClient.getApiService();
  42.  
  43. /**
  44. * Calling JSON
  45. */
  46. Call<List<Contact>> call = api.getMyJSON();
  47.  
  48. /**
  49. * Enqueue Callback will be call when get response...
  50. */
  51.  
  52. call.enqueue(new Callback<List<Contact>>() {
  53.  
  54. @Override
  55. public void onResponse(Call<List<Contact>> call, Response<List<Contact>> response) {
  56.  
  57. Log.d(TAG, "Я в onResponse ");
  58.  
  59. //Dismiss Dialog
  60. dialog.dismiss();
  61.  
  62. if (response.isSuccessful()) {
  63. /**
  64. * Got Successfully
  65. */
  66. contactList.addAll(response.body());
  67. Log.d(TAG, "Я в блоке if (response.isSuccessful())");
  68.  
  69. /**
  70. * Binding that List to Adapter
  71. */
  72. adapter = new ContactAdapter(MainActivity.this, contactList);
  73. listView.setAdapter(adapter);
  74.  
  75. } else {
  76. Snackbar.make(parentView, R.string.string_some_thing_wrong, Snackbar.LENGTH_LONG).show();
  77. }
  78. }
  79.  
  80. @Override
  81. public void onFailure(Call<List<Contact>> call, Throwable t) {
  82. dialog.dismiss();
  83. }
  84. });
  85.  
  86. } else {
  87. Snackbar.make(parentView, R.string.string_internet_connection_not_available, Snackbar.LENGTH_LONG).show();
  88. }
  89. }
  90. });
  91. }
  92. }
  93.  
  94. public class ContactAdapter extends ArrayAdapter<Contact> {
  95.  
  96. List<Contact> contactList;
  97. Context context;
  98. private LayoutInflater mInflater;
  99.  
  100. // Constructors
  101. public ContactAdapter(Context context, List<Contact> objects) {
  102. super(context, 0, objects);
  103. this.context = context;
  104. this.mInflater = LayoutInflater.from(context);
  105. contactList = objects;
  106. }
  107.  
  108. @Override
  109. public Contact getItem(int position) {
  110. return contactList.get(position);
  111. }
  112.  
  113. @Override
  114. public View getView(int position, View convertView, ViewGroup parent) {
  115. final ViewHolder vh;
  116. if (convertView == null) {
  117. View view = mInflater.inflate(R.layout.layout_row_view, parent, false);
  118. vh = ViewHolder.create((RelativeLayout) view);
  119. view.setTag(vh);
  120. } else {
  121. vh = (ViewHolder) convertView.getTag();
  122. }
  123.  
  124. Contact item = getItem(position);
  125.  
  126. vh.textViewTitleRu.setText(item.getTitleRu());
  127. vh.textViewTitleEng.setText(item.getTitleEng());
  128.  
  129. return vh.rootView;
  130. }
  131.  
  132. private static class ViewHolder {
  133. public final RelativeLayout rootView;
  134. public final TextView textViewTitleRu;
  135. public final TextView textViewTitleEng;
  136.  
  137. private ViewHolder(RelativeLayout rootView, TextView textViewTitleRu, TextView textViewTitleEng) {
  138. this.rootView = rootView;
  139. this.textViewTitleRu = textViewTitleRu;
  140. this.textViewTitleEng = textViewTitleEng;
  141. }
  142.  
  143. public static ViewHolder create(RelativeLayout rootView) {
  144. TextView textViewTitleRu = (TextView) rootView.findViewById(R.id.textViewTitleRu);
  145. TextView textViewTitleEng = (TextView) rootView.findViewById(R.id.textViewTitleEng);
  146. return new ViewHolder(rootView, textViewTitleRu, textViewTitleEng);
  147. }
  148.  
  149. }
  150.  
  151. }
  152.  
  153. public interface ApiService {
  154. @GET("menuserver5/categories")
  155. Call<List<Contact>> getMyJSON();
  156. }
  157.  
  158. public class RetroClient {
  159.  
  160. private static final String ROOT_URL = "http://139.59.164.239:8080/";
  161.  
  162. private static Retrofit getRetrofitInstance() {
  163. return new Retrofit.Builder()
  164. .baseUrl(ROOT_URL)
  165. .addConverterFactory(GsonConverterFactory.create())
  166. .build();
  167. }
  168.  
  169. public static ApiService getApiService() {
  170. return getRetrofitInstance().create(ApiService.class);
  171. }
  172. }
  173.  
  174. public class Contact {
  175. @SerializedName("id")
  176. @Expose
  177. private String id;
  178.  
  179. @SerializedName("title_ru")
  180. @Expose
  181. private String titleRu;
  182.  
  183. @SerializedName("title_eng")
  184. @Expose
  185. private String titleEng;
  186.  
  187. @SerializedName("url")
  188. @Expose
  189. private String url;
  190.  
  191. @SerializedName("img_url")
  192. @Expose
  193. private String imageUrl;
  194.  
  195. @SerializedName("weight")
  196. @Expose
  197. private int weight;
  198.  
  199. @SerializedName("menu_id")
  200. @Expose
  201. private int menuId;
  202.  
  203. @SerializedName("status")
  204. @Expose
  205. private int status;
  206.  
  207. /**
  208. * @return The id
  209. */
  210. public String getId() {
  211. return id;
  212. }
  213.  
  214. /**
  215. * @return The name
  216. */
  217. public String getTitleRu() {
  218. return titleRu;
  219. }
  220.  
  221. /**
  222. * @return The email
  223. */
  224. public String getTitleEng() {
  225. return titleEng;
  226. }
  227.  
  228. public String getUrl() {
  229. return url;
  230. }
  231.  
  232. public String getImageUrl() {
  233. return imageUrl;
  234. }
  235.  
  236. public int getWeight() {
  237. return weight;
  238. }
  239.  
  240. public int getMenuId() {
  241. return menuId;
  242. }
  243.  
  244. public int getStatus() {
  245. return status;
  246. }
  247. }
  248.  
  249. public Contact() {}
  250.  
  251. /**
  252. *
  253. * @param id
  254. * @param titleRu
  255. * @param weight
  256. * @param status
  257. * @param imageUrl
  258. * @param titleEng
  259. * @param menuId
  260. * @param url
  261. */
  262. public Contact(Integer id, String titleRu, String titleEng, String url, String imageUrl, Integer weight, Integer menuId, Integer status) {
  263. super();
  264. this.id = id;
  265. this.titleRu = titleRu;
  266. this.titleEng = titleEng;
  267. this.url = url;
  268. this.imageUrl = imageUrl;
  269. this.weight = weight;
  270. this.menuId = menuId;
  271. this.status = status;
  272. }
Add Comment
Please, Sign In to add comment