[英]notifyDataSetChanged() not working in ListFragment
I have a ListView
in a ListFragment
that is populated via a database query. 我在通过数据库查询填充的
ListFragment
中有一个ListView
。 It loads all the data just fine when it first populates the list in onCreate()
. 首次在
onCreate()
填充列表时,它会加载所有数据。 But when I requery the database, assign the return value to assignmentsCursor
, and call notifyDataSetChanged()
, it doesn't update adapter.mCursor
. 但是,当我重新查询数据库,将返回值
assignmentsCursor
给assignmentsCursor
并调用notifyDataSetChanged()
,它不会更新adapter.mCursor
。 If I go into the Debugging mode in Eclipse I can see that assignmentsCursor.mCount
has changed, but when I look at adapter.mCursor.mCount
, it's the same as before. 如果我在Eclipse中进入Debugging模式,我可以看到
assignmentsCursor.mCount
已更改,但是当我查看adapter.mCursor.mCount
,它与以前相同。 (Yes, I'm checking after notifyDataSetChanged() has been called.) (是的,我检查notifyDataSetChanged()被调用后 。)
The relevant parts of my code: 我的代码的相关部分:
SimpleCursorAdapter adapter;
Cursor assignmentsCursor;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (savedInstanceState != null) {
course = savedInstanceState.getShort(Values.ASSIGNMENT_KEY_COURSE);
}
setRetainInstance(true);
// Create and array to specify the fields we want
String[] from = new String[] { Values.KEY_TITLE, Values.ASSIGNMENT_KEY_COURSE };
// and an array of the fields we want to bind in the view
int[] to = new int[] { R.id.assignment_list_title, R.id.assignment_list_course };
// Need to give assignmentsCursor a value -- null will make it not work
updateAdapter();
adapter = new SimpleCursorAdapter(context, R.layout.assignment_list_item, assignmentsCursor, from, to);
setListAdapter(adapter);
}
public void onResume() {
super.onResume();
updateAdapter();
refresh();
}
/**
* Updates the content in the adapter.
*/
public void updateAdapter() {
SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(context);
boolean showingCompleted = sharedPrefs.getBoolean(Values.ASSIGNMENT_KEY_SHOWING_COMPLETED, false);
if (course < 0) // Showing assignments from all courses
if (showingCompleted)
assignmentsCursor = DbUtils.fetchAllAssignments(context, Values.ASSIGNMENT_LIST_FETCH, null, true);
else
assignmentsCursor = DbUtils.fetchIncompleteAssignments(context, Values.ASSIGNMENT_LIST_FETCH, null, true);
else // Showing assignments from specified course
if (showingCompleted)
assignmentsCursor = DbUtils.fetchAllAssignments(context, Values.ASSIGNMENT_LIST_FETCH, course, true);
else
assignmentsCursor = DbUtils.fetchIncompleteAssignments(context, Values.ASSIGNMENT_LIST_FETCH, course, true);
}
private void refresh() {
updateAdapter();
adapter.notifyDataSetChanged();
}
Help me!! 帮我!! ;)
;)
PS If you need any more details/code, just let me know. PS如果您需要更多详细信息/代码,请告诉我。 I am positive that the database is querying correctly though.
我肯定数据库可以正确查询。
Thanks to Deucalion, I have determined that I incorrectly assumed that the adapter was referencing the assignmentsCursor variable as opposed to creating a copy of the Cursor for itself. 多亏了Deucalion,我才确定我错误地认为适配器正在引用assignmentsCursor变量,而不是为其本身创建Cursor副本。 What I needed to do was just call
adapter.changeCursor(assignmentsCursor)
so that it updated its local copy. 我需要做的只是调用
adapter.changeCursor(assignmentsCursor)
以便它更新其本地副本。 Thanks again Deucalion! 再次感谢Deucalion!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.