Mata tak lagi terpejam. Rasa gelisah, resah menghantui pelupuk mata. Menusuk hingga ruang kesabaran jiwa.
Ahh..kemana sang rembulan?.. tak lagi berbagi cahaya!, tak lagi menyinari indahnya malam. meninggalkan kegelapan. hilang dalam sunyi malam.
Oke, kata-kata mutiaranya segitu aja, :p . Lanjut tutorial karna ane ndak bisa tidur semalam jadinya iseng-iseng mainin eclipse.
Kali ini, Ane berbagi catatan tentang membuat aplikasi MyLyric di Android dengan menggunakan bawaan database SQLite di android. Dimana, disini telah di lengkapi dengan fitur CRUD ( Create, Read, Update dan Delete ).
Berikut Tampilannya:
Disini ane memanfaatkan beberapa file XML untuk UI-nya dan beberapa file JAVA, Ane tidak tampilin semua, file-filenya, download aja file lengkapnya di akhir catatatn ini untuk bisa dipelajari atau di kembangkan lagi.
Berikut file main.xml nya:
Berikut file MainActivity.java nya
package com.mylyrik.yonandroid; import java.util.ArrayList; import com.mylyrik.yonandroid.adapter.LyrikAdapter; import com.mylyrik.yonandroid.database.LyrikDB; import com.mylyrik.yonandroid.model.Lyrik; import android.text.Editable; import android.text.TextWatcher; import android.util.Log; import android.app.AlertDialog; import android.content.DialogInterface; import android.content.Intent; import android.database.sqlite.SQLiteDatabase; import android.os.Bundle; import android.view.ContextMenu; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.view.Window; import android.view.ContextMenu.ContextMenuInfo; import android.widget.AdapterView; import android.widget.EditText; import android.widget.ListView; import android.widget.TextView; import android.widget.Toast; import android.widget.AdapterView.OnItemClickListener; public class MainActivity extends DashBoardActivity { private ListView lsvLyrikView = null; private LyrikAdapter adapter = null; private LyrikDB lyrikdb = null; private SQLiteDatabase db = null; private ArrayListlistLyrik = null; private EditText txtSearchText = null; private TextView txtNoDataLyrik; AlertDialog.Builder alertDialogConfirmDelete = null; private Lyrik lyrikIWantToDelete = null; public static String id = ""; public static String judul = ""; public static String penyanyi = ""; public static String isi = ""; private static void getId(String id) { MainActivity.id = id; } private static void getJudul(String judul) { MainActivity.judul = judul; } private static void getPenyanyi(String penyanyi) {MainActivity.penyanyi = penyanyi;} private static void getIsi(String isi) { MainActivity.isi = isi; } /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); requestWindowFeature(Window.FEATURE_NO_TITLE); setContentView(R.layout.main); setHeader(getString(R.string.HomeActivityTitle), false, true); if (getIntent().getBooleanExtra("EXIT", false)) { finish(); } txtNoDataLyrik = (TextView) findViewById(R.id.txtNoDataLyrik); lsvLyrikView = (ListView) findViewById(R.id.lsvListLyrik); lsvLyrikView.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView parent, View view,int position, long id) { String _id = ((TextView) view.findViewById(R.id.txtId)).getText().toString(); String _judul = ((TextView) view.findViewById(R.id.txtJudul)).getText().toString(); String _penyanyi = ((TextView) view.findViewById(R.id.txtPenyanyi)).getText().toString(); String _isi = ((TextView) view.findViewById(R.id.txtIsi)).getText().toString(); getId(_id); getJudul(_judul); getPenyanyi(_penyanyi); getIsi(_isi); Intent intent = new Intent(getApplicationContext(), DetailLyrik.class); overridePendingTransition( R.anim.slide_in_left, R.anim.slide_out_left ); startActivity(intent); } }); refreshListAdapter(); registerForContextMenu(lsvLyrikView); txtSearchText = (EditText) findViewById(R.id.searchText); txtSearchText.addTextChangedListener( new TextWatcher() { @Override //we don't need this public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) { } @Override //we don't need this public void beforeTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) { } @Override public void afterTextChanged(Editable text) { refreshListAdapter( text.toString() ); } }); DialogInterface.OnClickListener dialogConfirmDeleteClickListener = new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { switch (which){ case DialogInterface.BUTTON_POSITIVE: if( lyrikIWantToDelete != null ) { Log.i("Log", "item " + lyrikIWantToDelete.getJudul() + " deleted!"); showToast( "item " + lyrikIWantToDelete.getJudul() + " deleted!" ); lyrikdb.delete(lyrikIWantToDelete); lyrikIWantToDelete = null; refreshListAdapter(); } break; case DialogInterface.BUTTON_NEGATIVE: //No button clicked break; } } }; alertDialogConfirmDelete = new AlertDialog.Builder(this); alertDialogConfirmDelete.setPositiveButton("Yes", dialogConfirmDeleteClickListener); alertDialogConfirmDelete.setNegativeButton("No", dialogConfirmDeleteClickListener); } // TODO Get data from database SQLite private void getListLyrik() { lyrikdb = new LyrikDB(getApplicationContext()); adapter = new LyrikAdapter(this); db = lyrikdb.getWritableDatabase(); lyrikdb.createTable(db, getBaseContext()); //lyrikdb.generateData(db); } // TODO We need to refresh adapter for every data update. Without it, ListView will never be refreshed protected void refreshListAdapter() { try { getListLyrik(); } catch(Exception e) { Toast.makeText(MainActivity.this, e.toString(), Toast.LENGTH_SHORT).show(); } finally { listLyrik = lyrikdb.toArray(); adapter.updatelistLyrik(listLyrik); lsvLyrikView.setAdapter(adapter); if (adapter.getCount() == 0) { txtNoDataLyrik.setVisibility(View.VISIBLE); } else { txtNoDataLyrik.setVisibility(View.GONE); } } } // TODO We need to refresh adapter for every data update. Without it, ListView will never be refreshed protected void refreshListAdapter(String filterText) { listLyrik = lyrikdb.toArray( filterText ); adapter.updatelistLyrik(listLyrik); lsvLyrikView.setAdapter(adapter); if (adapter.getCount() == 0) { txtNoDataLyrik.setVisibility(View.VISIBLE); } else { txtNoDataLyrik.setVisibility(View.GONE); } } @Override public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo cmInfo) { if (v.getId() == R.id.lsvListLyrik) { AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) cmInfo; menu.setHeaderTitle(listLyrik.get(info.position).getJudul()); String[] menuItems = { "Update", "Delete" }; for (int menuIndex = 0; menuIndex < menuItems.length; menuIndex++) { menu.add(Menu.NONE, menuIndex, menuIndex, menuItems[menuIndex]); } } } @Override public boolean onContextItemSelected(MenuItem item) { AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo(); int menuItemIndex = item.getItemId(); Lyrik lyrik = listLyrik.get(info.position); switch (menuItemIndex) { case 0: String _id = lyrik.getId(); String _judul = lyrik.getJudul(); String _penyanyi = lyrik.getPenyanyi(); String _isi = lyrik.getIsi(); getId(_id); getJudul(_judul); getPenyanyi(_penyanyi); getIsi(_isi); Intent intent = new Intent(getApplicationContext(), EditLyrik.class); overridePendingTransition( R.anim.slide_in_left, R.anim.slide_out_left ); startActivity(intent); break; case 1: lyrikIWantToDelete = lyrik; alertDialogConfirmDelete.setMessage("Delete " + lyrik.getJudul() + "?"); alertDialogConfirmDelete.show(); break; } return true; } @Override protected void onDestroy() { super.onDestroy(); if ((lyrikdb != null)) { lyrikdb.close(); } if (db != null) { db.close(); } } }
0 komentar:
Post a Comment