nafiefn_10

TampilSemuaMhs.java (crud)

Jul 9th, 2021
258
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.48 KB | None | 0 0
  1. package nofieanggraeni.blogspot.com.crud_phpmysql;
  2.  
  3. import android.app.ProgressDialog;
  4. import android.content.Intent;
  5. import android.os.AsyncTask;
  6. import androidx.appcompat.app.AppCompatActivity;
  7. import android.os.Bundle;
  8. import android.util.Log;
  9. import android.view.View;
  10. import android.widget.AdapterView;
  11. import android.widget.ListAdapter;
  12. import android.widget.ListView;
  13. import android.widget.SimpleAdapter;
  14. import org.json.JSONArray;
  15. import org.json.JSONException;
  16. import org.json.JSONObject;
  17. import java.util.ArrayList;
  18. import java.util.HashMap;
  19.  
  20. public class TampilSemuaMhs extends AppCompatActivity implements ListView.OnItemClickListener{
  21.     private ListView listView;
  22.     private String JSON_STRING;
  23.  
  24.     @Override protected void onCreate(Bundle savedInstanceState) {
  25.         super.onCreate(savedInstanceState);
  26.         setContentView(R.layout.activity_tampil_semua_mhs);
  27.         listView = (ListView) findViewById(R.id.listView);
  28.         listView.setOnItemClickListener(this);
  29.         getJSON();
  30.     }
  31.  
  32.     private void showMhs(){
  33.         JSONObject jsonObject = null;
  34.         ArrayList<HashMap<String,String>> list = new ArrayList<HashMap<String, String>>();
  35.         try {
  36.             jsonObject = new JSONObject(JSON_STRING);
  37.             JSONArray result = jsonObject.getJSONArray(konfigurasi.TAG_JSON_ARRAY);
  38.             for(int i = 0; i<result.length(); i++){
  39.                 JSONObject jo = result.getJSONObject(i);
  40.                 String id = jo.getString(konfigurasi.TAG_ID);
  41.  
  42.                 String name = jo.getString(konfigurasi.TAG_NAMA);
  43.                 HashMap<String,String> mhs = new HashMap<>();
  44.                 mhs.put(konfigurasi.TAG_ID,id);
  45.                 mhs.put(konfigurasi.TAG_NAMA,name);
  46.                 list.add(mhs);
  47.             }
  48.         }
  49.         catch (JSONException e) { e.printStackTrace();
  50.         }
  51.         ListAdapter adapter = new SimpleAdapter(
  52.                 TampilSemuaMhs.this, list, R.layout.list_item,
  53.                 new String[]{konfigurasi.TAG_ID,konfigurasi.TAG_NAMA},
  54.                 new int[]{R.id.id, R.id.name});
  55.         listView.setAdapter(adapter);
  56.     }
  57.     private void getJSON(){
  58.         class GetJSON extends AsyncTask<Void,Void,String>{
  59.             ProgressDialog loading;
  60.             @Override
  61.             protected void onPreExecute() {
  62.                 super.onPreExecute();
  63.                 loading = ProgressDialog.show(TampilSemuaMhs.this,"Mengambil Data","Mohon Tunggu...",false,false);
  64.             }
  65.             @Override
  66.             protected void onPostExecute(String s) {
  67.                 super.onPostExecute(s);
  68.                 loading.dismiss();
  69.                 JSON_STRING = s;
  70.                 showMhs();
  71.             }
  72.             @Override protected String doInBackground(Void... params) { RequestHandler rh = new RequestHandler();
  73.                 Log.d("testing","test");
  74.                 String s = rh.sendGetRequest(konfigurasi.URL_GET_ALL);
  75.                 Log.d("response",s);
  76.  
  77.                 return s;
  78.             }
  79.         }
  80.         GetJSON gj = new GetJSON();
  81.         gj.execute();
  82.     }
  83.     @Override
  84.     public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
  85.         Intent intent = new Intent(this, TampilMhs.class);
  86.         HashMap<String,String> map =(HashMap)parent.getItemAtPosition(position);
  87.         String mhsId = map.get(konfigurasi.TAG_ID).toString();
  88.         intent.putExtra(konfigurasi.MHS_ID,mhsId);
  89.         startActivity(intent);
  90.     }
  91. }
Advertisement
Add Comment
Please, Sign In to add comment