listView item添加长按菜单,并对菜单做出响应

1.//添加长按点击    
    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);  
}  

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值