长按和鲍勃菜单[关闭]
Posted
技术标签:
【中文标题】长按和鲍勃菜单[关闭]【英文标题】:long click and bob menu [closed] 【发布时间】:2012-11-24 22:32:44 【问题描述】:我不知道如何处理这段代码中的删除和编辑
@Override
public void onCreateContextMenu(ContextMenu menu, View v,ContextMenuInfo menuInfo)
super.onCreateContextMenu(menu, v, menuInfo);
menu.setHeaderTitle("Select");
menu.add(0, R.id.editt, 0, "Edit");
menu.add(0, R.id.delete, 0, "Delete");
menu.add(0, R.id.DeleteAll, 0, "delete all");
public boolean onContextItemSelected(MenuItem item)
AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
switch(item.getItemId())
case R.id.editt:
Intent ed = new Intent(DsTest2Activity.this, Edit.class);
startActivity(ed);
break;
case R.id.delete:
break;
case R.id.DeleteAll:
SQLiteDatabase ld = openOrCreateDatabase(dbName, Context.MODE_PRIVATE, null);
ld.delete(tableName, null, null);
String[] from = "task", "_id";
int[] to = R.id.textview_id,R.id.checkbox_id;
SimpleAdapter sa = new SimpleAdapter(this, lc, R.layout.checkbox, from, to);
ListView li = (ListView) findViewById(R.id.listview_id);
li.setAdapter(sa);
sa.notifyDataSetChanged();
li.setVisibility(ListView.INVISIBLE);
break;
default: return true;
return super.onContextItemSelected(item);
当用户长按时
li.setOnItemLongClickListener(new OnItemLongClickListener()
public boolean onItemLongClick(AdapterView<?> adapter, View view, int position, long id)
registerForContextMenu(li);
return false;
);
【问题讨论】:
【参考方案1】:您必须在活动的onCreate()
方法中调用registerForContextMenu(li)
,如果使用片段,则必须在onActivityCreated()
方法中调用。
您不必拨打li.setOnItemLongClickListener(...)
。请删除它。
另请参阅以下讨论: How do you implement context menu in a ListActivity on android? 和 Context menu in list activity
【讨论】:
以上是关于长按和鲍勃菜单[关闭]的主要内容,如果未能解决你的问题,请参考以下文章
如何让 onTouchEvent、长按和上下文菜单协同工作?