ListView多选模式获取选中的项

android ListView有多选模式

        listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);

这样就ok了,最近在遍历选中的项时碰到了一点问题,在此记录一下!

  SparseBooleanArray booleanArray = listView.getCheckedItemPositions();
                for (int j = 0; j < booleanArray.size(); j++) {
                    int key = booleanArray.keyAt(j);
                    //放入SparseBooleanArray,未必选中
                    if (booleanArray.get(key)) {
                    //这样mAdapter.getItem(key)就是选中的项
          Log.d(TAG, "" + key + ": true");
                    } else {
                    //这里是用户刚开始选中,后取消选中的项
                        Log.d(TAG, "" + key + ": false");
                    }

                }
发布了134 篇原创文章 · 获赞 98 · 访问量 38万+
展开阅读全文

Listview框的

04-22

我创建了一个自定义的 listview,有 Image,Text 和 checkbox。如何检测是否选择或未选择复选框。我使用了 onItemClick() 但是没有被调用。我使用的正确吗? class PInfo { String appname = ""; String pname = ""; Drawable icon; } public class InstalledApps extends Activity{ private static LayoutInflater inflater=null; private ListView listview; private Activity activity; List<PInfo> installedApps = new ArrayList<PInfo>(); @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.applist); activity = this; listview = (ListView)findViewById(R.id.listView1); PackageManager pm = getPackageManager(); List<ApplicationInfo> packages = pm.getInstalledApplications(PackageManager.GET_META_DATA); for(ApplicationInfo app : packages) { PInfo newInfo = new PInfo(); newInfo.appname = (String) app.loadLabel(pm); newInfo.pname = app.packageName; //newInfo.icon = app.icon; //Drawable dd = activity.getResources().getDrawable(app.icon); newInfo.icon = app.loadIcon(getPackageManager()); if((app.flags & ApplicationInfo.FLAG_UPDATED_SYSTEM_APP) == 1) { installedApps.add(newInfo); } else if ((app.flags & ApplicationInfo.FLAG_SYSTEM) == 1) { installedApps.add(newInfo); } else { installedApps.add(newInfo); } } InstalledAppsAdapter iap = new InstalledAppsAdapter(this, installedApps); listview.setAdapter(iap); } } class InstalledAppsAdapter extends BaseAdapter{ private Activity activity; private static LayoutInflater inflater=null; List<PInfo> installedApps = new ArrayList<PInfo>(); ListView listview; public InstalledAppsAdapter(Activity a, List<PInfo> b) { // TODO Auto-generated constructor stub installedApps = b; activity = a; inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE); listview = (ListView)activity.findViewById(R.id.listView1); //Here is where I have put it currently.. listview.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) { // TODO Auto-generated method stub PInfo newInfo = (PInfo) listview.getItemAtPosition(arg2); System.out.println(newInfo.appname); } }); } @Override public int getCount() { // TODO Auto-generated method stub return installedApps.size(); } @Override public Object getItem(int arg0) { // TODO Auto-generated method stub return arg0; } @Override public long getItemId(int arg0) { // TODO Auto-generated method stub return arg0; } @Override public View getView(int arg0, View arg1, ViewGroup arg2) { // TODO Auto-generated method stub View customView = inflater.inflate(R.layout.listrow, arg2,false); ImageView iv = (ImageView)customView.findViewById(R.id.imageView1); TextView tv= (TextView) customView.findViewById(R.id.textView2); CheckBox cb= (CheckBox)customView.findViewById(R.id.checkBox1); PInfo pinfo = installedApps.get(arg0); tv.setText(""+pinfo.appname); iv.setImageDrawable(pinfo.icon); return customView; } } 我要在哪里添加下面的代码(如果这是正确的listner) listview.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) { // TODO Auto-generated method stub PInfo newInfo = (PInfo) listview.getItemAtPosition(arg2); System.out.println(newInfo.appname); } }); 谢谢! 问答

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 大白 设计师: CSDN官方博客

分享到微信朋友圈

×

扫一扫,手机浏览