Android 更改光标后,getCheckedItemCount()返回错误的值
我想做的是将Android 更改光标后,getCheckedItemCount()返回错误的值,android,android-listview,Android,Android Listview,我想做的是将ListView放在我的应用程序中,它使用内部SQLite数据库来获取数据。当用户单击时,通过执行新查询从数据库中获取新数据 我已经为ListView项实现了自定义视图,它扩展了LinearLayout并实现了可检查的接口。工作 基于CursorAdapter获取自定义适配器。在OnCreate()中,我正在使用空光标为ListView设置适配器 ListView markersList = (ListView) findViewById(R.id.markersList); Cu
ListView
放在我的应用程序中,它使用内部SQLite数据库来获取数据。当用户单击时,通过执行新查询从数据库中获取新数据
我已经为ListView
项实现了自定义视图,它扩展了LinearLayout
并实现了可检查的
接口。工作
基于CursorAdapter
获取自定义适配器。在OnCreate()
中,我正在使用空光标为ListView
设置适配器
ListView markersList = (ListView) findViewById(R.id.markersList);
Cursor c = null;
MarkersListAdapter adapter = new MarkersListAdapter(this, c, 0);
markersList.setAdapter(adapter);
接下来在AsyncTask
中,我在doInBackground()
中查询数据库,它将新光标传递到onPostExecute()
以更新ListView
。我是这样做的:
adapter.changeCursor(newCursor);
然后我默认检查所有项目(用户只需取消选中不需要的项目)
我得到了错误的值。我猜它和旧游标有关。只有当新游标中的行数小于新游标中的行数时,才会发生这种情况
一般来说,我会得到这样的东西
int s = this.markersList.getCheckedItemCount(); // s = 7
int d = this.markersList.getCount(); // d = 5
是否有解决方案?最简单的解决方案是手动清除选中的计数:
this.markersList.clearChoices();
for(int i = 0; i < this.markersList.getCount(); i++)
{
this.markersList.setItemChecked(i, true);
}
this.markersList.clearChoices();
对于(int i=0;i
int s = this.markersList.getCheckedItemCount(); // s = 7
int d = this.markersList.getCount(); // d = 5
this.markersList.clearChoices();
for(int i = 0; i < this.markersList.getCount(); i++)
{
this.markersList.setItemChecked(i, true);
}