user1591668
user1591668

Reputation: 2893

Android ListView save items onClick in adapter

I have a listview which is in a CustomAdapter, I have the ListView clickable, so when users click a row a little checkmark appears next to it. My problem is that if you scroll down the ListView and then come back up the CheckMark disappears or it forgets what item's where clicked. This is the image enter image description here

As you can see the image correctly appears when an item is clicked but if you scroll down then up again the image is gone. This is what I have in my ListView

  @Override
    public View getView(int position,View convertView,ViewGroup parent) {
        final ViewHolder holder;
     if(convertView==null){
         holder  = new ViewHolder();
         inflater = (LayoutInflater) c.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
         convertView = inflater.from(c).inflate(R.layout.mylists, null);
         // Initialize
         holder.textView=(TextView) convertView.findViewById(R.id.textView);


         binding.list.setOnItemClickListener(new AdapterView.OnItemClickListener() {

             @Override
             public void onItemClick(AdapterView<?> adapterView, View view,int position, long l) {

                 holder.selected_genres = (ImageView) view.findViewById(R.id.selected_genre);
                 holder.selected_genres.setVisibility(View.VISIBLE);
             }

         });


          convertView.setTag(holder);
    }else {
         holder = (ViewHolder) convertView.getTag();
     }
        lists= mylist.toArray(new String[0]);

        holder.text1.setText(lists[position]);

        return convertView;
    }

Upvotes: 0

Views: 180

Answers (1)

Matias Elorriaga
Matias Elorriaga

Reputation: 9150

You need to keep track of which are the "selected" items..

There are a lot of possible solutions to the problem, one is to create a list (same size as the list of items) that start with all falses, and when you select an item in a position (inside onItemClick), set the value in that position to true.. and then when you create the view (inside getView), check for that value and show (or not) the checkmark.

Upvotes: 1

Related Questions