2

我創建的應用程序通過外部Web服務將電影數據添加到內部數據庫並顯示電影列表作爲ListFragment中的主要活動,另一個活動負責搜索功能。CursorLoader不刷新數據

ListFragment上面提到的使用LoaderManager從數據庫提取數據。此片段也使用setRetainInstance(true)保留。

我遇到的問題是,當我導航到搜索時,向數據庫添加內容,然後按Back Button回到ListFragment未刷新它,即不顯示添加的電影。

一些重要的事情:

  • 我不使用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()"); 
     } 
} 

回答

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

嘗試

1

我會建議:

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