gpt4 book ai didi

Android:SimpleCursorAdapter 用法

转载 作者:行者123 更新时间:2023-11-29 22:29:16 26 4
gpt4 key购买 nike

我有一个关于 SimpleCursorAdapter 的恼人问题。我的程序有 ListView 和 ListActivity。每行都有自己的布局:

<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="wrap_content" android:layout_width="fill_parent"
android:orientation="horizontal" android:weightSum="1.0">
<TableRow>
<TextView android:id="@+id/task_time"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:textSize="24sp" android:text="Time">
</TextView>
<LinearLayout android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="fill_parent">
<TextView android:id="@+id/task_name"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:textSize="20sp" android:text="Name">
</TextView>
<TextView android:id="@+id/task_categoty"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:text="Category" android:textSize="12sp">
</TextView>
</LinearLayout>
<TextView android:id="@+id/task_state"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:text="State" android:textSize="12sp">
</TextView>
<CheckBox android:id="@+id/task_enabled"
android:layout_width="wrap_content"
android:layout_height="wrap_content" android:focusable="false">
</CheckBox>

</TableRow>

任务存储在 SQLite 数据库中。我有 DAO 对象(单例)来访问数据库。任务道:

    public void updateEnabled(int id, boolean enabled){
SQLiteDatabase db = dbHelper.getWritableDatabase();
ContentValues cv = new ContentValues();
cv.put(ENABLED_COLUMN, enabled==true?1:0);
Log.i(TAG, "update to " + cv.get(ENABLED_COLUMN) );
try{
db.beginTransaction();
db.update(TASK_TABLE, cv, ID_COLUMN+"=?", new String[]{id+""});
db.setTransactionSuccessful();
} catch (SQLException e) {
Log.i(TAG, "edit task failed!");
} finally {
db.endTransaction();
if (db != null)
db.close();
}
}

和 ListActivity 的 Cursor 方法:

    public Cursor getTasks(){
SQLiteDatabase db = dbHelper.getReadableDatabase();
return db.query(TASK_TABLE, COLUMNS, null, null, null, null, NAME_COLUMN);
}

我像这样扩展了 SimpleCursorAdapter (TaskDbAdapter):

    @Override
public View getView(int position, View convertView, ViewGroup parent) {
if(convertView==null){
convertView = inflater.inflate(R.layout.task_list_row, null);
}
Cursor c = getCursor();
c.moveToPosition(position);
Log.i(TAG, "getView " + position + " = " + c.getInt(enabledIdx));
enabled.setTag(c.getInt(c.getColumnIndex(BaseColumns._ID)));
enabled.setChecked(c.getInt(enabledIdx)>0?true:false);
enabled.setOnClickListener(this);
return convertView;
}
@Override
public void onClick(View v) {
CheckBox box = (CheckBox) v;
Integer id = (Integer)box.getTag();
TaskDao.getInstance(context).updateEnabled(id.intValue(), box.isChecked());
}

最后,我在我的主要 ListActivity 中使用了上述所有内容

    private void refreshList(){
c = TaskDao.getInstance(this).getTasks();
startManagingCursor(c);
adapter = new TaskDbAdapter(this, R.layout.task_list_row, c, new String[]{TaskDao.ENABLED_COLUMN}, new int[]{R.id.task_enabled});
setListAdapter(adapter);
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.task);
getListView().setItemsCanFocus(false);
getListView().setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
getListView().setVerticalScrollBarEnabled(true);
registerForContextMenu(getListView());
getListView().setOnCreateContextMenuListener(this);
refreshList();
}


@Override
protected void onResume() {
super.onResume();
refreshList();
}
@Override
protected void onPause() {
super.onPause();

}

一切正常。但是 CheckBoxes 失去了它们的状态。例如,我检查我的第一列并向下滚动列表。在我出版前的痕迹中,我有:

getView 0 = 0
getView 2 = 0
getView 3 = 0

然后

uptate to 1

然后(当我向上滚动到第一个元素时)

getView 0 = 0
getView 2 = 0
getView 3 = 0

我试图在我的 TaskDbAdapter onClick 方法中创建 getCursor().requery();。但是后来我在列表中没有看到任何项目!并且由于游标管理而出现异常(连接被 android 关闭)。当我在 refreshList() 方法中编写 startManagingCursor(c); 时,选中和取消选中方法不起作用。请帮忙!

最佳答案

我没有阅读你所有的来源,所以我的建议可能是完全错误的,但我会试一试。

查看 BaseAdapter 的文档类(class)。

public void notifyDataSetChanged () 

可以完成这项工作。

您也可以为此注册 Observer...

public void registerDataSetObserver (DataSetObserver observer)

关于Android:SimpleCursorAdapter 用法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4834142/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com