根据logcat的我的片段似乎已经更新了CursorLoader,但实际上并没有显示,直到我向左和向右滑动(到其他片段)并且最终显示它。我怎样才能立即显示信息?
public class ThreeFragments extends Fragment implements LoaderManager.LoaderCallbacks<Cursor> {//
public static final int RECENT_ARREST_FRAG = 1;
public static final int SEARCH_BY_NAME_FRAG = 2;
public static final int FAVORITE_FRAG = 3;
private RecyclerView recyclerView;
public static final String ARG_FRAG_TYPE = "frag_arg_type";
private static final String TAG = "MainActivity";
private static final int LOADER_ID = 1;
private int thisFragType = 0;
private Activity mActivity;
static boolean recent = false;
static boolean search = false;
static boolean favorite = false;
public static int thisFragTypew = 0;
FavoriteAdapter adapter;
List<TodoItem> items;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
RecyclerView rv = (RecyclerView) inflater.inflate(
R.layout.fragment_prod_list, container, false);
mActivity = getActivity();
thisFragType = getArguments().getInt(ARG_FRAG_TYPE, 0);
thisFragTypew = getArguments().getInt(ARG_FRAG_TYPE, 1);
getLoaderManager().initLoader(LOADER_ID, null, this);
adapter = new FavoriteAdapter(new FavoriteAdapter.ItemClickListenerPublic() {
@Override
public void onItemClickedPublic(TodoItem itemPublic) {
}
});
Log.d(TAG, "The Frag Type is: " + thisFragType);
setupRecyclerView(rv);
return rv;
}
private void setupRecyclerView(RecyclerView recyclerView) {
switch (thisFragType) {
case (RECENT_ARREST_FRAG):
recent = true;
search = false;
favorite = false;
recyclerView.setLayoutManager(new LinearLayoutManager(recyclerView.getContext()));
recyclerView.setAdapter(new SimpleStringRecyclerViewAdapter(getActivity(), getDataListNamesOfStates()));
break;
case (SEARCH_BY_NAME_FRAG):
recyclerView.setLayoutManager(new GridLayoutManager(recyclerView.getContext(), 2));
recyclerView.setAdapter(new SimpleStringRecyclerViewAdapterSearchByName(getActivity(), getDataListNamesOfStates()));
recent = false;
search = true;
favorite = false;
break;
case (FAVORITE_FRAG):
Log.d(TAG,"FAVORITE_FRAG was called");
recyclerView.setLayoutManager(new GridLayoutManager(recyclerView.getContext(), 2));
recyclerView.setAdapter(new SimpleStringRecyclerViewAdapter(getActivity(), getDataListNamesOfFavorites()));
break;
}
}
@Override
public Loader<Cursor> onCreateLoader(int id, Bundle args) {
Log.d(TAG, "Loader Created");
return new CursorLoader(ThreeFragments.this.getActivity(), TodoItemsContentProvider.CONTENT_URI,
null, null, null, null);
}
@Override
public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
Log.d(TAG, "Load Finished");
if(data != null){
Log.d(TAG, "We have " + data.getCount() + " items");
if(data.getCount() == 0){
return;
}
data.moveToFirst();
items= new LinkedList<>();
do{
items.add(TodoItem.buildFromCursor(data));
}while (data.moveToNext());
Log.d(TAG,"items:"+items.get(0).getText()+" " + items.get(0).getTextSubtitle());
}else {
Log.w(TAG, "Data returned null!");
}
}
@Override
public void onLoaderReset(Loader<Cursor> loader) {
Log.d(TAG, "Loader reset");
}