1

谁能指出一个使用 CursorLoader 查询 SQLite 数据库并填充 ListFragment的简单示例?下面的代码会编译,但是当我运行它时,LogCat 告诉我“ListFrag”不能转换为 LoaderManager.LoaderCallbacks。如果我将 ListFrag 更改为不是 ListFrag,而只是 ListFrag,我会被告知“无法将 ListFrag 强制转换为 android.v4.support.Fragment”。请注意,由于此论坛帖子中的信息,我的活动扩展了 FragmentActivity 并且我的 Fragment 扩展了 ListFragment 。我一直在努力让它工作一段时间,但我就是不明白。这是包含片段的活动的代码,谢谢:

import android.os.Bundle;
import android.support.v4.app.FragmentActivity

public class MyList extends FragmentActivity {
public void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    setContentView(R.layout.myfragment);
}
}

这是我的片段代码:

import android.app.ListFragment;
import android.app.LoaderManager.LoaderCallbacks;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.content.CursorLoader;
import android.support.v4.content.Loader;
import android.support.v4.widget.CursorAdapter;
import android.support.v4.widget.SimpleCursorAdapter;
import android.support.v4.app.LoaderManager;
import android.view.View;
import android.widget.ListView;

@SuppressWarnings("hiding")
public class ListFrag<Cursor> extends ListFragment implements LoaderManager.LoaderCallbacks<Cursor> {   
    private static final String TABLE_BASEPATH = "MyTable_tbl";
    private static final String AUTHORITY = "SQLData";
    public static final Uri MY_URI = Uri.parse("content://" + AUTHORITY + "/" + TABLE_BASEPATH);
    private static final String[] PROJECTION = new String[] { "_id", "fieldname" };
    private SimpleCursorAdapter mAdapter;
    private static final int LOADER_ID = 0;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @SuppressWarnings("unchecked")
    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        Intent myData = getActivity().getIntent();
        Bundle info = myData.getExtras();


            SimpleCursorAdapter adapter;
            String[] dataColumns = { "fieldname" };
            int[] viewIDs = { R.id.mydetails };
            adapter = new SimpleCursorAdapter(getActivity(), R.layout.mylist, null, dataColumns, viewIDs, CursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER);
            setListAdapter(adapter);
            getLoaderManager().initLoader(0, info, (LoaderCallbacks<Cursor>) this); 

    }

          @Override
    public void onListItemClick(ListView l, View v, int position, long id) {
        String item = (String) getListAdapter().getItem(position);
        DetailFrag frag = (DetailFrag) getFragmentManager().findFragmentById(R.id.frag_detail);
        if (frag != null && frag.isInLayout()) {
            frag.setText(item);
        }
    }

    @SuppressWarnings("unchecked")
    public Loader<Cursor> onCreateLoader(int id, Bundle args) {
        String selection = "level='" + args.getString("Level") + "'";
        return (Loader<Cursor>) new CursorLoader(getActivity(), MY_URI,
                PROJECTION, selection, null, null); 
    }
    public void onLoadFinished(Loader<Cursor> loader, Cursor cursor) {
        switch (loader.getId()) {
          case LOADER_ID:
            mAdapter.swapCursor((android.database.Cursor) cursor);
            break;
        }


    }
    public void onLoaderReset(Loader<Cursor> loader) {
        mAdapter.swapCursor(null);

    }

}

刚刚看到您对 LogCat 的评论。哎呀。这是我的整个 LogCat:

