CursorLoader不刷新数据

时间:2014-07-03 23:58:16

标签: java android android-fragments loader android-loadermanager

创建应用程序以通过外部Web服务将电影数据添加到内部数据库并显示电影列表作为ListFragment中的主要活动,另一个活动负责搜索功能。

上面提到的

ListFragment使用LoaderManager从数据库中提取数据。此片段也使用setRetainInstance(true)保留。

我遇到的问题是,当我导航到搜索时,向数据库添加内容,然后按ListFragment返回Back Button,不刷新,即不显示添加的电影。

一些重要的事情:

  • 我没有使用ContentProvider,因此我在onCreateLoaderMethod的数据库中手动检索光标。
  • 我使用支持库 - 我希望所有导入都是正确的(如果有人可以检查)。
  • ListFragment使用自定义CursorAdapter作为其视图。

以下是代码:

package com.mp.movieplanner;

import android.app.Activity;
import android.database.Cursor;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.ListFragment;
import android.support.v4.app.LoaderManager;
import android.support.v4.content.CursorLoader;
import android.support.v4.content.Loader;
import android.util.Log;
import android.view.View;
import android.widget.ListView;

import com.mp.movieplanner.data.DataManager;

public class MovieListFragment extends ListFragment
        implements LoaderManager.LoaderCallbacks<Cursor> {

    public static String TAG = MovieListFragment.class.getSimpleName();

    private OnMovieSelectedListener mCallback;  

    private DataManager mDataManager;

    private MovieCursorAdapter mAdapter;

    public interface OnMovieSelectedListener {
        public void onMovieSelected(int position);      
    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        Log.i(TAG, "onAttach(Activity)");
        mDataManager = ((MoviePlannerApp)activity.getApplication()).getDataManager();
        try {
            mCallback = (OnMovieSelectedListener) activity;
        } catch (ClassCastException e) {
            throw new ClassCastException(activity.toString()
                    + " must implement OnMovieSelectedListener");
        }   
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState); 
        Log.i(TAG, "onCreate(Bundle)");
        final int layout = Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB ? android.R.layout.simple_list_item_activated_1
                : android.R.layout.simple_list_item_1;

        setRetainInstance(true);
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        Log.i(TAG, "onActivityCreated(Bundle)");
        super.onActivityCreated(savedInstanceState);
        mAdapter = new MovieCursorAdapter(getActivity(), null, 0);
        setListAdapter(mAdapter);
        getLoaderManager().initLoader(0, null, this);
    }

    @Override
    public void onStart() {
        super.onStart();
        Log.i(TAG, "onStart()");
        if (getFragmentManager().findFragmentById(R.id.movie_details_fragment) != null) {
            getListView().setChoiceMode(ListView.CHOICE_MODE_SINGLE);
        }
    }

    @Override
    public void onResume() {
        super.onResume();
        Log.i(TAG, "onResume()");
    }

    @Override
    public void onDetach() {
        super.onDetach();
        Log.i(TAG, "onDetach()");
        mCallback = null;
    }

    @Override
    public void onListItemClick(ListView l, View v, int position, long id) {
        // Notify the parent activity of selected item      
        if (mCallback != null) {
            mCallback.onMovieSelected(position);
            // Set the item as checked to be highlighted when in two-pane layout
            getListView().setItemChecked(position, true);
        }
    }


    @Override
    public Loader<Cursor> onCreateLoader(int id, Bundle args) {
        return new CursorLoader(getActivity()) {
            @Override
            public Cursor loadInBackground() {
                return mDataManager.getMovieCursor();
            }
        };
    }

    @Override
    public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
        mAdapter.swapCursor(data);
    }

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

      @Override
      public void onPause() {
         Log.i(TAG, "onPause()");
        super.onPause();
      }

      @Override
      public void onStop() {
        Log.i(TAG, "onStop()");
        super.onStop();
      }

      @Override
      public void onDestroy() {
          super.onDestroy();
          Log.i(TAG, "onDestroy()");
      }
}

2 个答案:

答案 0 :(得分:1)

我建议:

@Override
public void onResume() {
    super.onResume();
    Log.i(TAG, "onResume()");
    getLoaderManager().restartLoader(0, null, this);
}

答案 1 :(得分:0)

@Override
public void onResume() {
    super.onResume();
    Log.i(TAG, "onResume()");
    getLoaderManager().initLoader(0, null, this);
}

试试