0

I am trying to select multiple items from a listview and when the items are selected they are each given a unique 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? Thank you

1
  • Do you already have multiple selection working, or do you need to know first how to allow the user to select multiple items, then how to ensure that each item has a unique ID? Commented May 12, 2014 at 15:28

1 Answer 1

2

Use ListView.setChoiceMode() and pass it CHOICE_MODE_MULTIPLE. Then you can call getCheckedItemIds().

Your adapter should also implement 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.

Sign up to request clarification or add additional context in comments.

2 Comments

For some reason when I added: lv.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE); it doesn't even mark/highlight items in list. Why is that?
Based on the code you've provided, I don't have enough information to tell you why this might happen.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.