2

What I Have

I have a ListView showing the files on a device. I have a hidden ImageView which I want to make VISIBILE when an user clicks the "Select All" button.

The Issue

When the user clicks the select all button, all the items are not selected. When I scroll down, I find some items have not been selected (there image view is still INVISIBLE).

My Code

selectAll.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {

                List<Item> selectList = dir;

                for (int i = 1; i < list.getChildCount(); i++) {

                    tmpView = list.getChildAt(i);
                    select = (ImageView) tmpView.findViewById(R.id.fileSelect);
                    select.setVisibility(View.VISIBLE);

                }

                for (int i = 1; i < selectList.size(); i++) {

                    Item o = adapter.getItem(i);
                    o.setSelected();
                }
            }
        });

Please suggest how to solve this problem? I know that the ListView items are reused and that is actually causing the problem, but I do not know how to solve this problem.

3
  • can you please post your adapter code ? Commented Jul 27, 2014 at 4:49
  • 1
    try to use flag and when select all button click make flag true and notify your list try to show image on list base on this flag in adapter. Commented Jul 27, 2014 at 4:51
  • Aritra @Haresh is right, you need handle that in getView method in adapter class, post that code if you had a problem Commented Jul 27, 2014 at 5:04

1 Answer 1

1

Your approach is not correct. Cause ListView.getChildCount() only return the amount of visible list items.
I suggest this clean way: try to add selected property into your item, then you can update your view by notify dataset change. It looks like this:

  public class Item{
    boolean selected;
  }

And your adapter should like this:

 public View getView(int position, View convertView, ViewGroup parent) {
     // do something to get view

     if(item.selected){
         yourImageView.setVisibility(View.VISIBLE);
     }else{
          yourImageView.setVisibility(View.INVISIBLE);
     }
 }

Finally, in your select all button click listener:

  List<Item> yourList = getYourListData();
  for (int i = 1; i < yourList.size(); i++) {

       Item o = yourList.get(i);
       o.selected = true;
  }

  // remember notify dataset changed
   adapter.notifyDatasetChanged();
1
  • Thank you, that was a great solution. My code is now working perfectly.
    – Aritra Roy
    Commented Jul 27, 2014 at 10:20

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.