【问题标题】:can't call construtor SimpleCursorAdapter in Fragment不能在 Fragment 中调用构造函数 SimpleCursorAdapter
【发布时间】:2014-07-13 10:28:05
【问题描述】:

为什么我不能在 Fragment 项目中调用 SimpleCursorAdapter,这是我的代码:

    private void fillData() {
    Cursor notesCursor = mDbHelper.fetchAllNotes();
    getActivity().startManagingCursor(notesCursor);

    String[] from = new String[] { NotesDbAdapter.KEY_TITLE ,NotesDbAdapter.KEY_DATE};
    int[] to = new int[] { R.id.text1 ,R.id.date_row};

    SimpleCursorAdapter notes =
            new SimpleCursorAdapter(this, R.layout.note_row, notesCursor, from, to);
        setListAdapter(notes);
    }

谢谢,请帮帮我,因为我正在赶截止日期

【问题讨论】:

  • 错误是什么?首先想到的是您需要将this 替换为getActivity()。片段本身不是有效的上下文,上下文由底层 Activity 提供。
  • @cYrixmorten Yeees 它的工作,但我再次遇到 setListAdapter 错误,错误是“方法 setListAdapter(SimpleCursorAdapter) 未定义为 ReviewFragment 类型”我该怎么办?谢谢之前
  • 嗯,从少量代码中很难分辨,但听起来你是在扩展 Fragment,但实际上是在扩展 ListFragment

标签: android android-fragments fragment android-fragmentactivity simplecursoradapter


【解决方案1】:

SimpleCursorAdapter(Context context, int layout, Cursor c, String[] from, int[] to) 在 lvl 11 中被弃用。使用SimpleCursorAdapter(Context context, int layout, Cursor c, String[] from, int[] to, int flags)。如果您想在 lvl11 下开发应用程序,请使用

android.support.v4.widget.SimpleCursorAdapter

【讨论】:

  • 谢谢,现在我又遇到了 setListAdapter 错误,错误是“对于 ReviewFragment 类型的方法 setListAdapter(SimpleCursorAdapter) 未定义”我该怎么办?谢谢之前
  • 您的片段必须扩展 ListFragment。可能是你的扩展片段。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-09-22
  • 2013-11-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-03-04
  • 2011-05-30
相关资源
最近更新 更多