listView.setOnCreateContextMenuListener(new OnCreateContextMenuListener() {
@Override
public void onCreateContextMenu(android.view.ContextMenu menu, View v,
ContextMenuInfo menuInfo) {
menu.setHeaderTitle("确定删除?");
menu.add(0, 0, 0, "OK");
menu.add(0, 1, 0, "Cancel");
menu.add(0,2,0,"update");
}
});
其中,menu.add()的四个参数分别为:
第一个int类型的group ID参数,代表的是组概念,你可以将几个菜单项归为一组,以便更好的以组的方式管理你的菜单按钮。
第二个int类型的item ID参数,代表的是项目编号。这个参数非常重要,一个item ID对应一个menu中的选项。在后面使用菜单的时候,就靠这个item ID来判断你使用的是哪个选项。
第三个int类型的order ID参数,代表的是菜单项的显示顺序。默认是0,表示菜单的显示顺序就是按照add的显示顺序来显示。
第四个String类型的title参数,表示选项中显示的文字。
2.重写activity的onContextItemSelected(MenuItem item)方法@Override
public boolean onContextItemSelected(MenuItem item) {
// TODO Auto-generated method stub
ContextMenuInfo menuInfo = (ContextMenuInfo) item.getMenuInfo();
AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo)item.getMenuInfo();
//info.id获得listview中点击的哪一项
int id=(int)info.id;
switch(item.getItemId())
{
//删除
case 0:
ListView templist = listView;
View mView = templist.getChildAt(id);
TextView textView=(TextView)mView.findViewById(R.id.text1);
String string=textView.getText().toString();
String[] s=string.split(" ");
GoodsInfoEntity goodsInfoEntity=new GoodsInfoEntity();
goodsInfoEntity.setUbid("123456");
goodsInfoEntity.setTakegoods_name(s[0]);
goodsInfoEntity.setTakegoods_telephone(s[2]);
goodsInfoEntity.setCountry(s[4]);
goodsInfoEntity.setArea(s[5]);
goodsInfoEntity.setDetailAddress(s[6]);
myAdapter.remove(id);
goodInfoDao.deleteSimpleData(goodsInfoEntity);
break;
//取消
case 1:
break;
}
return super.onContextItemSelected(item);
}