IMPLEMENTASI CRUD DB ANDROID MENGGUNAKAN PHPMYADMIN
1. Manifest
<uses-permission android:name="android.permission.INTERNET" />
2. TAMPILKAN activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context="com.example.alimu.lastsunday.MainActivity"> <Button android:id="@+id/buttonTambahBiodata" android:layout_width="186dp" android:layout_height="wrap_content" android:layout_gravity="center" android:text="Tambah Biodata"/> <HorizontalScrollView android:id="@+id/horizontalScrollView" android:layout_width="match_parent" android:layout_height="wrap_content"> <ScrollView android:id="@+id/verticalScrollView" android:layout_width="wrap_content" android:layout_height="wrap_content"> <TableLayout android:id="@+id/tableBiodata" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="80dp"> </TableLayout> </ScrollView> </HorizontalScrollView> </LinearLayout>
3. Koneksi.java
public class Koneksi { // Mendapatkan nilai bit yang diperlukanpublic String call(String url) { int BUFFER_SIZE = 2000;InputStream in = null; try { in = OpenHttpConnection(url); } catch (IOException e) { } // Membaca nilai bit menjadi nilai character InputStreamReader inputStreamReader = new InputStreamReader(in); int charRead; String string = ""; char[] inputBuffer = new char[BUFFER_SIZE]; try { while ((charRead = inputStreamReader.read(inputBuffer)) > 0) { String readString = String.copyValueOf(inputBuffer, 0, charRead); string += readString; inputBuffer = new char[BUFFER_SIZE]; } in.close(); } catch (IOException e) { e.printStackTrace(); return ""; } return string;} // Membuka URL dan meminta respon dari inputStreamReaderprivate InputStream OpenHttpConnection(String url) throws IOException { InputStream in = null; int response = -1; URL url1 = new URL(url); URLConnection conn = url1.openConnection(); if (!(conn instanceof HttpURLConnection)) throw new IOException("Not An Http Connection"); try { HttpURLConnection httpurlconnection = (HttpURLConnection) conn; httpurlconnection.setAllowUserInteraction(false); httpurlconnection.setInstanceFollowRedirects(true); httpurlconnection.setRequestMethod("GET"); httpurlconnection.connect(); response = httpurlconnection.getResponseCode(); if (response == HttpURLConnection.HTTP_OK) { in = httpurlconnection.getInputStream(); } } catch (Exception e) { throw new IOException("Error Connecting"); } return in;} }
4. Biodata.java
public class Biodata extends Koneksi { // Source Code untuk URL -> URL menggunakan IP Address yang didapat dari komputer / laptop String URL = " http://192.168.43.170/android_mysql/api_server.php"; String url = ""; String response = ""; // Menampilkan biodata dari database public String tampilBiodata() { try { url = URL + "?operasi=view"; System.out.println("URL Tampil Biodata : " + url); response = call(url); } catch (Exception e) { } return response; } // Memasukkan data biodata baru ke database public String insertBiodata(String nama, String alamat) { try { url = URL + "?operasi=insert&nama=" + nama + "&alamat=" + alamat; System.out.println("URL Insert Biodata : " + url); response = call(url); } catch (Exception e) { } return response; } // Melihat biodata yang diinginkan melalui ID public String getBiodataById (int id) { try { url = URL + "?operasi=get_biodata_by_id&id=" + id; System.out.println("URL Insert Biodata : " + url); response = call(url); } catch (Exception e) { } return response; } // Mengubah isi biodata yang sudah ada di database public String updateBiodata (String id, String nama, String alamat) { try { url = URL + "?operasi=update&id=" + id + "&nama=" + nama + "&alamat=" + alamat; System.out.println("URL Insert Biodata : " + url); response = call(url); } catch (Exception e) { } return response; } // Menghapus salah satu biodata pada database public String deleteBiodata (int id) { try { url = URL + "?operasi=delete&id=" + id; System.out.println("URL Insert Biodata : " + url); response = call(url); } catch (Exception e) { } return response; } }
5. MainActivity.Java
public class MainActivity extends AppCompatActivity implements View.OnClickListener { Biodata Biodata = new Biodata(); TableLayout tableLayout; Button buttonTambahBiodata; ArrayList<Button> buttonEdit = new ArrayList<Button>(); ArrayList<Button>buttonDelete = new ArrayList<Button>(); JSONArray arrayBiodata; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // Jika SDK Android diatas API Ver.9 if (android.os.Build.VERSION.SDK_INT > 5) { StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build(); StrictMode.setThreadPolicy(policy); } // Mendapatkan data widget dari XML Activity melalui ID tableLayout = (TableLayout) findViewById(R.id.tableBiodata); buttonTambahBiodata = (Button) findViewById(R.id.buttonTambahBiodata); buttonTambahBiodata.setOnClickListener(this);// Menambahkan baris untuk tabel TableRow barisTabel = new TableRow(this); barisTabel.setBackgroundColor(Color.CYAN);// Menambahkan tampilan teks untuk judul pada tabel TextView viewHeaderId = new TextView(this); TextView viewHeaderNama = new TextView(this); TextView viewHeaderAlamat = new TextView(this); TextView viewHeaderAction = new TextView(this); viewHeaderId.setText("ID"); viewHeaderNama.setText("Nama"); viewHeaderAlamat.setText("Alamat"); viewHeaderAction.setText("Action"); viewHeaderId.setPadding(5, 1, 5, 1); viewHeaderNama.setPadding(5, 1, 5, 1); viewHeaderAlamat.setPadding(5, 1, 5, 1); viewHeaderAction.setPadding(5, 1, 5, 1);// Menampilkan tampilan TextView ke dalam tabel barisTabel.addView(viewHeaderId); barisTabel.addView(viewHeaderNama); barisTabel.addView(viewHeaderAlamat); barisTabel.addView(viewHeaderAction);// Menyusun ukuran dari tabel tableLayout.addView(barisTabel, new TableLayout.LayoutParams(TableLayout.LayoutParams.WRAP_CONTENT, TableLayout.LayoutParams.WRAP_CONTENT)); try { // Mengubah data dari Biodata yang berupa String menjadi array arrayBiodata = new JSONArray(Biodata.tampilBiodata()); for (int i = 0; i < arrayBiodata.length(); i++) { JSONObject jsonChildNode = arrayBiodata.getJSONObject(i); String nama = jsonChildNode.optString("nama"); String alamat = jsonChildNode.optString("alamat"); String id = jsonChildNode.optString("id"); System.out.println("Nama : " + nama ); System.out.println("Alamat : " + alamat); System.out.println("ID : " + id); barisTabel = new TableRow(this);// Memberi warna pada baris tabel if (i % 2 == 0) { barisTabel.setBackgroundColor(Color.LTGRAY); } TextView viewId = new TextView(this); viewId.setText(id); viewId.setPadding(5, 1, 5, 1); barisTabel.addView(viewId); TextView viewNama = new TextView(this); viewNama.setText(nama); viewNama.setPadding(5, 1, 5, 1); barisTabel.addView(viewNama); TextView viewAlamat = new TextView(this); viewAlamat.setText(alamat); viewAlamat.setPadding(5, 1, 5, 1); barisTabel.addView(viewAlamat);// Menambahkan button Edit buttonEdit.add(i, new Button(this)); buttonEdit.get(i).setId(Integer.parseInt(id)); buttonEdit.get(i).setTag("Edit"); buttonEdit.get(i).setText("Edit"); buttonEdit.get(i).setOnClickListener(this); barisTabel.addView(buttonEdit.get(i));// Menambahkan tombol Delete buttonDelete.add(i, new Button(this)); buttonDelete.get(i).setId(Integer.parseInt(id)); buttonDelete.get(i).setTag("Delete"); buttonDelete.get(i).setText("Delete"); buttonDelete.get(i).setOnClickListener(this); barisTabel.addView(buttonDelete.get(i)); tableLayout.addView(barisTabel, new TableLayout.LayoutParams (TableLayout.LayoutParams.MATCH_PARENT, TableLayout.LayoutParams.MATCH_PARENT)); } } catch (JSONException e) { e.printStackTrace(); } } public void onClick (View view) { if (view.getId() == R.id.buttonTambahBiodata) { tambahBiodata(); } else { for (int i= 0; i < buttonEdit.size(); i++) { // Jika ingin mengedit data pada biodata if (view.getId() == buttonEdit.get(i).getId() && view.getTag().toString().trim().equals("Edit")) { Toast.makeText(MainActivity.this, "Edit : " + buttonEdit.get(i).getId(), Toast.LENGTH_SHORT).show(); int id = buttonEdit.get(i).getId(); getDataByID(id); } // Menghapus data di Tabel else if (view.getId() == buttonDelete.get(i).getId() && view.getTag().toString().trim().equals("Delete")){ Toast.makeText(MainActivity.this, "Delete : " + buttonDelete.get(i).getId(), Toast.LENGTH_SHORT).show(); int id = buttonDelete.get(i).getId(); deleteBiodata(id); } } } } public void deleteBiodata (int id) { Biodata.deleteBiodata(id); finish(); startActivity(getIntent()); } // Mendapatkan Biodata melalui ID public void getDataByID (int id) { String namaEdit = null, alamatEdit = null; JSONArray arrayPersonal; try { arrayPersonal = new JSONArray(Biodata.getBiodataById(id)); for (int i = 0; i < arrayPersonal.length(); i++) { JSONObject jsonChildNode = arrayPersonal.getJSONObject(i); namaEdit = jsonChildNode.optString("nama"); alamatEdit = jsonChildNode.optString("alamat"); } } catch (JSONException e) { e.printStackTrace(); } LinearLayout layoutInput = new LinearLayout(this); layoutInput.setOrientation(LinearLayout.VERTICAL);// Membuat id tersembunyi pada AlertDialog final TextView viewId = new TextView(this); viewId.setText(String.valueOf(id)); viewId.setTextColor(Color.TRANSPARENT); layoutInput.addView(viewId); final EditText editNama = new EditText(this); editNama.setText(namaEdit); layoutInput.addView(editNama); final EditText editAlamat = new EditText(this); editAlamat.setText(alamatEdit); layoutInput.addView(editAlamat);// Membuat AlertDialog untuk mengubah data di Biodata AlertDialog.Builder builderEditBiodata = new AlertDialog.Builder(this); //builderEditBiodata.setIcon(R.drawable.webse); builderEditBiodata.setTitle("Update Biodata"); builderEditBiodata.setView(layoutInput); builderEditBiodata.setPositiveButton("Update", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { String nama = editNama.getText().toString(); String alamat = editAlamat.getText().toString(); System.out.println("Nama : " + nama + "Alamat : " + alamat); String laporan = Biodata.updateBiodata(viewId.getText().toString(), editNama.getText().toString(), editAlamat.getText().toString()); Toast.makeText(MainActivity.this, laporan, Toast.LENGTH_SHORT).show(); finish(); startActivity(getIntent()); } });// Jika tidak ingin mengubah data pada Biodata builderEditBiodata.setNegativeButton("Cancel", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { dialog.cancel(); } }); builderEditBiodata.show(); } public void tambahBiodata() { LinearLayout layoutInput = new LinearLayout(this); layoutInput.setOrientation(LinearLayout.VERTICAL); final EditText editNama = new EditText(this); editNama.setHint("Nama"); layoutInput.addView(editNama); final EditText editAlamat = new EditText(this); editAlamat.setHint("Alamat"); layoutInput.addView(editAlamat);// Membuat AlertDialog untuk menambahkan data pada Biodata AlertDialog.Builder builderInsertBiodata= new AlertDialog.Builder(this); //builderInsertBiodata.setIcon(R.drawable.webse); builderInsertBiodata.setTitle("Insert Biodata"); builderInsertBiodata.setView(layoutInput); builderInsertBiodata.setPositiveButton("Insert", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { String nama = editNama.getText().toString(); String alamat = editAlamat.getText().toString(); System.out.println("Nama : " + nama + "Alamat : " + alamat); String laporan = Biodata.insertBiodata(nama, alamat); Toast.makeText(MainActivity.this, laporan, Toast.LENGTH_SHORT).show(); finish(); startActivity(getIntent()); } }); builderInsertBiodata.setNegativeButton("Cancel", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { dialog.cancel(); } }); builderInsertBiodata.show(); } }
Comments
Post a Comment