Skip to content Skip to sidebar Skip to footer

Checkedtextview Disappears When Scroll In Listview

I have custom ListView. I'm using array adapter to fill my ListViews rows. It worked perfectly. When I select the ListView row, my checkedtextview become visible. After scrolling m

Solution 1:

You should use use the "view holder" design pattern as described by developer.android.com and explained with details on this post.

The most important here is:

  1. Create the ViewHolder class;
  2. Store the ViewHolder instance in a tag if the convertView is null;
  3. Restore the ViewHolder instance from a tag if it is not.

That is my Adapter's code:

package br.com.inovant.pm;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.*;

import java.util.List;

publicclassContactsAdapterextendsArrayAdapter<CheckedContact>{

    publicContactsAdapter(Context context, List<CheckedContact> contacts) {
        super(context, 0, contacts);
    }

    staticclassViewHolder {
        protected CheckedTextView checkedTextView;
    }

    @Overridepublic View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder;
        if (convertView == null) {
            convertView = LayoutInflater.from(getContext()).inflate(android.R.layout.select_dialog_multichoice, parent, false);
            viewHolder = newViewHolder();
            viewHolder.checkedTextView = (CheckedTextView) convertView.findViewById(android.R.id.text1);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }

        finalCheckedContactcheckedContact= getItem(position);

        finalCheckedTextViewcheckedTextView= viewHolder.checkedTextView;
        checkedTextView.setText(checkedContact.getContact().getDisplayName());
        checkedTextView.setChecked(checkedContact.isChecked());
        return convertView;
    }

    @OverridepubliclonggetItemId(int position) {
        return getItem(position).getContact().getId().hashCode();
    }

    @OverridepublicbooleanhasStableIds() {
        returntrue;
    }
}

Solution 2:

I was got same problem so, i had used following methods in my custom adapter, i got solution.

Add following methods inside your ListViewItemsAdapter:

@OverridepublicintgetCount() {
        return alUpgradeTour.size();
    }

    @OverridepublicintgetItemViewType(int position) {
        return position;
    }

    @OverridepublicintgetViewTypeCount() {

        return getCount();
    }

Try it, you will also get solution.

Solution 3:

Android's ListView recycles all its individual views, that is, when a list item is scrolled off of the screen its view is recycled to be used by the one that just scrolled into the visible screen area. Now when an older list item is scrolled back into view it gets some other recycled view to draw itself in. Because of this the checkmark is lost or any data that was changed / updated, for that matter.

The solution to this is you save your changes in the list item, in your case - ListViewItems. Say you have a flag boolean isChecked = false in the class definition of ListViewItems and in the onItemClick where you call the setChecked method, set isChecked = true This will help you persist the check mark.

Hope it helps.

Post a Comment for "Checkedtextview Disappears When Scroll In Listview"