1

in my app i load the contacts in a listview. i have the listview with image,text and a checkbox and implemented by cursor adapter. my problem is when i scroll the listview up / down its position is changed. that is if choose(tick the checkbox) the position 1,3,4,2 and scroll down and up the ticked position is randomly changed. i have spend more time to find out the solution but i could not get it. please help me.

my code ex for adapter:

    public class ContactsListAdapter extends CursorAdapter {

    ContentResolver cr;
    private final LayoutInflater mInflater;
    private Context mContext;

    @SuppressWarnings("unchecked")
    public ContactsListAdapter(Context context, Cursor c,ArrayList<ContactPhoneInfo> selected) {
        super(context, c, true);
        this.checkedContacts = (ArrayList<ContactPhoneInfo>) selected.clone();
        mInflater = LayoutInflater.from(context);
        this.mContext = context;
    }

    public void bindView(View view,Context context_bind, Cursor cursor)
    {   
        final LinearLayout linear = (LinearLayout) view.findViewById(R.id.contacts_linearLayout1);
        final TextView contactEntryText = (TextView) view.findViewById(R.id.contacts_txtViewContactName);
        final CheckBox chkContact = (CheckBox) view.findViewById(R.id.contacts_chkContact);
        final TextView textCaption = (TextView) view.findViewById(R.id.contacts_layoutCaption);
        final ImageView photoView = (ImageView)view.findViewById(R.id.contacts_imgViewcontact);

        ContentResolver cr = context_bind.getContentResolver();
            .......

        int id = Integer.parseInt(cursor.getString(0));
        final String name = cursor.getString(1);

        contactEntryText.setText(name);
            .......
        view.setClickable(true);
        view.setFocusable(true);

        view.setOnClickListener(new OnClickListener() {
            public void onClick(View v) 
            {
                chkContact.setChecked(!chkContact.isChecked());
            }
        });

        chkContact.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                .buttonView.........
            }
        });
    }

    @Override
    public View newView(Context context, Cursor cursor, ViewGroup parent) {
        final View view = mInflater.inflate(R.layout.contacts_listitem, parent,
                false);
        return view;
    }
}

the calling class coding:

        contactsListAdapter = new ContactsListAdapter(this,app.loadContactCursor(""),checkedContacts);
    setListAdapter(contactsListAdapter);
M.A.Murali
  • 9,988
  • 36
  • 105
  • 182

1 Answers1

1

i have solved my problem by call the setOnCheckedChangeListener method before calling setChecked

    chkContact.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            .buttonView.........
        }
    });

after that

chkContact.setChecked(!chkContact.isChecked());

i refer the following stack over flow question:

Android listview with checkbox problem

Community
  • 1
  • 1
M.A.Murali
  • 9,988
  • 36
  • 105
  • 182