1
在我的應用程序我加載列表視圖中的聯繫人。我有圖像,文本和複選框的列表視圖,並由遊標適配器實現。我的問題是當我向上/向下滾動列表視圖時,它的位置發生了變化。也就是說如果選擇(勾選複選框)位置1,3,4,2並向下滾動並向上滾動的位置是隨機更改的。我花了更多時間找出解決方案,但我無法得到它。請幫幫我。Android的列表視圖與光標適配器滾動問題
的適配器我的代碼例如:
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);
你爲什麼要使用'CursorAdapter'而不是'ArrayAdapter'? –
,因爲我正在提取聯繫人並直接在listview中加載光標。我不使用數組列表。有沒有辦法通過arrayadapter加載遊標? –
是的,您只需訪問ArrayAdapter的getView方法中的數據。視圖被回收以提高性能,所以請記住這一點。 –