如何让数据库显示在 pagerAdapter 片段中?

How to get a database to show in a pagerAdapter Fragment?

这是我的片段中的代码,目前它没有从数据库中加载任何东西,这肯定存在。我一直在按照 Edmund 的指导修改它,将 mCursor 和整个数据库 table 传递给 mCursorAdapter,希望 onLoadFinished 适配器将更新打印数据库到屏幕。但是 onLoadFinished() 没有任何反应:/。求助!

import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.LoaderManager;
import android.support.v4.content.CursorLoader;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;

import com.quotes.obama.obamaquotes.data.PetContract.PetEntry;
import com.quotes.obama.obamaquotes.data.PetDbHelper;

import static android.content.ContentValues.TAG;

//import android.content.CursorLoader;

/**
 * Created by Sam on 30/10/2016.
 */
public class AllQuotesFragment extends Fragment implements
        LoaderManager.LoaderCallbacks<Cursor> { //}, android.support.v4.app.LoaderManager.LoaderCallbacks<Object> {

    /** Identifier for the pet data loader */
    private static final int PET_LOADER = 0;

    /** Adapter for the ListView */
    PetCursorAdapter mCursorAdapter;

    /** so know when we have the database ready for display */
    static Boolean databaseLoaderComplete = false;

    public AllQuotesFragment() {
        // Required empty public constructor
    }

    /**
     * Use this factory method to create a new instance of
     * this fragment using the provided parameters.
     *
     * @return A new instance of fragment GameFragment.
     */
    // TODO: Rename and change types and number of parameters
    public static AllQuotesFragment newInstance() {
        AllQuotesFragment fragment = new AllQuotesFragment();
        //Bundle args = new Bundle();
        //args.putString(TITLE, pageTitle);
        return fragment;
    }
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_quotes_all, container, false);
        // inflate fragment_game into "container",

        // Find the ListView which will be populated with the pet data
        ListView petListView = (ListView) rootView.findViewById(R.id.list);

        // Find and set empty view on the ListView, so that it only shows when the list has 0 items.
        View emptyView = rootView.findViewById(R.id.empty_view);
        petListView.setEmptyView(emptyView);

        /* as per Edmund
        *  */

        // To access our database, we instantiate our subclass of SQLiteOpenHelper
        // and pass the context, which is the current activity.
        PetDbHelper mDbHelper = new PetDbHelper(getContext());

        // Create and/or open a database to read from it
        SQLiteDatabase db = mDbHelper.getReadableDatabase();

        // Perform this raw SQL query "SELECT * FROM pets"
        // to get a Cursor that contains all rows from the pets table.
        Cursor mCursor = db.rawQuery("SELECT * FROM " + PetEntry.TABLE_NAME, null);


        // Setup an Adapter to create a list item for each row of pet data in the Cursor.
        // There is no pet data yet (until the loader finishes) so pass in null for the Cursor.
        mCursorAdapter = new PetCursorAdapter(getContext(), mCursor); // was null
        petListView.setAdapter(mCursorAdapter);

        // TODO Setup the item click listener?
        Log.w(TAG, "about to initLoader");
        // Kick off the loader, need activity to get Context

        getLoaderManager().initLoader(PET_LOADER, null, this);

        Log.w(TAG, "All Quotes UI created, returning ");
        return rootView;

    }
    @Override
    public android.support.v4.content.Loader<Cursor> onCreateLoader(int i, Bundle bundle) {
        // Define a projection that specifies the columns from the table we care about.
        Log.i(TAG, "creating a loader");

        String[] projection = {
                PetEntry._ID,
                PetEntry.COLUMN_PET_NAME,
                PetEntry.COLUMN_PET_BREED };


        // This loader will execute the ContentProvider's query method on a background thread
        return new CursorLoader(getContext(),   // Parent activity context
                PetEntry.CONTENT_URI,   // Provider content URI to query
                projection,             // Columns to include in the resulting Cursor
                null,                   // No selection clause
                null,                   // No selection arguments
                null);                  // Default sort order

    }

    @Override
    public void onLoadFinished(android.support.v4.content.Loader<Cursor> loader, Cursor data) {
        // Update {@link PetCursorAdapter} with this new cursor containing updated pet data
        mCursorAdapter.swapCursor(data);
        Log.i(TAG, "database updated");
    }

    @Override
    public void onLoaderReset(android.support.v4.content.Loader<Cursor> loader) {
        // Callback called when the data needs to be deleted
        mCursorAdapter.swapCursor(null);
        Log.i(TAG, "database reset");
    }

你需要使用自然逻辑,使用方法onLoadFinished()

[已编辑:此答案现在包括在下面的评论中提出的建议,因此无需阅读评论。 ]

片段 "winning" 应该不是问题,因为 onLoadFinished() 应该在片段显示后加载光标数据。

删除 while 循环以允许 onCreateView() return - 我假设显示的代码在 onCreateView() 中。

也值得更换:

getActivity().getLoaderManager()

与:

getLoaderManager()

并确保您使用的是支持 Fragment 的支持 LoaderManager。
此外,将游标而不是 null 传递到适配器构造函数中。

片段 "winning" 应该不是问题,因为 onLoadFinished() 应该在片段显示后加载光标数据。

替换:

getActivity().getLoaderManager()

与:

getLoaderManager()

并确保您使用的是支持 Fragment 的支持 LoaderManager。

此外,将游标而不是 null 传递给适配器构造函数。