【发布时间】:2011-12-09 00:03:28
【问题描述】:
我有一个列表视图,其中每个项目由一组 Textview 和一个 CheckBox 组成。 我将 复选框的状态存储在数据库中 并从 clickListener 上更新它。它适用于可见的控件。默认情况下,所有复选框都处于选中状态。
如果有 10 个项目并且屏幕可以容纳 7 个,那么当我取消选中第一个项目并滚动到第 10 个项目并再次滚动回第一个项目时。第一个失去了它的状态(它再次被检查)。我检查了数据库的行状态,这反映正确。但是 BindView 中的 fetch 总是让我处于错误的状态。我无法确定问题出在哪里。我已附上列表适配器以供审核...
// 列出适配器代码
公共类 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();
}
});
// 主活动中的列表视图 XML 属性
<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"/>
【问题讨论】:
-
我已经在这里回答了“stackoverflow.com/a/8410557/525978”如果这没有帮助..让我知道...我可以检查您的代码...
-
一个简单的问题,其他值是否正确检索?还是只是复选框给出了问题?
-
@Vamsi,其他值 r 已正确检索。
-
@havexz / Bobakke4 我有一个疑问,BindView 和 Getview 有什么区别。我看到你们的建议已经在 BindView 方法中完成了。感谢您的帮助...
-
两者都可以正常工作。如果使用 CursorAdapter,则为 bindView;如果使用 ListAdapter 或 BaseAdapter,则为 getView
标签: android