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

时间:2014-05-12 15:22:46

标签: java android android-listview

我正在尝试从列表视图中选择多个项目,当选择项目时,每个项目都会被赋予唯一的ID。

到目前为止,这是我的适配器类:

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;
        }

}

如何编辑它以便在选择多个项目时,每个项目都有一个唯一的ID号?谢谢

1 个答案:

答案 0 :(得分:2)

使用ListView.setChoiceMode()并传递CHOICE_MODE_MULTIPLE。然后你可以拨打getCheckedItemIds()

您的适配器还应实现getItemId(position)。如果没有别的,你可以只返回position参数本身,所以根据适配器,至少所有项目都有不同的ID。