0

I have a custom listview with textview and imageview and the imageview like checkmark. on OnItemClickListener of Listview when textview is selected, i will make imageview Visibilty to visible to show that is selected.

 stickyList.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
                stickyList.setOnItemClickListener(new OnItemClickListener() {

                    @Override
                    public void onItemClick(AdapterView<?> parent, View view,
                            int position, long id) {
                        // TODO Auto-generated method stub
                        String tag = (String) ((TextView) view
                                .findViewById(R.id.tvtag)).getText();
                         ImageView cb = (ImageView)
                         view.findViewById(R.id.ic_check);
                        if (cb.getVisibility() == View.VISIBLE) {
                            cb.setVisibility(
                                    View.GONE);

                            selectedtags.remove(tag);
                        } else {
                            cb.setVisibility(
                                    View.VISIBLE);
                            selectedtags.add(tag);
                        }
                    }
                });

It working fine untill Listview scrolldown. If I have selected first two postions textview of listview, when I scrolldown listview the visibilty of imageview(cb) is visible for 1st two postions.It means imageview is getting visible for same positions after scrolldown.

GetviewMethod:

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    // TODO Auto-generated method stub
    ViewHolder holder;
    if (convertView == null) {
        holder = new ViewHolder();
        convertView = mInflater.inflate(R.layout.recenttag_list_item,
                parent, false);
        holder.text = (TextView) convertView.findViewById(R.id.tvtag);
        holder.checkmark = (ImageView) convertView
                .findViewById(R.id.ic_check);
        convertView.setTag(holder);
    } else {
        holder = (ViewHolder) convertView.getTag();
    }
    holder.text.setText(data.get(position).mytag);
    return convertView;
}
halfer
  • 19,824
  • 17
  • 99
  • 186
Sree Reddy Menon
  • 1,301
  • 13
  • 23

1 Answers1

4

The reason you are seeing that is because the list item views get recycled (this is what convertView is for). When you scroll the ListView, the previous item that scrolls off screen is passed back to your adapter as the convertView for the next position.

You need to make sure you completely reset the state of your item view in each call to getView() so that changes like this don't leak from one use of the view item to the next. This may mean that you have to always inspect the current "checked" value of that position in getView() to determine if it ought to be visible or not.

EDIT: Perhaps this will help...

@Override
public View getView(int position, View convertView, ViewGroup parent) {

    ViewHolder holder;

    /* Unchanged code omitted */

    holder.text.setText(data.get(position).mytag);

    boolean checkVisible = ???; //Use whatever logic your app has to determine if this position should be checked
    holder.checkmark.setVisibility(checkVisible);  //Always set the visibility to what it should be for this position

    return convertView;
}
devunwired
  • 62,780
  • 12
  • 127
  • 139
  • I didnt get you .can you bit more specific , Please I have added snapchats .Check again! – Sree Reddy Menon Feb 18 '15 at 21:20
  • 1
    Put plainly, when you scroll a `ListView` the items that slide into view are not new views created from scratch, they are the old views that scroll off-screen getting re-used (or recycled). If a `convertView` is handed to you in`getView()`, it's an old view and you need to reset anything not specific to that position (in your case, the image visibility). – devunwired Feb 18 '15 at 21:24
  • Calling NotifiyDataSetChanged will help me? What should i do now ? Can you add some code ? Please. – Sree Reddy Menon Feb 18 '15 at 21:32
  • Not, updating the adapter wouldn't change anything. I tried to spell it out in code. – devunwired Feb 18 '15 at 21:40
  • Thank you so much Dev!! You save my day. I never thought of Getview recycling.I have been following you on google+ . You Rockzzz. – Sree Reddy Menon Feb 18 '15 at 21:56