即使删除所有项目,Android ListView也未清除
更新:无论是否使用调试模式,我都可以在Galaxy S2上复制此问题,但在Emulator上永远不会 我在ListView上使用了一个上下文菜单,它使用了CursorAdapter的自定义实现,让用户选择“全部删除”选项。选择此选项后,列表中显示的所有项目都将从数据库中永久删除,然后调用changeCursor。。在适配器上强制更新列表 然而,即使在从数据库中删除记录并调用changeCursor..之后,这些项仍然可见。只有项目分隔符消失。只有在我触碰清单上的某个地方后,这些项目才会被清除 当用户激活关联菜单时: 从数据库中删除并调用changeCursor..后: 我在ListView中遇到了另一个问题,我使用的是同一个ListView,所以这些问题可能是相关的?在数据库更新之后,是否有强制ListView重画的步骤?还是因为我在实施解决方案时犯了一个错误而没有自动发生?提前谢谢 下面是ListView的XML bindView。。我的游标适配器的方法 这是我的onContextItemSelected。。方法,该方法来自包含ListView的活动 光标更改后,在适配器上调用notifyDataSetChanged以重新加载视图。 如果运行预蜂窝设备,则更好地使用 只是查看了代码。最好使用swapCursor,它会自动为您注册新的内容观察者和notifyDataSetChanged调用 来自CursorAdapter源代码即使删除所有项目,Android ListView也未清除,android,android-listview,Android,Android Listview,更新:无论是否使用调试模式,我都可以在Galaxy S2上复制此问题,但在Emulator上永远不会 我在ListView上使用了一个上下文菜单,它使用了CursorAdapter的自定义实现,让用户选择“全部删除”选项。选择此选项后,列表中显示的所有项目都将从数据库中永久删除,然后调用changeCursor。。在适配器上强制更新列表 然而,即使在从数据库中删除记录并调用changeCursor..之后,这些项仍然可见。只有项目分隔符消失。只有在我触碰清单上的某个地方后,这些项目才会被清除 当
/**
* Change the underlying cursor to a new cursor. If there is an existing cursor it will be
* closed.
*
* @param cursor The new cursor to be used
*/
public void changeCursor(Cursor cursor) {
Cursor old = swapCursor(cursor);
if (old != null) {
old.close();
}
}
/**
* Swap in a new Cursor, returning the old Cursor. Unlike
* {@link #changeCursor(Cursor)}, the returned old Cursor is <em>not</em>
* closed.
*
* @param newCursor The new cursor to be used.
* @return Returns the previously set Cursor, or null if there wasa not one.
* If the given new Cursor is the same instance is the previously set
* Cursor, null is also returned.
*/
public Cursor swapCursor(Cursor newCursor) {
if (newCursor == mCursor) {
return null;
}
Cursor oldCursor = mCursor;
if (oldCursor != null) {
if (mChangeObserver != null) oldCursor.unregisterContentObserver(mChangeObserver);
if (mDataSetObserver != null) oldCursor.unregisterDataSetObserver(mDataSetObserver);
}
mCursor = newCursor;
if (newCursor != null) {
if (mChangeObserver != null) newCursor.registerContentObserver(mChangeObserver);
if (mDataSetObserver != null) newCursor.registerDataSetObserver(mDataSetObserver);
mRowIDColumn = newCursor.getColumnIndexOrThrow("_id");
mDataValid = true;
// notify the observers about the new cursor
notifyDataSetChanged();
} else {
mRowIDColumn = -1;
mDataValid = false;
// notify the observers about the lack of a data set
notifyDataSetInvalidated();
}
return oldCursor;
}
更改光标后,请尝试添加ActiveMembersAdapter AllMembersList.getAdapter.notifyDataSetChanged调用。Nikita是正确的,最好使用支持库中的光标适配器并调用swapCursor,而不是changeCursor@SaifuddinSarker,我也尝试了invalidateViews,但它对我无效。谢谢回复。notifyDataSetChanged的行为与对changeCursor的调用有什么不同吗?将检查并确认它是否解决了我的问题。我只针对IC和更新的设备。更新答案。notifyDataSetChanged是BaseAdapter的一种方法,从中扩展CursorAdapter,强制重新加载视图。谢谢,但我尝试在changeCursor之后调用notifyDataSetChanged。我也试着打电话给swapCursor。我仍然有完全相同的问题。@PritinTyagaraj在更改光标之前,删除后是否执行查询?您确定设置的光标为空吗?注销cursor.getCount并查看包含的行数。是的,我已确保相同的行数。databaseHelper.GetAllMembersForList方法实际上执行一个SELECT查询并为结果返回一个光标。我注意到的另一件奇怪的事情是,我无法在模拟器中复制它,但我可以在Galaxy S2上每次都复制它,无论是否处于调试模式
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;
}
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)));
}
}
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;
}
/**
* Change the underlying cursor to a new cursor. If there is an existing cursor it will be
* closed.
*
* @param cursor The new cursor to be used
*/
public void changeCursor(Cursor cursor) {
Cursor old = swapCursor(cursor);
if (old != null) {
old.close();
}
}
/**
* Swap in a new Cursor, returning the old Cursor. Unlike
* {@link #changeCursor(Cursor)}, the returned old Cursor is <em>not</em>
* closed.
*
* @param newCursor The new cursor to be used.
* @return Returns the previously set Cursor, or null if there wasa not one.
* If the given new Cursor is the same instance is the previously set
* Cursor, null is also returned.
*/
public Cursor swapCursor(Cursor newCursor) {
if (newCursor == mCursor) {
return null;
}
Cursor oldCursor = mCursor;
if (oldCursor != null) {
if (mChangeObserver != null) oldCursor.unregisterContentObserver(mChangeObserver);
if (mDataSetObserver != null) oldCursor.unregisterDataSetObserver(mDataSetObserver);
}
mCursor = newCursor;
if (newCursor != null) {
if (mChangeObserver != null) newCursor.registerContentObserver(mChangeObserver);
if (mDataSetObserver != null) newCursor.registerDataSetObserver(mDataSetObserver);
mRowIDColumn = newCursor.getColumnIndexOrThrow("_id");
mDataValid = true;
// notify the observers about the new cursor
notifyDataSetChanged();
} else {
mRowIDColumn = -1;
mDataValid = false;
// notify the observers about the lack of a data set
notifyDataSetInvalidated();
}
return oldCursor;
}