【发布时间】:2013-02-08 19:50:30
【问题描述】:
更新:我每次都可以在我的 Galaxy S2 上复制这个问题(有和没有调试模式),但从来没有在模拟器上!
我在ListView(它使用CursorAdapter 的自定义实现)上使用上下文菜单让用户选择“全部删除”选项。选择此选项后,列表中显示的所有项目都应该从数据库中永久删除,然后在适配器上调用 changeCursor(..) 以强制更新列表。
然而,发生的情况是,即使在从数据库中删除记录并调用changeCursor(..) 之后,这些项目仍然可见。只有项目分隔符消失。只有在我触摸列表中的某个位置后,这些项目才会被清除。
当用户激活上下文菜单时: http://i.stack.imgur.com/ivFvJ.png
从数据库中删除并调用 changeCursor(..) 后:
http://i.stack.imgur.com/CX6BM.png
我在使用 ListView (Android ListView items overlap while scrolling) 时遇到了另一个问题,而且我使用的是相同的 ListView,所以问题是否相关?是否有一些步骤可以强制ListView 在数据库更新后重绘?还是由于我如何实施解决方案的错误而没有自动发生?提前致谢!
这是ListView 的 XML
<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">
这是我自定义CursorAdapter的newView(..)方法
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;
}
我的CursorAdapter的bindView(..)方法
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)));
}
}
这是我的 onContextItemSelected(..) 方法,来自 Activity,其中包含 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;
}
【问题讨论】:
-
更改光标后尝试添加
((ActiveRemindersAdapter) allRemindersList.getAdapter()).notifyDataSetChanged()调用。 -
Nikita 是对的,最好使用支持库中的 CursorAdapter 并调用 swapCursor() 而不是 changeCursor()
-
@SaifuddinSarker,我也试过
invalidateViews(),但它对我不起作用。