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'.
This commit is contained in:
Tom Willemsen 2012-08-10 22:03:34 +02:00
parent 2689fd0c50
commit f30b4fcd3a
9 changed files with 165 additions and 17 deletions

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.6 KiB

BIN
res/drawable-hdpi/edit.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.8 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.3 KiB

BIN
res/drawable-mdpi/edit.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.5 KiB

View file

@ -0,0 +1,11 @@
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@+id/main_context_menu_edit"
android:title="@string/main_context_menu_edit"
android:showAsAction="ifRoom|withText"
android:icon="@drawable/edit" />
<item android:id="@+id/main_context_menu_delete"
android:title="@string/main_context_menu_delete"
android:showAsAction="ifRoom|withText"
android:icon="@drawable/delete" />
</menu>

View file

@ -6,4 +6,6 @@
<string name="watched_entry">Watched</string> <string name="watched_entry">Watched</string>
<string name="main_menu_add">Add</string> <string name="main_menu_add">Add</string>
<string name="add_menu_save">Save</string> <string name="add_menu_save">Save</string>
<string name="main_context_menu_edit">Edit</string>
<string name="main_context_menu_delete">Delete</string>
</resources> </resources>

View file

@ -13,8 +13,9 @@ import android.widget.EditText;
public class AddDVDActivity extends Activity public class AddDVDActivity extends Activity
{ {
private DVDDataSource datasource; private DVDDataSource datasource;
private long id;
public void createDVD() public void saveDVD()
{ {
EditText name_entry = (EditText)findViewById(R.id.name_entry); EditText name_entry = (EditText)findViewById(R.id.name_entry);
CheckBox watched_entry = CheckBox watched_entry =
@ -22,10 +23,16 @@ public class AddDVDActivity extends Activity
CheckBox wishlist_entry = CheckBox wishlist_entry =
(CheckBox)findViewById(R.id.wishlist_entry); (CheckBox)findViewById(R.id.wishlist_entry);
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(), datasource.createDVD(name_entry.getText().toString(),
watched_entry.isChecked() ? 1 : 0, watched_entry.isChecked() ? 1 : 0,
wishlist_entry.isChecked() ? 1 : 0); wishlist_entry.isChecked() ? 1 : 0);
} }
}
@Override @Override
public void onCreate(Bundle savedInstanceState) public void onCreate(Bundle savedInstanceState)
@ -35,6 +42,23 @@ public class AddDVDActivity extends Activity
datasource = new DVDDataSource(this); datasource = new DVDDataSource(this);
datasource.open(); 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 @Override
@ -57,7 +81,7 @@ public class AddDVDActivity extends Activity
{ {
switch (item.getItemId()) { switch (item.getItemId()) {
case R.id.add_menu_save: case R.id.add_menu_save:
createDVD(); saveDVD();
finish(); finish();
return true; return true;
} }

View file

@ -33,7 +33,8 @@ public class DVDDataSource
dbHelper.close(); dbHelper.close();
} }
public DVD createDVD(String title, int watched, int wishlist) private ContentValues createValues(String title, int watched,
int wishlist)
{ {
ContentValues values = new ContentValues(); ContentValues values = new ContentValues();
@ -41,6 +42,12 @@ public class DVDDataSource
values.put(SQLiteHelper.COLUMN_WATCHED, watched); values.put(SQLiteHelper.COLUMN_WATCHED, watched);
values.put(SQLiteHelper.COLUMN_WISHLIST, wishlist); 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, long insertId = database.insert(SQLiteHelper.TABLE_DVD, null,
values); values);
Cursor cursor = database.query(SQLiteHelper.TABLE_DVD, Cursor cursor = database.query(SQLiteHelper.TABLE_DVD,
@ -57,14 +64,42 @@ public class DVDDataSource
return newDVD; 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) public void deleteDVD(DVD dvd)
{ {
long id = dvd.getId(); deleteDVD(dvd.getId());
System.out.println("DVD deleted with id: " + id); }
public void deleteDVD(long id)
{
database.delete(SQLiteHelper.TABLE_DVD, database.delete(SQLiteHelper.TABLE_DVD,
SQLiteHelper.COLUMN_ID + " = " + id, null); 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<DVD> getListFromCursor(Cursor cursor) public List<DVD> getListFromCursor(Cursor cursor)
{ {
List<DVD> dvds = new ArrayList<DVD>(); List<DVD> dvds = new ArrayList<DVD>();

View file

@ -6,17 +6,22 @@ import android.app.ListActivity;
import android.content.Context; import android.content.Context;
import android.content.Intent; import android.content.Intent;
import android.os.Bundle; import android.os.Bundle;
import android.view.ActionMode;
import android.view.Menu; import android.view.Menu;
import android.view.MenuInflater; import android.view.MenuInflater;
import android.view.MenuItem; import android.view.MenuItem;
import android.view.View; import android.view.View;
import android.widget.ArrayAdapter; import android.widget.ArrayAdapter;
import android.widget.EditText; import android.widget.EditText;
import android.widget.Toast; import android.widget.ListView;
public class DVDroidActivity extends ListActivity public class DVDroidActivity extends ListActivity
implements ActionMode.Callback
{ {
private DVDDataSource datasource; private DVDDataSource datasource;
private Intent add_dvd;
private int saved_position = 0;
private ActionMode mode;
private List<DVD> getData() private List<DVD> getData()
{ {
@ -29,14 +34,40 @@ public class DVDroidActivity extends ListActivity
Intent data) Intent data)
{ {
super.onActivityResult(requestCode, resultCode, data); super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 2) {
add_dvd.putExtra(SQLiteHelper.COLUMN_ID, 0l);
if (mode != null)
mode.finish();
}
datasource.open(); datasource.open();
reloadList();
}
@Override
public boolean onActionItemClicked(ActionMode mode, MenuItem item)
{
ArrayAdapter<DVD> adapter = (ArrayAdapter<DVD>) getListAdapter(); ArrayAdapter<DVD> adapter = (ArrayAdapter<DVD>) getListAdapter();
List<DVD> dvds = getData(); DVD dvd = adapter.getItem(saved_position);
adapter.clear(); switch (item.getItemId()) {
adapter.addAll(dvds); case R.id.main_context_menu_edit:
adapter.notifyDataSetChanged(); 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. */ /** Called when the activity is first created. */
@ -56,6 +87,16 @@ public class DVDroidActivity extends ListActivity
android.R.layout.simple_list_item_1, android.R.layout.simple_list_item_1,
values); values);
setListAdapter(adapter); 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 @Override
@ -66,6 +107,20 @@ public class DVDroidActivity extends ListActivity
return true; 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 @Override
public boolean onOptionsItemSelected(MenuItem item) public boolean onOptionsItemSelected(MenuItem item)
{ {
@ -85,6 +140,12 @@ public class DVDroidActivity extends ListActivity
super.onPause(); super.onPause();
} }
@Override
public boolean onPrepareActionMode(ActionMode mode, Menu menu)
{
return false;
}
@Override @Override
protected void onResume() protected void onResume()
{ {
@ -92,11 +153,26 @@ public class DVDroidActivity extends ListActivity
super.onResume(); super.onResume();
} }
private void reloadList()
{
ArrayAdapter<DVD> adapter = (ArrayAdapter<DVD>)getListAdapter();
List<DVD> dvds = getData();
adapter.clear();
adapter.addAll(dvds);
adapter.notifyDataSetChanged();
}
public void showAddDVD() public void showAddDVD()
{ {
Intent intent = new Intent(this, AddDVDActivity.class);
datasource.close(); 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);
} }
} }