带有光标适配器滚动问题的android ListView

标签 android listview scroll android-listview android-contacts

在我的应用程序中,我将联系人加载到 ListView 中。我有带有图像、文本和复选框的 ListView ,并由游标适配器实现。我的问题是当我向上/向下滚动 ListView 时,它的位置发生了变化。也就是说,如果选择(勾选复选框)位置 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);

最佳答案

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

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

之后

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

我引用了以下堆栈溢出问题:

Android listview with checkbox problem

关于带有光标适配器滚动问题的android ListView ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10636541/

相关文章:

java - 安卓旋转矩阵

android - 在 Fragment 中实现接口(interface)

android - "WHERE IN"使用 greendao Android ORM 的语句

查询 |与滚动条相关的位置div

jquery - 固定滚动 Div

android - 平滑滚动 TextView

java - anr Intent 广播。我的错误是什么?

mysql - 如何根据vb.net中的选择查询设置 ListView 中复选框的值

java - 从 ListView 中获取选定的对象

c# - ListView HasUnEvenRows 不工作