mardi 31 mars 2015

newView(), bindView() scrolling with checkbox

I want to make contact picker and downloaded a sample app from developer.android.com - and now I am trying to add checkbox for the contact list.


However I found that when I try scolling then checkbox set checked randomly.


After a weeks search, I learned that this is because of recycling the view and found many articles solving problems with getView().


However, I cannot use the answers and examples shown for getView(), because my code - originally from developer.android.com - is useing newView(), bindView().


I also found some answers suggesting that adding a new column into the database for boolean numbers about whether it is checked, but I am getting data from Caontacts so I cannot modify the database. So, I cannot use this solution.


Would you please help me? Here is my code - almost the same with example from developer.android.com.



public void bindView(final View view, final Context context, final Cursor cursor) {
final ViewHolder holder = (ViewHolder) view.getTag();
final String photoUri = cursor.getString(ContactsQuery.PHOTO_THUMBNAIL_DATA);

final String displayName = cursor.getString(ContactsQuery.DISPLAY_NAME);
final int startIndex = indexOfSearchQuery(displayName);
if (startIndex == -1) {
holder.text1.setText(displayName);
if (TextUtils.isEmpty(mSearchTerm)) {
holder.text2.setVisibility(View.GONE);
} else {
holder.text2.setVisibility(View.VISIBLE);
}
} else {

final SpannableString highlightedName = new SpannableString(displayName);


holder.text2.setVisibility(View.GONE);
}


final Uri contactUri = Contacts.getLookupUri(
cursor.getLong(ContactsQuery.ID),
cursor.getString(ContactsQuery.LOOKUP_KEY));

final CheckBox checkbox = (CheckBox)view.findViewById(R.id.checkbox);
final int position = cursor.getPosition();

checkbox.setOnCheckedChangeListener(null);
checkbox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener()
{
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
{
//// SHOULD I TO SOMETHING HERE???
}
});




Aucun commentaire:

Enregistrer un commentaire