Using a Context Menu to delete from a SQLite database in Android
I have created a list view that displays the n开发者_JAVA百科ames and dates of items stored in a SQLite database, now I want to use a Context Menu to modify these items stored in the database such as edit the name, delete, and view.
This is the code for the list view:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.listview);
SQLiteDatabase myDB = null;
myDB = this.openOrCreateDatabase(MY_DB_NAME, MODE_PRIVATE, null);
Cursor cur = myDB.rawQuery("SELECT _id, trackname, tracktime" + " FROM " + MY_DB_TABLE, null);
ListAdapter adapter = new SimpleCursorAdapter(this, R.layout.listview, cur,
new String[] { Constants.TRACK_NAME, Constants.TRACK_TIME}, new int[] { R.id.text1, R.id.text2});
ListView list = (ListView)findViewById(R.id.list);
list.setAdapter(adapter);
registerForContextMenu(list);
}
and the Context Menu...
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
menu.setHeaderTitle("Track Options");
menu.add(0, CHANGE_NAME, 0, "Change name");
menu.add(0, VIEW_TRACK, 0, "View track");
menu.add(0, SEND_TRACK, 0, "Send track");
menu.add(0, DELETE_TRACK, 0, "Delete track");
}
I have used a Switch statement to control the menu items..
public boolean onContextItemSelected(MenuItem item) {
switch (item.getItemId()){
case CHANGE_NAME:
changename();
return true;
case DELETE_TRACK:
deletetrack();
return true;
default:
return super.onContextItemSelected(item);
}
So how would I go ahead and map the deletetrack(); method to find the ID of the track stored in the database to the item that has been selected in the list view?
in your onContextItemSelected()
, do something like this:
@Override
public boolean onContextItemSelected(MenuItem item) {
AdapterView.AdapterContextMenuInfo info =
(AdapterView.AdapterContextMenuInfo)item.getMenuInfo();
Track track = (Track)mAdapter.getItem(info.position);
The key is "position", which will give you the index of the item selected.
I tried this method and many others but none worked for me. This is how I finally got it work:
1. I created a method in my database handler class (your class that extends SQLiteOpenHelper) that retrieves all the ids in my database:
public ArrayList<HashMap<String, String>> selectAllIds() {
try {
ArrayList<HashMap<String, String>> idArrayList = new ArrayList<HashMap<String, String>>();
HashMap<String, String> map;
SQLiteDatabase database = this.getReadableDatabase();
String query = "SELECT * FROM " + DEBTS_TABLE_NAME;
Cursor cursor = database.rawQuery(query, null);
if (cursor != null) {
if (cursor.moveToFirst()) {
do {
map = new HashMap<String, String>();
map.put("id", cursor.getString(0));
idArrayList.add(map);
} while (cursor.moveToNext());
}
}
if (cursor != null) {
cursor.close();
}
database.close();
return idArrayList;
} catch (Exception e) {
return null;
}
}
2. In the class or Fragment that has the onContextItemSelected
method, declare this:
ArrayList<HashMap<String, String>> myIds;
3. Then in my onContextItemSelected
method:
public boolean onContextItemSelected(MenuItem item) {
AdapterView.AdapterContextMenuInfo info =
(AdapterView.AdapterContextMenuInfo) item.getMenuInfo();
myIds = debtDatabaseHandler.selectAllIds();
int id = Integer.parseInt(myIds.get(info.position).get("id"));
the id in my database is of type Integer
that's why I had to parse it.If your id is a String, just do:
String id = myIds.get(info.position).get("id");
4. Finally, pass this id to the call to delete a row in the database:
yourDatabase.deleteMethod(id);
Idea Credit: Android Delete Rows Data in SQLite Database (Android SQLite)
I can't add comment to @synic 's answer, but it's totally works for me. The code below can get the item of Listview:
AdapterView.AdapterContextMenuInfo info =
(AdapterView.AdapterContextMenuInfo)aItem.getMenuInfo();
Then you can do what you want to the item, like getId(), getName(), getDate() or something. Wish you glad.
精彩评论