11-05 15:47:27.953: D/dalvikvm(553): Not late-enabling CheckJNI (already on)
11-05 15:47:28.643: I/dalvikvm(553): threadid=3: reacting to signal 3
11-05 15:47:28.683: I/dalvikvm(553): Wrote stack traces to '/data/anr/traces.txt'
11-05 15:47:29.143: I/dalvikvm(553): threadid=3: reacting to signal 3
11-05 15:47:29.153: I/dalvikvm(553): Wrote stack traces to '/data/anr/traces.txt'
11-05 15:47:29.643: I/dalvikvm(553): threadid=3: reacting to signal 3
11-05 15:47:29.653: I/dalvikvm(553): Wrote stack traces to '/data/anr/traces.txt'
11-05 15:47:30.143: I/dalvikvm(553): threadid=3: reacting to signal 3
11-05 15:47:30.153: I/dalvikvm(553): Wrote stack traces to '/data/anr/traces.txt'
11-05 15:47:30.643: I/dalvikvm(553): threadid=3: reacting to signal 3
11-05 15:47:30.693: D/gralloc_goldfish(553): Emulator without GPU emulation detected.
11-05 15:47:30.693: I/dalvikvm(553): Wrote stack traces to '/data/anr/traces.txt'
11-05 15:47:32.843: E/ActivityThread(553): Failed to find provider info for SQLData
11-05 15:47:32.883: D/AndroidRuntime(553): Shutting down VM
11-05 15:47:32.883: W/dalvikvm(553): threadid=1: thread exiting with uncaught exception    (group=0x409c01f8)
11-05 15:47:32.903: E/AndroidRuntime(553): FATAL EXCEPTION: main
11-05 15:47:32.903: E/AndroidRuntime(553): java.lang.NullPointerException
11-05 15:47:32.903: E/AndroidRuntime(553):  at com.MyKnitCards.project.ListFrag.onLoadFinished(ListFrag.java:71)
11-05 15:47:32.903: E/AndroidRuntime(553):  at android.support.v4.app.LoaderManagerImpl$LoaderInfo.callOnLoadFinished(LoaderManager.java:425)
11-05 15:47:32.903: E/AndroidRuntime(553):  at android.support.v4.app.LoaderManagerImpl$LoaderInfo.onLoadComplete(LoaderManager.java:393)
11-05 15:47:32.903: E/AndroidRuntime(553):  at android.support.v4.content.Loader.deliverResult(Loader.java:103)
11-05 15:47:32.903: E/AndroidRuntime(553):  at android.support.v4.content.CursorLoader.deliverResult(CursorLoader.java:81)
11-05 15:47:32.903: E/AndroidRuntime(553):  at android.support.v4.content.CursorLoader.deliverResult(CursorLoader.java:35)
11-05 15:47:32.903: E/AndroidRuntime(553):  at android.support.v4.content.AsyncTaskLoader.dispatchOnLoadComplete(AsyncTaskLoader.java:221)
11-05 15:47:32.903: E/AndroidRuntime(553):  at android.support.v4.content.AsyncTaskLoader$LoadTask.onPostExecute(AsyncTaskLoader.java:61)
11-05 15:47:32.903: E/AndroidRuntime(553):  at android.support.v4.content.ModernAsyncTask.finish(ModernAsyncTask.java:461)
11-05 15:47:32.903: E/AndroidRuntime(553):  at android.support.v4.content.ModernAsyncTask.access$500(ModernAsyncTask.java:47)
11-05 15:47:32.903: E/AndroidRuntime(553):  at android.support.v4.content.ModernAsyncTask$InternalHandler.handleMessage(ModernAsyncTask.java:474)
11-05 15:47:32.903: E/AndroidRuntime(553):  at android.os.Handler.dispatchMessage(Handler.java:99)
11-05 15:47:32.903: E/AndroidRuntime(553):  at android.os.Looper.loop(Looper.java:137)
11-05 15:47:32.903: E/AndroidRuntime(553):  at android.app.ActivityThread.main(ActivityThread.java:4424)
11-05 15:47:32.903: E/AndroidRuntime(553):  at java.lang.reflect.Method.invokeNative(Native Method)
11-05 15:47:32.903: E/AndroidRuntime(553):  at java.lang.reflect.Method.invoke(Method.java:511)
11-05 15:47:32.903: E/AndroidRuntime(553):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
11-05 15:47:32.903: E/AndroidRuntime(553):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
11-05 15:47:32.903: E/AndroidRuntime(553):  at dalvik.system.NativeStart.main(Native Method)
11-05 15:47:33.213: I/dalvikvm(553): threadid=3: reacting to signal 3
11-05 15:47:33.233: I/dalvikvm(553): Wrote stack traces to '/data/anr/traces.txt'
11-05 15:47:33.513: I/dalvikvm(553): threadid=3: reacting to signal 3
11-05 15:47:33.533: I/dalvikvm(553): Wrote stack traces to '/data/anr/traces.txt'
4

2 回答 2

3

首先尝试将这些导入切换到支持库版本。改变:

import android.app.ListFragment;
import android.app.LoaderManager.LoaderCallbacks;

到:

import android.support.v4.app.ListFragment;
import android.support.v4.app.LoaderManager.LoaderCallbacks;

接下来确保您要求支持片段管理器:

DetailFrag frag = (DetailFrag) getActivity().getSupportFragmentManager().findFragmentById(R.id.frag_detail);
//                                              ^^^^^^^

(这可能无法解决所有问题,如果您仍然需要帮助发布这些更改中的 LogCat 错误,以便我们可以确切地看到正在发生的事情。)


加法 mAdapternull,改adaptermAdapter这里:

mAdapter = new SimpleCursorAdapter(getActivity(), R.layout.mylist, null, dataColumns, viewIDs, CursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER);
于 2012-11-05T20:30:05.890 回答
0

你应该检查一下Sherlock,他们提供了不同版本的加载程序来处理片段。你只需要下载他们的包,你就可以在里面找到工作的示例。

Ups,我刚看到这是七个月大的。希望它仍然有用。

于 2013-06-06T03:31:18.723 回答