listview和ContextMenu总结

本文介绍了如何在Android中使用ListView,并添加ContextMenu。首先,在XML布局文件中定义ListView,然后在代码中设置监听事件,包括点击事件和ContextMenu创建监听。接着,通过数据绑定将数据展示在ListView中。最后,详细阐述了如何处理ContextMenu的选择项,实现特定操作,如拨打电话。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

(1)在XML中添加

<ListView android:id="@+id/vlist" android:layout_width="wrap_content" android:layout_height="wrap_content">

</ListView>

 

(2)增加LIST事件,包括ContextMenu菜单监听:

 

listView = new ListView(this);

 

        //增加相关的ContextMenu菜单。

listView.setOnCreateContextMenuListener(new OnCreateContextMenuListener() {

  public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) { 

   menu.add(0, 1, 0, "打电话"); 

   menu.add(0, 2, 0, "发短信"); 

   menu.add(0, 3, 0, "退出"); 

   } 

}); 

        //添加短按

listView.setOnItemClickListener(new OnItemClickListener() {  

            public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,  

                    long arg3) {  

             //SelectItem=arg2;  

            }  

        });  

// //长按事件,会和ContextMenu菜单监听冲突

// listView.setOnItemLongClickListener(

//    new OnItemLongClickListener(){public boolean onItemLongClick(AdapterView parent, View view, int position,long id) {

//   // Log.e("CallLogActivity", view.toString() + "position=" + position);

//   // CharSequence number = ((TextView) view).getText();

//     SelectItem= position;  

//     return true;} 

//    });

 

setContentView(listView);

 

 

(3)绑定数据:

 

data = m.getData().getString("data"); // 从消息拿出数据

String[] dataMore=data.split(";");

List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();

for(int i=0;i<dataMore.length;i++)

{    

 

String[] inf=dataMore[i].split(",");

Map<String, Object> map = new HashMap<String, Object>();

map.put("title", inf[0]);

map.put("info",   inf[2]);

map.put("img", R.drawable.icon);

list.add(map);

}

 

 

SimpleAdapter adapter = new SimpleAdapter(otherlist.this, list,R.layout.vlist2,

new String[] { "title", "info", "img" },

new int[] { R.id.title, R.id.info, R.id.img });

listView.setAdapter(adapter);

 

 

(4)对ContextMenu的处理:

 

 public boolean onContextItemSelected(MenuItem aItem) { 

     //处理弹出的ContextMenu。。

         ContextMenuInfo menuInfo = (ContextMenuInfo) aItem.getMenuInfo(); 

         /* Switch on the ID of the item, to get what the user selected. */ 

         switch (aItem.getItemId()) { 

              case 1: 

              AdapterContextMenuInfo info = (AdapterContextMenuInfo) aItem.getMenuInfo();//这个很重要,查了半天资料哦

                  if (info == null) {

                      return super.onContextItemSelected(aItem); 

                  }

                   /* Get the selected item out of the Adapter by its position. */ 

              Map<String, Object> map =(Map<String, Object>)listView.getAdapter().getItem(info.position);

                    //关联listview的点击项位置

                   /* Remove it from the list.*/ 

                   String tel=(String)map.get("info");

                   Intent DialIntent = new Intent(Intent.ACTION_DIAL,Uri.parse("tel:"+ tel));

   DialIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK );

   /** Finally start the Activity */

        startActivity(DialIntent);

                   return true; /* true means: "we handled the event". */ 

         } 

         return false; 

    } 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值