1

在我的应用程序中,我将联系人加载到列表视图中。我有带有图像、文本和复选框的列表视图,并由光标适配器实现。我的问题是当我向上/向下滚动列表视图时,它的位置发生了变化。也就是说,如果选择(勾选复选框)位置 1、3、4、2 并向下和向上滚动,勾选的位置会随机更改。我花了更多时间来找出解决方案,但我无法得到它。请帮我。

我的适配器代码前:

    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;
    }
}

调用类编码:

        contactsListAdapter = new ContactsListAdapter(this,app.loadContactCursor(""),checkedContacts);
    setListAdapter(contactsListAdapter);
4

1 回答 1

1

在调用setChecked之前,我通过调用setOnCheckedChangeListener方法解决了我的问题

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

之后

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

我参考以下堆栈溢出问题:

带有复选框问题的Android列表视图

于 2012-05-21T05:21:50.453 回答