Guest User

Untitled

a guest
Sep 19th, 2018
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.76 KB | None | 0 0
  1. public class TampilUser extends AppCompatActivity {
  2.  
  3. ListView listView;
  4. CustomAdapter customAdapter;
  5. ArrayList<User> user = new ArrayList<>();
  6.  
  7. @Override
  8. protected void onCreate(Bundle savedInstanceState) {
  9. super.onCreate(savedInstanceState);
  10. setContentView(R.layout.activity_tampil_user);
  11.  
  12. listView = (ListView) findViewById(R.id.listUser);
  13.  
  14. getJSON("http://www.trigroupindonesia.com/php/read_user.php");
  15. }
  16.  
  17. @Override
  18. public void onResume() {
  19. super.onResume();
  20.  
  21. getJSON("http://www.trigroupindonesia.com/php/read_user.php");
  22. }
  23.  
  24. private void getJSON(final String urlWebService) {
  25.  
  26. class GetJSON extends AsyncTask<Void, Void, String> {
  27.  
  28. @Override
  29. protected void onPreExecute() {
  30. super.onPreExecute();
  31. }
  32.  
  33. @Override
  34. protected void onPostExecute(String s) {
  35. super.onPostExecute(s);
  36. Toast.makeText(getApplicationContext(), s, Toast.LENGTH_SHORT).show();
  37. try {
  38. loadIntoListView(s);
  39. } catch (JSONException e) {
  40. e.printStackTrace();
  41. }
  42. }
  43.  
  44. @Override
  45. protected String doInBackground(Void... voids) {
  46. try {
  47. URL url = new URL(urlWebService);
  48. HttpURLConnection con = (HttpURLConnection) url.openConnection();
  49. StringBuilder sb = new StringBuilder();
  50. BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(con.getInputStream()));
  51. String json;
  52. sb.setLength(0);
  53. while ((json = bufferedReader.readLine()) != null) {
  54. sb.append(json + "n");
  55. }
  56. bufferedReader.close();
  57. con.disconnect();
  58. return sb.toString().trim();
  59. } catch (Exception e) {
  60. return null;
  61. }
  62. }
  63. }
  64. GetJSON getJSON = new GetJSON();
  65. getJSON.execute();
  66. }
  67.  
  68. public void loadIntoListView(String json) throws JSONException {
  69. JSONArray jsonArray = new JSONArray(json);
  70. user = new ArrayList<>();
  71. user.clear();
  72. User u;
  73. for (int i = 0; i < jsonArray.length(); i++) {
  74. JSONObject obj = jsonArray.getJSONObject(i);
  75. String email = obj.getString("email");
  76. String nama = obj.getString("nama");
  77.  
  78. u = new User();
  79. u.setEmail(email);
  80. u.setNama(nama);
  81.  
  82. user.add(u);
  83. }
  84. customAdapter = new CustomAdapter(getApplicationContext(),user);
  85. listView.setAdapter(customAdapter);
  86.  
  87. listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
  88. @Override
  89. public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
  90. String nama = customAdapter.user.get(position).getNama();
  91. String email = customAdapter.user.get(position).getEmail();
  92. Intent i = new Intent(getApplicationContext(), DetailUser.class);
  93.  
  94. i.putExtra("email", email);
  95. i.putExtra("nama", nama);
  96. startActivityForResult(i, 0);
  97. }
  98. });
  99. }
  100. @Override
  101. protected void onActivityResult(int requestCode, int resultCode, Intent data){
  102. getJSON("http://www.trigroupindonesia.com/php/read_user.php");
  103. customAdapter.notifyDataSetChanged();
  104. customAdapter.notifyDataSetInvalidated();
  105. }
  106. }
  107.  
  108. public View getView(int position, View convertView, ViewGroup parent) {
  109.  
  110. if(convertView==null)
  111. {
  112. convertView=inflater.inflate(R.layout.daftar_user,parent,false);
  113. }
  114. TextView textView_email = (TextView) convertView.findViewById(R.id.textView_email);
  115. TextView textView_nama = (TextView) convertView.findViewById(R.id.textView_nama);
  116.  
  117. textView_email.setText(user.get(position).getEmail());
  118. textView_nama.setText(user.get(position).getNama());
  119.  
  120. return convertView;
  121. }
Add Comment
Please, Sign In to add comment