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

时间:2016-11-09 23:45:09

标签: android cursor android-cursorloader

这是我的片段中的代码,目前它没有从数据库加载任何东西,这肯定存在。我按照Edmund的指导修改了它,将mCursor与整个数据库表一起传递给了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
        * http://stackoverflow.com/questions/40517872/how-to-wait-for-cursorloader-to-complete/40530754?noredirect=1#comment68332991_40530754 */

        // 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");
    }

3 个答案:

答案 0 :(得分:1)

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

替换:

getActivity().getLoaderManager()

使用:

getLoaderManager()

并确保您使用支持片段的支持LoaderManager。

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

答案 1 :(得分:0)

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

答案 2 :(得分:0)

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

片段&#34;获胜&#34;不应该是一个问题,因为onLoadFinished()应该在显示片段后加载光标数据。

删除while循环以允许onCreateView()返回 - 我假设所显示的代码位于onCreateView()中。

它也值得更换:

getActivity().getLoaderManager()

使用:

getLoaderManager()

并确保您使用支持片段的支持LoaderManager 另外,将游标而不是null传递给适配器构造函数。