What I’m trying to do is to put ListView in my app which uses internal SQLite database to get data. When user clicks new data is fetched from DB by executing new query.
I’ve implemented custom view for ListView item which extends LinearLayout and implements Checkable interface. Works.
Got custom adapter based on CursorAdapter. In OnCreate() I’m setting my adapter for ListView with empty cursor.
ListView markersList = (ListView) findViewById(R.id.markersList);
Cursor c = null;
MarkersListAdapter adapter = new MarkersListAdapter(this, c, 0);
markersList.setAdapter(adapter);
Next in AsyncTask I’m querying database in doInBackground() which passes new cursor to onPostExecute() to update ListView. I’m doing this with:
adapter.changeCursor(newCursor);
Then I’m by default checking all items (user just uncheck unwanted items)
for(int i = 0; i < this.markersList.getCount(); i++)
{
this.markersList.setItemChecked(i, true);
}
So far so good. But when I do
int s = this.markersList.getCheckedItemCount();
I’m getting wrong value. I guess it’s associated with old cursor. This only happens when rows count in new cursor is smaller than rows count in new cursor.
Generally, I’m getting something like this
int s = this.markersList.getCheckedItemCount(); // s = 7
int d = this.markersList.getCount(); // d = 5
Is there a solution to this?
The easiest solution is to clear the checked count manually: