Guest User

Untitled

a guest
Dec 10th, 2018
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.94 KB | None | 0 0
  1. <...>
  2. public List<ChatsEntity> listContents = new ArrayList<>();
  3. private ViewHolder mViewHolder = new ViewHolder();
  4. private ChatsEntity mChatsEntity;
  5.  
  6. @Override
  7. public void onCreate(Bundle savedInstanceState) {
  8. super.onCreate(savedInstanceState);
  9. listContents = new ArrayList<>();
  10. }
  11.  
  12. @Override
  13. public View onCreateView(LayoutInflater inflater, ViewGroup container,
  14. Bundle savedInstanceState) {
  15. final View view = inflater.inflate(R.layout.fragment_list, container, false);
  16.  
  17. this.mContext = view.getContext();
  18.  
  19. this.mViewHolder.mRecyclerViewContacts = view.findViewById(R.id.recycler_contacts);
  20. this.mViewHolder.mTextHead = view.findViewById(R.id.head_list);
  21. this.mViewHolder.mTextOffline = view.findViewById(R.id.txt_offline);
  22. this.mViewHolder.mTextOffline.setVisibility(View.GONE);
  23.  
  24. RetrieveDataAPI retrieveDataAPI = new RetrieveDataAPI(mContext,"");
  25. retrieveDataAPI.retornaValoresRetornados();
  26.  
  27. return view;
  28. }
  29. <...>
  30.  
  31. //Method that get JSON from RetrieveDataAPI
  32. public void recebeDados(String recebido) throws JSONException {
  33. recebidoAPI = recebido;
  34.  
  35. JSONObject jsonObject = new JSONObject(recebido);
  36.  
  37. for (int i = 0; i < jsonObject.length(); i++) {
  38. String id = jsonObject.getString("id");
  39. String name = jsonObject.getString("name");
  40. String description = jsonObject.getString("description");
  41.  
  42. Log.d("ListFragment", "id: " + id);
  43. Log.d("ListFragment", "name: " + name);
  44. Log.d("ListFragment", "description: " + description);
  45.  
  46. retornaValores(id, name, description);
  47. }
  48. }
  49.  
  50. private void retornaValores(String id_valor, String name_valor, String desc_valor) {
  51. ChatsEntity mChatsEntity;
  52. mChatsEntity = new ChatsEntity(id_valor, name_valor, desc_valor);
  53. mChatsEntity.setName(name_valor);
  54. mChatsEntity.setDescription(desc_valor);
  55.  
  56. if(id_valor == null){
  57. Log.d("ERRONULO1","null id");
  58. }else{
  59. listContents.add(mChatsEntity);
  60. listAux = listContents;
  61. }
  62. retornaList();
  63. }
  64.  
  65. private void retornaList() {
  66. if (listContents.size() == 0) {
  67. Log.e("listContents.size()","" + listContents.size());
  68.  
  69. } else {
  70. this.mGuestListAdapter = new ContactsAdapter(listContents, this.mChatsListener);
  71. this.mViewHolder.mRecyclerViewContacts.setLayoutManager(new LinearLayoutManager(getContext()));
  72. this.mViewHolder.mRecyclerViewContacts.setHasFixedSize(true);
  73.  
  74. this.mViewHolder.mRecyclerViewContacts.setAdapter(this.mGuestListAdapter);
  75. this.mGuestListAdapter.notifyDataSetChanged();
  76. }
  77. }
  78.  
  79. private static class ViewHolder {
  80. RecyclerView mRecyclerViewContacts;
  81. TextView mTextHead;
  82. TextView mTextOffline;
  83. }
  84.  
  85. public class RetrieveDataAPI {
  86.  
  87. private ViewHolder mViewHolder = new ViewHolder();
  88.  
  89. private Context mContext;
  90. public List<ChatsEntity> list_API;
  91.  
  92. public RetrieveDataAPI(Context context, String table_name) {
  93. this.mViewHolder.table_API = table_name;
  94. this.mContext = context;
  95. }
  96.  
  97. private void loadChats() {
  98. final ProgressDialog progressDialog = new ProgressDialog(mContext);
  99. progressDialog.setTitle("Loading list");
  100. progressDialog.setMessage("Loading...");
  101. progressDialog.show();
  102.  
  103. Runnable progressRunnable = new Runnable() {
  104. @Override
  105. public void run() {
  106. progressDialog.cancel();
  107. }
  108. };
  109.  
  110. Handler pdCanceller = new Handler();
  111. pdCanceller.postDelayed(progressRunnable, 5000);
  112.  
  113. StringRequest stringRequest = new StringRequest(com.android.volley.Request.Method.GET,
  114. ChatConstants.URL_PASTEL, new Response.Listener<String>() {
  115. @Override
  116. public void onResponse(String response) {
  117. progressDialog.dismiss();
  118. try {
  119. JSONArray jsonArray = new JSONArray(response);
  120.  
  121. for (int i = 0; i < jsonArray.length(); i++) {
  122.  
  123. JSONObject jo = jsonArray.getJSONObject(i);
  124. mViewHolder.valores = jo.toString();
  125. ListFragment listFragment = new ListFragment();
  126. listFragment.recebeDados(mViewHolder.valores);
  127. carregaValoresRetornados(mViewHolder.valores);
  128. }
  129. } catch (JSONException e) {
  130. Toast.makeText(mContext, e.getMessage() + "Problema ao carregar JSON!", Toast.LENGTH_LONG).show();
  131. e.printStackTrace();
  132. }
  133. }
  134. }, new Response.ErrorListener() {
  135. @Override
  136. public void onErrorResponse(VolleyError error) {
  137. Toast.makeText(mContext, "Error to load data", Toast.LENGTH_LONG).show();
  138. }
  139. });
  140. RequestQueue requestQueue = Volley.newRequestQueue(mContext);
  141. requestQueue.add(stringRequest);
  142. }
  143. public class ViewHolder{
  144. private String table_API;
  145. private String valores;
  146. }
  147. public void carregaValoresRetornados(String valoresAPI) {
  148. mViewHolder.valores = valoresAPI;
  149. }
  150. public String retornaValoresRetornados() {
  151. loadChats();
  152. return mViewHolder.valores;
  153. }
Add Comment
Please, Sign In to add comment