Advertisement
Guest User

Untitled

a guest
Feb 21st, 2019
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.73 KB | None | 0 0
  1. package it.proprionoi.volleytest;
  2.  
  3. import android.app.Activity;
  4. import android.util.Log;
  5. import android.view.View;
  6. import android.view.ViewGroup;
  7. import android.widget.BaseAdapter;
  8. import android.widget.ListAdapter;
  9. import android.widget.TextView;
  10.  
  11. import org.json.JSONArray;
  12. import org.json.JSONException;
  13. import org.json.JSONObject;
  14.  
  15. public class elencoUtentiAdapter extends BaseAdapter implements ListAdapter
  16. {
  17.  
  18. private JSONArray ar;
  19. private Activity activity;
  20.  
  21.  
  22. public elencoUtentiAdapter(Activity a, JSONArray ar)
  23. {
  24. this.activity = a;
  25. this.ar = ar;
  26. }
  27.  
  28. @Override
  29. public int getCount()
  30. {
  31. int retValue = 0;
  32.  
  33. if(ar != null)
  34. retValue = ar.length();
  35.  
  36. return retValue;
  37. }
  38.  
  39. @Override
  40. public JSONObject getItem(int i)
  41. {
  42. JSONObject retObject = null;
  43.  
  44. if(ar!=null)
  45. {
  46. try
  47. {
  48. retObject = ar.getJSONObject(i);
  49. }
  50. catch (JSONException ex)
  51. {
  52. Log.e("elencoUtentiAdapeter",ex.getMessage());
  53. }
  54. }
  55.  
  56. return retObject;
  57. }
  58.  
  59. @Override
  60. public long getItemId(int i)
  61. {
  62. long retID = 0;
  63.  
  64. if(ar!=null)
  65. {
  66. try
  67. {
  68. JSONObject o = getItem(i);
  69. retID = o.getInt("idutente");
  70. }
  71. catch (JSONException ex)
  72. {
  73. Log.e("elencoUtentiAdapeter",ex.getMessage());
  74. }
  75. }
  76.  
  77. return retID;
  78. }
  79.  
  80. @Override
  81. public View getView(int i, View view, ViewGroup viewGroup)
  82. {
  83. if(view==null)
  84. {
  85. view = activity.getLayoutInflater().inflate(R.layout.cella_layout,null);
  86. }
  87.  
  88. // Recupero il dato da mappare dall'arry JSON
  89.  
  90. JSONObject o = getItem(i);
  91.  
  92. // Ottengo i riferimenti JAVA agli oggetti del layout della cella
  93.  
  94. TextView nomeTextView = (TextView) view.findViewById(R.id.nomeTextView);
  95. TextView cognomeTextView = (TextView) view.findViewById(R.id.cognomeTextView);
  96. TextView emailTextView = (TextView) view.findViewById(R.id.emailTextView);
  97.  
  98. // Binding
  99. try
  100. {
  101. nomeTextView.setText(o.getString("nome"));
  102. cognomeTextView.setText(o.getString("cognome"));
  103. emailTextView.setText(o.getString("email"));
  104. }
  105. catch (JSONException ex)
  106. {
  107. Log.e("elencoUtentiAdapter",ex.getMessage());
  108. }
  109.  
  110. // Rendiamo la vista della cella appena creata/aggiornata al chiamante
  111. // Il chiamante è sempre la ListView
  112.  
  113. return view;
  114.  
  115.  
  116. }
  117. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement