diff --git a/res/drawable-hdpi/delete.png b/res/drawable-hdpi/delete.png new file mode 100644 index 0000000..ffd19d9 Binary files /dev/null and b/res/drawable-hdpi/delete.png differ diff --git a/res/drawable-hdpi/edit.png b/res/drawable-hdpi/edit.png new file mode 100644 index 0000000..3eb5190 Binary files /dev/null and b/res/drawable-hdpi/edit.png differ diff --git a/res/drawable-mdpi/delete.png b/res/drawable-mdpi/delete.png new file mode 100644 index 0000000..a8ee5f2 Binary files /dev/null and b/res/drawable-mdpi/delete.png differ diff --git a/res/drawable-mdpi/edit.png b/res/drawable-mdpi/edit.png new file mode 100644 index 0000000..4a28d03 Binary files /dev/null and b/res/drawable-mdpi/edit.png differ 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); } }