Android ListView两种长按弹出菜单方式

/**
* @author xianglong guo 2012-05-17 22:15 
* 知识点1:ListView item:两种长按弹出菜单方式
* 知识点2:ListView SimpleAdapter的使用
*  知识点 3:在java代码中创建一个ListView
*/ 

-----------------------------------------------------
Activity
?
代码片段,双击复制

packageorg.gxl.com;
 
publicclass ListOnLongClickActivity extendsActivity {
        privateLinearLayout myListViewlayout;
        privateListView mListView;
        SimpleAdapter adapter;
        publicint MID;
 
        // 创建一个List对象,用来存放列表项的每一行map信息
        List<Map<String, Object>> list = newArrayList<Map<String, Object>>();
 
        @Override
        publicvoid onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.main);
 
                myListViewlayout = (LinearLayout) findViewById(R.id.myListViewlayout);
                mListView = newListView(this);
                // 创建布局参数
                LinearLayout.LayoutParams listviewParams = newLinearLayout.LayoutParams(
                                LinearLayout.LayoutParams.FILL_PARENT,
                                LinearLayout.LayoutParams.FILL_PARENT);
                // 当滑动列表上,默认显示的黑色
                mListView.setCacheColorHint(Color.WHITE);
                // 将列表添加到流式布局myListViewlayout中
                myListViewlayout.addView(mListView, listviewParams);
 
                FillListData();
 
                // 列表现的单机事件
                mListView.setOnItemClickListener(newOnItemClickListener() {
 
                        @Override
                        publicvoid onItemClick(AdapterView<?> arg0, View arg1,
                                        intposition, longid) {
                                /*
                                 * 点击列表项时触发onItemClick方法,四个参数含义分别为
                                 * arg0:发生单击事件的AdapterView
                                 * arg1:AdapterView中被点击的View 
                                 * position:当前点击的行在adapter的下标
                                 * id:当前点击的行的id
                                 */
                                Toast.makeText(ListOnLongClickActivity.this,
                                                "您选择的是" + list.get(position).get("name").toString(),
                                                Toast.LENGTH_SHORT).show();
                        }
                });
 
                /**
                 * Item 长按方式弹出菜单多选方式1
                 * Item 长按方式弹出菜单多选方式2
                 * ItemOnLongClick1()与ItemOnLongClick2()不共存,按实际需要选择
                 */
        //        ItemOnLongClick1();
                ItemOnLongClick2();
        }
 
        // 填充ListView资源
        privatevoid FillListData() {
 
                adapter = newSimpleAdapter(ListOnLongClickActivity.this,
                                getListData(), R.layout.listviewrow, newString[] { "name",
                                                "price"}, newint[] { R.id.tv_name, R.id.tv_price });
                mListView.setAdapter(adapter);
        }
 
        privateList<Map<String, Object>> getListData() {
 
                Map<String, Object> _map = newHashMap<String, Object>();
                _map.put("name","MOTO ME865");
                _map.put("price","2350元");
                list.add(_map);
 
                _map = newHashMap<String, Object>();
                _map.put("name","HTC G18");
                _map.put("price","3340元");
                list.add(_map);
 
                _map = newHashMap<String, Object>();
                _map.put("name","MOTO XT928");
                _map.put("price","5450元");
                list.add(_map);
 
                _map = newHashMap<String, Object>();
                _map.put("name","iPhone 4S");
                _map.put("price","4650元");
                list.add(_map);
 
                _map = newHashMap<String, Object>();
                _map.put("name","MOTO ME525");
                _map.put("price","1345元");
                list.add(_map);
                returnlist;
 
        }
 
        privatevoid ItemOnLongClick1() {
//注:setOnCreateContextMenuListener是与下面onContextItemSelected配套使用的
                mListView
                                .setOnCreateContextMenuListener(newOnCreateContextMenuListener() {
 
                                        publicvoid onCreateContextMenu(ContextMenu menu, View v,
                                                        ContextMenuInfo menuInfo) {
                                                menu.add(0,0,0,"添加");
                                                menu.add(0,1,0,"删除");
                                                menu.add(0,2,0,"删除ALL");
 
                                        }
                                });
        }
 
        // 长按菜单响应函数
        publicboolean onContextItemSelected(MenuItem item) {
 
                AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) item
                                .getMenuInfo();
                MID = (int) info.id;// 这里的info.id对应的就是数据库中_id的值
 
                switch(item.getItemId()) {
                case0:
                        // 添加操作
                        Toast.makeText(ListOnLongClickActivity.this,
                                        "添加",
                                        Toast.LENGTH_SHORT).show();
                        break;
 
                case1:
                        // 删除操作
                        break;
 
                case2:
                        // 删除ALL操作
                        break;
 
                default:
                        break;
                }
 
                returnsuper.onContextItemSelected(item);
 
        }
 
        privatevoid ItemOnLongClick2() {
                mListView.setOnItemLongClickListener(newOnItemLongClickListener() {
 
                        @Override
                        publicboolean onItemLongClick(AdapterView<?> arg0, View arg1,
                                        finalint arg2, longarg3) {
                                list.remove(arg2);
                                newAlertDialog.Builder(ListOnLongClickActivity.this)
                                                .setTitle("对Item进行操作")
                                                .setItems(R.array.arrcontent,
                                                                newDialogInterface.OnClickListener() {
                                                                        publicvoid onClick(DialogInterface dialog,
                                                                                       intwhich) {
                                                                                String[] PK = getResources()
                                                                                                .getStringArray(
                                                                                                               R.array.arrcontent);
                                                                                Toast.makeText(
                                                                                                ListOnLongClickActivity.this,
                                                                                                PK[which], Toast.LENGTH_LONG)
                                                                                                .show();
                                                                                if(PK[which].equals("删除")) {
                                                                                       // 按照这种方式做删除操作,这个if内的代码有bug,实际代码中按需操作
                                                                                       list.remove(arg2);
                                                                                       adapter = (SimpleAdapter) mListView
                                                                                                        .getAdapter();
                                                                                       if(!adapter.isEmpty()) {
                                                                                                adapter.notifyDataSetChanged();// 实现数据的实时刷新
                                                                                       }
                                                                                }
                                                                        }
                                                                })
                                                .setNegativeButton("取消",
                                                                newDialogInterface.OnClickListener() {
                                                                        publicvoid onClick(DialogInterface dialog,
                                                                                       intwhich) {
                                                                                // TODO Auto-generated method stub
 
                                                                        }
                                                                }).show();
                                returntrue;
                        }
                });
 
        }
}



-----------
listviewrow.xml
?
代码片段,双击复制
<?xml version="1.0"encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   android:background="@drawable/listviewbg"
   android:orientation="vertical">
 
   <TextView
        android:id="@+id/tv_name"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:textColor="@android:color/black"/>
 
   <TextView
        android:id="@+id/tv_price"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:textColor="@android:color/black"/>
 
</LinearLayout>


-----------------
有图有真相:

工程代码:


 该贴已经同步到 qiannuo0317的微博

11.png (19.63 KB, 下载次数: 7)

22.png (21.91 KB, 下载次数: 6)

Android ListView两种长按弹出菜单方式_第1张图片

你可能感兴趣的:(android,object,ListView,layout,dialog,encoding)