From f30b4fcd3a525d2fa3c2d85fb11e8e7aa3a6a031 Mon Sep 17 00:00:00 2001 From: Tom Willemsen Date: Fri, 10 Aug 2012 22:03:34 +0200 Subject: [PATCH] Add edit and delete functionality * res/values/strings.xml: Add strings for `main_context_menu_edit' and `main_context_menu_delete'. * src/ryuslash/org/dvdroid/AddDVDActivity.java (saveDVD): Renamed from `createDVD'. When ID is greater than 0 update, otherwise create. (onCreate): Get ID from the `Intent' that was used to start this activity. If ID is greater than 0, show known information in the inputs. * src/ryuslash/org/dvdroid/DVDDataSource.java: (createValues): (updateDVD): (getById): New functions. (createDVD): Use `createValues' to get the values for the database. (deleteDVD): Add overloaded function that takes a `long', make the original function use this to delete a `DVD'. * src/ryuslash/org/dvdroid/DVDroidActivity.java: Implement `ActionMode.Callback'. (onActivityResult): If REQUESTCODE is `2' reset the intent information so as not to confuse it during its next invocation. Finish action mode. (onActionItemClicked): (onCreateActionMode): (onDestroyActionMode): (onListItemClick): (onPrepareActionMode): (reloadList): (showEditDVD): New functions. (onCreate): Instantiate the ADD_DVD `Intent'. --- res/drawable-hdpi/delete.png | Bin 0 -> 1611 bytes res/drawable-hdpi/edit.png | Bin 0 -> 1875 bytes res/drawable-mdpi/delete.png | Bin 0 -> 1358 bytes res/drawable-mdpi/edit.png | Bin 0 -> 1490 bytes res/menu/main_context_menu.xml | 11 ++ res/values/strings.xml | 2 + src/ryuslash/org/dvdroid/AddDVDActivity.java | 34 ++++++- src/ryuslash/org/dvdroid/DVDDataSource.java | 41 +++++++- src/ryuslash/org/dvdroid/DVDroidActivity.java | 94 ++++++++++++++++-- 9 files changed, 165 insertions(+), 17 deletions(-) create mode 100644 res/drawable-hdpi/delete.png create mode 100644 res/drawable-hdpi/edit.png create mode 100644 res/drawable-mdpi/delete.png create mode 100644 res/drawable-mdpi/edit.png create mode 100644 res/menu/main_context_menu.xml diff --git a/res/drawable-hdpi/delete.png b/res/drawable-hdpi/delete.png new file mode 100644 index 0000000000000000000000000000000000000000..ffd19d9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0FuvZE&u=k literal 0 HcmV?d00001 diff --git a/res/drawable-hdpi/edit.png b/res/drawable-hdpi/edit.png new file mode 100644 index 0000000000000000000000000000000000000000..3eb5190fb44de54efc5a1675090aeedb25b1d0c6 GIT binary patch literal 1875 zcmaJ?Yfuwc6b?iLf}jIZq6J*yyFjuV5+NZ`LKY%wXe3bq6$!~A2__q}izE~a6;ROf zXnj#}tS_tzMnoP}5CpACE22&DiB1rO4p>1E3p#2yDz-ngyR&=mJ#)VAeCM&Vd6Gzx zGi5S`L?Ssy%!i|h6|_DMV~O`N+mTJgG6@fh#-(T?o+{TPB!Lo5K!6C1d^Hk<$d$&m zEeMZ9B2QDvqVZ_)BCZ0}(B)PixymGzI6=2&L~*(C9wUYO(iaA_~SZ$aM^e4q8(hbrg&L-&Lde;Emx? z$iI3&N{q>jI)o90U}%b7K{PILsx_33E7T)$9M#KEbj@fLB}pibVo9hD5K7r}2oTE^ zDy`M>79kdMBeWPU*D8<*m`@`(=qiWSRgape)NMT zDPK;#*mmny@y?d+=~d^!#Scnb>|IAP$5(b{EsY&?HBPDi*62G0j#IPXX}ijY<3q*Y zyC?WDPUN-P!*SP(PjD{wo8BDxsf)L+hSSC=!)_eMuB_jKT^Y(S^))>1YbdzWGkA(S zsJl4kJaNN0nGHqGThCHu&NnRD)f0+b>f}6DM2JUH_7~N9Eef&Y zZTRA6@`0@NwNl#)LZ6O}u?E{Z!23mL9xRJF|FFqvWpLZ$x^t>3$F@`86=vX==S=|y z$C*AcG`i{vZ(UXsbMBi}jc$=PuY-q4n|;nbjWa!d?i*yYGJaym_FTIO_I5>7N1MKs zoxVB7*Atfh24dnv2_4k>7|eH{%jO4yFOwVH3;e3Qca)i)#CG$4?eW+qzHBNXkKE3| zwk<%9<}P;|FpAVZ!Cl8*X14T{R;(OZKhH1QT(RUxwP#%L%E;Qp^u4)RlJZwg4rxv? zPd9e;d?U%PUAlip_KpzuiGr-n!N<+Ex7GC-R7QuSe7y8jhvivb`I!qZ`@q4jEx2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7+O9uGMd@aqnMs+dBH-V%`TKM+CD()?aA- z*ju50WBrrY7k_ literal 0 HcmV?d00001 diff --git a/res/menu/main_context_menu.xml b/res/menu/main_context_menu.xml new file mode 100644 index 0000000..9cdf622 --- /dev/null +++ b/res/menu/main_context_menu.xml @@ -0,0 +1,11 @@ + + + + + diff --git a/res/values/strings.xml b/res/values/strings.xml index dc3e703..0a4ee4d 100644 --- a/res/values/strings.xml +++ b/res/values/strings.xml @@ -6,4 +6,6 @@ Watched Add Save + Edit + Delete diff --git a/src/ryuslash/org/dvdroid/AddDVDActivity.java b/src/ryuslash/org/dvdroid/AddDVDActivity.java index f5244c3..cd7b853 100644 --- a/src/ryuslash/org/dvdroid/AddDVDActivity.java +++ b/src/ryuslash/org/dvdroid/AddDVDActivity.java @@ -13,8 +13,9 @@ import android.widget.EditText; public class AddDVDActivity extends Activity { private DVDDataSource datasource; + private long id; - public void createDVD() + public void saveDVD() { EditText name_entry = (EditText)findViewById(R.id.name_entry); CheckBox watched_entry = @@ -22,9 +23,15 @@ public class AddDVDActivity extends Activity CheckBox wishlist_entry = (CheckBox)findViewById(R.id.wishlist_entry); - datasource.createDVD(name_entry.getText().toString(), - watched_entry.isChecked() ? 1 : 0, - wishlist_entry.isChecked() ? 1 : 0); + if (id > 0) { + datasource.updateDVD(id, name_entry.getText().toString(), + watched_entry.isChecked() ? 1 : 0, + wishlist_entry.isChecked() ? 1 : 0); + } else { + datasource.createDVD(name_entry.getText().toString(), + watched_entry.isChecked() ? 1 : 0, + wishlist_entry.isChecked() ? 1 : 0); + } } @Override @@ -35,6 +42,23 @@ public class AddDVDActivity extends Activity datasource = new DVDDataSource(this); datasource.open(); + + Intent intent = getIntent(); + id = intent.getLongExtra(SQLiteHelper.COLUMN_ID, 0l); + + if (id > 0) { + DVD dvd = datasource.getById(id); + EditText name_entry = + (EditText) findViewById(R.id.name_entry); + CheckBox watched_entry = + (CheckBox) findViewById(R.id.watched_entry); + CheckBox wishlist_entry = + (CheckBox) findViewById(R.id.wishlist_entry); + + name_entry.setText(dvd.getTitle()); + watched_entry.setChecked(dvd.getWatched() == 1); + wishlist_entry.setChecked(dvd.getWishlist() == 1); + } } @Override @@ -57,7 +81,7 @@ public class AddDVDActivity extends Activity { switch (item.getItemId()) { case R.id.add_menu_save: - createDVD(); + saveDVD(); finish(); return true; } diff --git a/src/ryuslash/org/dvdroid/DVDDataSource.java b/src/ryuslash/org/dvdroid/DVDDataSource.java index b9fe166..e10fb24 100644 --- a/src/ryuslash/org/dvdroid/DVDDataSource.java +++ b/src/ryuslash/org/dvdroid/DVDDataSource.java @@ -33,7 +33,8 @@ public class DVDDataSource dbHelper.close(); } - public DVD createDVD(String title, int watched, int wishlist) + private ContentValues createValues(String title, int watched, + int wishlist) { ContentValues values = new ContentValues(); @@ -41,6 +42,12 @@ public class DVDDataSource values.put(SQLiteHelper.COLUMN_WATCHED, watched); values.put(SQLiteHelper.COLUMN_WISHLIST, wishlist); + return values; + } + + public DVD createDVD(String title, int watched, int wishlist) + { + ContentValues values = createValues(title, watched, wishlist); long insertId = database.insert(SQLiteHelper.TABLE_DVD, null, values); Cursor cursor = database.query(SQLiteHelper.TABLE_DVD, @@ -57,14 +64,42 @@ public class DVDDataSource return newDVD; } + public void updateDVD(long id, String title, int watched, int wishlist) + { + ContentValues values = createValues(title, watched, wishlist); + String whereArgs[] = { "" + id }; + + database.update(SQLiteHelper.TABLE_DVD, values, "id = ?", + whereArgs); + } + public void deleteDVD(DVD dvd) { - long id = dvd.getId(); - System.out.println("DVD deleted with id: " + id); + deleteDVD(dvd.getId()); + } + + public void deleteDVD(long id) + { database.delete(SQLiteHelper.TABLE_DVD, SQLiteHelper.COLUMN_ID + " = " + id, null); } + public DVD getById(long id) + { + Cursor cursor = database.query(SQLiteHelper.TABLE_DVD, + allColumns, + SQLiteHelper.COLUMN_ID + " = " + id, + null, null, null, null); + + cursor.moveToFirst(); + + DVD dvd = cursorToDVD(cursor); + + cursor.close(); + + return dvd; + } + public List getListFromCursor(Cursor cursor) { List dvds = new ArrayList(); diff --git a/src/ryuslash/org/dvdroid/DVDroidActivity.java b/src/ryuslash/org/dvdroid/DVDroidActivity.java index 533bc62..07fe1fb 100644 --- a/src/ryuslash/org/dvdroid/DVDroidActivity.java +++ b/src/ryuslash/org/dvdroid/DVDroidActivity.java @@ -6,17 +6,22 @@ import android.app.ListActivity; import android.content.Context; import android.content.Intent; import android.os.Bundle; +import android.view.ActionMode; import android.view.Menu; import android.view.MenuInflater; import android.view.MenuItem; import android.view.View; import android.widget.ArrayAdapter; import android.widget.EditText; -import android.widget.Toast; +import android.widget.ListView; public class DVDroidActivity extends ListActivity + implements ActionMode.Callback { private DVDDataSource datasource; + private Intent add_dvd; + private int saved_position = 0; + private ActionMode mode; private List getData() { @@ -29,14 +34,40 @@ public class DVDroidActivity extends ListActivity Intent data) { super.onActivityResult(requestCode, resultCode, data); + + if (requestCode == 2) { + add_dvd.putExtra(SQLiteHelper.COLUMN_ID, 0l); + + if (mode != null) + mode.finish(); + } + datasource.open(); + reloadList(); + } - ArrayAdapter adapter = (ArrayAdapter)getListAdapter(); - List dvds = getData(); + @Override + public boolean onActionItemClicked(ActionMode mode, MenuItem item) + { + ArrayAdapter adapter = (ArrayAdapter) getListAdapter(); + DVD dvd = adapter.getItem(saved_position); - adapter.clear(); - adapter.addAll(dvds); - adapter.notifyDataSetChanged(); + switch (item.getItemId()) { + case R.id.main_context_menu_edit: + showEditDVD(dvd.getId()); + return true; + + case R.id.main_context_menu_delete: + datasource.deleteDVD(dvd.getId()); + reloadList(); + + if (mode != null) + mode.finish(); + + return true; + } + + return false; } /** Called when the activity is first created. */ @@ -56,6 +87,16 @@ public class DVDroidActivity extends ListActivity android.R.layout.simple_list_item_1, values); setListAdapter(adapter); + + add_dvd = new Intent(this, AddDVDActivity.class); + } + + @Override + public boolean onCreateActionMode(ActionMode mode, Menu menu) + { + MenuInflater inflater = mode.getMenuInflater(); + inflater.inflate(R.menu.main_context_menu, menu); + return true; } @Override @@ -66,6 +107,20 @@ public class DVDroidActivity extends ListActivity return true; } + @Override + public void onDestroyActionMode(ActionMode mode) + { + saved_position = 0; + } + + @Override + public void onListItemClick(ListView l, View v, int pos, long id) + { + saved_position = pos; + mode = startActionMode(this); + v.setSelected(true); + } + @Override public boolean onOptionsItemSelected(MenuItem item) { @@ -85,6 +140,12 @@ public class DVDroidActivity extends ListActivity super.onPause(); } + @Override + public boolean onPrepareActionMode(ActionMode mode, Menu menu) + { + return false; + } + @Override protected void onResume() { @@ -92,11 +153,26 @@ public class DVDroidActivity extends ListActivity super.onResume(); } + private void reloadList() + { + ArrayAdapter adapter = (ArrayAdapter)getListAdapter(); + List dvds = getData(); + + adapter.clear(); + adapter.addAll(dvds); + adapter.notifyDataSetChanged(); + } + public void showAddDVD() { - Intent intent = new Intent(this, AddDVDActivity.class); - datasource.close(); - startActivityForResult(intent, 1); + startActivityForResult(add_dvd, 1); + } + + public void showEditDVD(long id) + { + datasource.close(); + add_dvd.putExtra(SQLiteHelper.COLUMN_ID, id); + startActivityForResult(add_dvd, 2); } }