简体   繁体   English

如何在listview android java中选择多个项目

[英]How to select multiple items in listview android java

I am trying to select multiple items from a listview and when the items are selected they are each given a unique id. 我试图从列表视图中选择多个项目,当选择项目时,它们每个都被赋予一个唯一的ID。

This is my adapter class so far: 到目前为止,这是我的适配器类:

public class CallDisplay extends ArrayAdapter<String>{
        private final Context context;
        private final List values;

        public CallDisplay(Context context, List values) {
            super(context, R.layout.product_list, values);
            this.context = context;
            this.values = values;
        }




        class DownloadImageTask extends AsyncTask<String, Void, Bitmap> {
            ImageView bmImage;

            public DownloadImageTask(ImageView bmImage) {
                this.bmImage = bmImage;
            }

            @Override
            protected void onPreExecute() {
                // TODO Auto-generated method stub
                super.onPreExecute();
            }

            protected Bitmap doInBackground(String... urls) {
                String urldisplay = urls[0];
                Bitmap mIcon11 = null;
                try {
                  InputStream in = new java.net.URL(urldisplay).openStream();
                  mIcon11 = BitmapFactory.decodeStream(in);
                } catch (Exception e) {
                    Log.e("Error", e.getMessage());
                    e.printStackTrace();
                }
                return mIcon11;
            }

            @Override 
            protected void onPostExecute(Bitmap result) {
                super.onPostExecute(result);
                bmImage.setImageBitmap(result);
            }
          }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            LayoutInflater inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

            View rowView = inflater.inflate(R.layout.product_list, parent, false);
            TextView Name = (TextView) rowView.findViewById(R.id.name);
            TextView Desc = (TextView) rowView.findViewById(R.id.description);
            TextView Price = (TextView)rowView.findViewById(R.id.price);
            TextView ebaylink = (TextView)rowView.findViewById(R.id.elink);
            ImageView imageView = (ImageView) rowView.findViewById(R.id.prod_image);
            TextView id = (TextView)rowView.findViewById(R.id.id);

            id.setText(((HashMap<String, String>)values.get(position)).get("id"));
            Name.setText(((HashMap<String, String>)values.get(position)).get("name"));  
            Desc.setText(((HashMap<String, String>)values.get(position)).get("desc"));
            Price.setText(((HashMap<String, String>)values.get(position)).get("price"));
            ebaylink.setText(((HashMap<String, String>)values.get(position)).get("elink"));
            new DownloadImageTask(imageView).execute(((HashMap<String, String>)values.get(position)).get("photo"));

            //imageView.setImageBitmap("http://www.digitaltrends.com/wp-content/uploads/2013/04/Samsung-Galaxy-6-3-sample-image-3.jpg");

            return rowView;
        }

} }

How can I edit it so that when multiple items are selected they are each given a unique id number? 如何编辑它以便在选择多个项目时为每个项目分配一个唯一的ID号码? Thank you 谢谢

Use ListView.setChoiceMode() and pass it CHOICE_MODE_MULTIPLE . 使用ListView.setChoiceMode()并将其传递给CHOICE_MODE_MULTIPLE Then you can call getCheckedItemIds() . 然后你可以调用getCheckedItemIds()

Your adapter should also implement getItemId(position) . 您的适配器还应实现getItemId(position) If nothing else, you can just return the position argument itself, so at least all the items will have different IDs according to the adapter. 如果没有别的,你可以只返回position参数本身,所以根据适配器,至少所有项目都有不同的ID。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM