若是使用ListActivity的話
主要使用onCreateContextMenu與onContextItemSelected
@Override
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
menu.add(0, DELETE_ID, 0, R.string.menu_delete);
//menu.add(0, 1, 1, R.string.menu_Edit);
}
@Override
public boolean onContextItemSelected(MenuItem item) {
switch(item.getItemId()) {
case DELETE_ID:
AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
mDbHelper.deleteStockInfo(info.id);
fillData(StoreId);
break;
}
return super.onContextItemSelected(item);
}
若不是使用ListActivity的話
自建一個ListViewMenuListener 去implements OnCreateContextMenuListener
如下
//宣告
private static final int DELETE_ID = 0;
private ListView lv_main;
private ListViewMenuListener l = new ListViewMenuListener();
//在oncreate()裡面 將listview設定長按選單
{
lv_main.setOnCreateContextMenuListener(l);
}
//自建長按選單
private class ListViewMenuListener implements OnCreateContextMenuListener{
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenuInfo menuInfo) {
menu.add(0, DELETE_ID, 0, R.string.menu_delete);
}
}
//通用的長按選單動作。
public boolean onContextItemSelected(MenuItem item) {
switch(item.getItemId()) {
case DELETE_ID:
AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
mDbHelper.deleteOrderItem(info.id);
fillCartData();
break;
}
return super.onContextItemSelected(item);
}
留言列表