I'm pritty new to Android and I've got a little question. I've got a Problem with my ContextMenu. I have filled my ListView with my database entries, now i wand if i longclick it, that the ContextMenu pops up and there i want to delete or to edit my databaseentries: But I don't get how I can find out what entrie it is.
Here's my Code..
package de.retowaelchli.filterit.stats;
import de.retowaelchli.filterit.R;
import de.retowaelchli.filterit.database.ADFilterDBAdapter;
import android.app.ListActivity;
import android.database.Cursor;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.SimpleCursorAdapter;
public class CreatedADFilters extends ListActivity {
//Variablen deklaration
private ADFilterDBAdapter mDbHelper;
开发者_开发问答
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
//Kontext-Menu registrieren
registerForContextMenu(findViewById(R.layout.list_layout));
//Hier wir die Datenbank aufgerufen
mDbHelper = new ADFilterDBAdapter(this);
mDbHelper.open();
fillData();
}
private void fillData() {
// Get all of the notes from the database and create the item list
Cursor c = mDbHelper.getAllADFilter();
startManagingCursor(c);
String[] from = new String[] { ADFilterDBAdapter.NAME, ADFilterDBAdapter.ROW_ID };
int[] to = new int[] { R.id.label };
// Now create an array adapter and set it to display using our row
SimpleCursorAdapter adname =
new SimpleCursorAdapter(this, R.layout.list_layout, c, from, to);
setListAdapter(adname);
mDbHelper.close();
}
//ContextMenu erstellen und definieren
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
if (v.getId() == R.layout.list_layout) {
getMenuInflater().inflate(R.menu.createdadmenu, menu);
}
super.onCreateContextMenu(menu, v, menuInfo);
}
public boolean onContextItemSelected(MenuItem item) {
final AdapterView.AdapterContextMenuInfo info =
(AdapterView.AdapterContextMenuInfo) item.getMenuInfo();
switch (item.getItemId()) {
case R.id.delete_adfilter: {
deleteAD();
return true;
}
case R.id.edit_adfilter:{
editAD();
return true;
}
return super.onContextItemSelected(item);
}
}
// THIS IS THE PART I DONT KNOW HOW TO REALIZE
private void deleteAD(){
}
private void editAD(){
}
}
Thx for you Help in Advance!
best regards
safari
Your info
local variable in onContextItemSelected()
has an id
data member which contains the _ID
value from your Cursor
. Here is a sample project demonstrating how to use that to delete an item based on a ListView
context menu.
精彩评论