我有一個列表視圖每個Textviews和一個複選框的集合組成的項目。 我存儲複選框的狀態在DB,並從上clickListener。它更新它對於那些visible.By默認控件正常工作的所有複選框的處於選中狀態。複選框刷新問題與自定義適配器
如果有10個項目和屏幕可以容納7,然後當我取消選中第一個和滾動到第10個項目,並再次回到第一個滾動。第一個失去它的狀態(它再次被檢查)。我檢查了數據庫的行狀態,這是正確的反映。但是BindView中的提取總是讓我錯誤的狀態。我無法確定問題出在哪裏。我重視這種審查沿列表適配器...
//名單適配器代碼
公共類ListAdaptor擴展的CursorAdapter {
private LayoutInflater mInflater;
Cursor dataCursor;
Context context;
ListView mLv;
private static final String TAG = "Delete";
public ListAdaptor(Context context, Cursor cursor, ListView lv)
{
super(context, cursor);
this.context = context;
mLv = lv;
mInflater = LayoutInflater.from(context);
}
@Override
public void bindView(View view, Context context, final Cursor cursor) {
// Get the stored tag for the view
CheckBox tmp_Chk = (CheckBox)view.findViewById(R.id.chkbox);
String selText = cursor.getString(11);
// Debug Message
int val = cursor.getPosition();
tmp_Chk.setChecked(false);
SparseBooleanArray sba = mLv.getCheckedItemPositions();
if(sba != null)
if(sba.get(cursor.getPosition()))
tmp_Chk.setChecked(true);
}
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent)
{
View newView = mInflater.inflate(R.layout.listviewlyt, null);
return newView;
}
}
// Item layout
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<CheckBox
android:id="@+id/chkbox"
android:focusable="false"
android:clickable="false"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</CheckBox>
<TextView
android:id="@+id/label"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@+id/label"
android:textSize="18sp"
android:textColor="#000000" >
</TextView>
</LinearLayout>
// List control code in the Main Activity
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
int lv_Pos = ListView.INVALID_POSITION;
CheckBox tmp_Chk = (CheckBox)view.findViewById(R.id.chkbox);
if (lv_Pos != ListView.INVALID_POSITION) {
if(tmp_Chk.isChecked()){
Check_Uncheck(Integer.toString(lv_Pos + 1), 1);
}
else if(!tmp_Chk.isChecked()){
Check_Uncheck(Integer.toString(lv_Pos + 1), 0);
}
}
public void Check_Uncheck(String deleteItem , int select)
{
// Initialize database
DB dbAdapters = DB.getDBAdapterInstance(TabActivity.this);
dbAdapters.openDataBase();
ContentValues cv_InitialValues = new ContentValues();
cv_InitialValues.put("Selection", select);
dbAdapters.b_UpdateRecordInDB("Items", cv_InitialValues, "_id=?", new String[] {deleteItem});
dbAdapters.close();
}
});
//在主要活動
<ListView
android:id="@+id/LV_Instore_CartTab"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:textSize="2px"
android:layout_weight="1"
android:choiceMode="multipleChoice"/>
我這個在這裏「http://stackoverflow.com/a/8410557/525978回答說:」如果這樣做不是幫助..我知道...我可以去你的代碼... – havexz
一個快速的問題,其他值檢索正確嗎?或者它只是提供問題的複選框? – Vamsi
@Vamsi,其他值正確檢索。 – Phoenix