java - notifyDataSetChanged() 在 ListFragment 中不起作用

标签 java android simplecursoradapter android-listfragment

我在通过数据库查询填充的 ListFragment 中有一个 ListView。当它第一次填充 onCreate() 中的列表时,它加载了所有数据。但是,当我重新查询数据库、将返回值分配给 assignmentsCursor 并调用 notifyDataSetChanged() 时,它不会更新 adapter.mCursor。如果我进入 Eclipse 中的“调试”模式,我可以看到 assignmentsCursor.mCount 已更改,但当我查看 adapter.mCursor.mCount 时,它与以前相同。 (是的,我正在检查调用了notifyDataSetChanged()之后。)

我的代码的相关部分:

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();
}

帮帮我!! ;)

附注如果您需要更多详细信息/代码,请告诉我。不过,我确信数据库正在正确查询。

最佳答案

感谢Deucalion,我确定我错误地认为适配器正在引用指派游标变量,而不是为其自身创建游标的副本。我需要做的只是调用adapter.changeCursor(assignmentsCursor),以便它更新其本地副本。再次感谢丢卡利翁!

关于java - notifyDataSetChanged() 在 ListFragment 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9882886/

相关文章:

java - 小服务程序 : convert an ServletInputStream into a FileInputStream

java - DatePicker:点击 Button 旁边的监听器 react

android - 更新 SimpleCursorAdapter,同时保持 ListView 中的滚动位置

android - ListView 显示排序的数据而不是它在数据库中的存储方式

android - 带有 CursorLoader 和 SimpleCursorAdapter 的 AutoCompleteTextView

java mongodb正则表达式非字母字符

java - Java中如何压缩switch结构?

android - 从通知栏打开时的两个 Activity 实例

java - java中通过mysql请求排序

安卓 : Share session between Webview and httpclient