UPDATE : I am able to replicate this issue every time on my Galaxy S2 (with and without debugging mode), but never on the Emulator!
I am using a context menu on a ListView
(which uses a custom implementation of CursorAdapter
) to let the user select the option to 'Delete all'. When this option is selected, all the items displayed in the list are supposed to get deleted permanently from the database, followed by a call to changeCursor(..)
on the adapter to force the list to get updated.
What is happening, however, is that even after deleting the records from the database and calling changeCursor(..)
, the items are visible. Only the item dividers disappear. Only after I touch somewhere on the list, do these items get cleared.
When the user activates the context menu : https://i.stack.imgur.com/ivFvJ.png
After deletion from database AND calling changeCursor(..)
:
https://i.stack.imgur.com/CX6BM.png
I am having another problem with ListView (Android ListView items overlap while scrolling), and I am using the same ListView, so maybe the problems are related? Is there some step to force the ListView
to redraw after a database update? Or is it not happening automatically due to a mistake in how I've implemented the solution? Thanks in advance!
Here's the XML for the ListView
<ListView
android:id="@+id/all_reminders_list"
android:paddingLeft="4dp"
android:paddingRight="4dp"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:layout_alignParentLeft="true"
android:clickable="true"
android:dividerHeight="1.0sp"
android:animateLayoutChanges="true">
Here's the newView(..)
method of my custom CursorAdapter
public View newView(Context context, Cursor cursor, ViewGroup parent) {
LayoutInflater inflater = LayoutInflater.from(parent.getContext());
View view = inflater.inflate(R.layout.view_list_item, parent, false);
return view;
}
The bindView(..)
method of my CursorAdapter
public void bindView(View view, Context context, Cursor cursor) {
TextView whatTextView = (TextView) view.findViewById(R.id.item_what_text);
whatTextView.setText(cursor.getString(1));
TextView whenTextView = (TextView) view.findViewById(R.id.item_when_text);
if(cursor.getInt(9) != 0) // DONE_FLAG = 1 (completed)
{
//Arrow visibility
ImageView arrow = (ImageView)view.findViewById(R.id.list_item_arrow);
arrow.setVisibility(View.INVISIBLE);
//Text color
whatTextView.setTextColor(Color.LTGRAY);
whenTextView.setTextColor(Color.LTGRAY);
//WHEN text
whenTextView.setText(TimeCalculationHelper.getCompletedTimeString(cursor.getLong(2)));
}
else // DONE_FLAG = 0
{
//Arrow visibility
ImageView arrow = (ImageView)view.findViewById(R.id.list_item_arrow);
arrow.setVisibility(View.VISIBLE);
//Text color
whatTextView.setTextColor(Color.BLACK);
whenTextView.setTextColor(Color.BLACK);
//WHEN text
whenTextView.setText(TimeCalculationHelper.getTimeRemainingString(cursor.getLong(2)));
}
}
Here's my onContextItemSelected(..)
method from the Activity
that contains the ListView
public boolean onContextItemSelected(MenuItem item)
{
AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
ListView allRemindersList = (ListView)findViewById(R.id.all_reminders_list);
switch (item.getItemId()) {
case R.id.delete_item:
//Delete the selected reminder from the database
databaseHelper.deleteRowByID(info.id);
//Refresh the main activity list
((ActiveRemindersAdapter) allRemindersList.getAdapter()).changeCursor(databaseHelper.getAllRemindersForList());
return true;
case R.id.delete_done:
//Delete all reminders with DONE_FLAG = 1
databaseHelper.deleteDoneReminders();
//Refresh the main activity list
((ActiveRemindersAdapter) allRemindersList.getAdapter()).changeCursor(databaseHelper.getAllRemindersForList());
}
return false;
}