游标加载器没有刷新游标

时间:2016-02-16 08:21:31

标签: java android android-fragments android-cursoradapter android-cursorloader

您好我正在制作电影应用。在main fragment我正在显示grid imagesdetail fragment我正在显示movie detailscheckbox位于detail fragment,用户可以选择main fragment最喜欢的电影。

现在,当用户选择一部电影作为收藏时,我更新了数据库,但当我返回notification uri并检查其收藏价值为1的电影时,数据未更新。

我已在content provider中设置了restart loader,当我使用init loader代替init loader时,它可以正常工作,但我希望它能够与public class MoviesFragment extends Fragment implements MovieTask.OnTaskCompleted,LoaderManager.LoaderCallbacks<Cursor> { @Bind(R.id.recycler_view)RecyclerView mRecyclerView; @Bind(R.id.progress_bar)ProgressBar progressBar; public static String API_KEY; private CardViewAdapter cursorAdapter; private static final int LOADER_ID = 1; private ArrayList<Movie> mMovieArrayList = new ArrayList<>(); public MoviesFragment() { // Required empty public constructor } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { setHasOptionsMenu(true); View rootView = inflater.inflate(R.layout.fragment_movies, container, false); ButterKnife.bind(this, rootView); progressBar.setVisibility(View.VISIBLE); if(Utility.checkNetwork(getActivity())){ cursorAdapter = new CardViewAdapter(getActivity(), null, 1); } //Initialising recycler view mRecyclerView.setLayoutManager(new GridLayoutManager(getActivity(),2)); API_KEY = getString(R.string.API_KEY); updateData(); return rootView; } //To check setting value and call AsyncTask to get json data private void updateData(){ //checking preference value String order = Utility.getPreferenceValue(getActivity()); if(Utility.checkNetwork(getActivity())){ if(order.equalsIgnoreCase(getString(R.string.pref_fav))){ getLoaderManager().initLoader(LOADER_ID, null, this); }else { new MovieTask(getActivity(), order, this).execute(); } }else { withoutNet(); } } //Tasks to do without internet private void withoutNet(){ getLoaderManager().initLoader(LOADER_ID, null, this); Toast.makeText(getActivity(), getString(R.string.network), Toast.LENGTH_SHORT).show(); Toast.makeText(getActivity(), getString(R.string.fav_toast), Toast.LENGTH_SHORT).show(); } //Getting data from database private void getDataFromDb(Cursor cursor) { mMovieArrayList.clear(); mMovieArrayList = Utility.getDataFromCursor(cursor); if (mMovieArrayList.size() == 0) { Toast.makeText(getActivity(), getString(R.string.no_selection), Toast.LENGTH_LONG).show(); } setCursorAdapter(cursor); } //Setting adapter to grid view and passing details to detail activity public void setAdapter(){ RecyclerViewAdapter adapter = new RecyclerViewAdapter(getActivity()); mRecyclerView.setAdapter(adapter); progressBar.setVisibility(View.GONE); adapter.setList(mMovieArrayList); mRecyclerView.addOnItemTouchListener(new RecyclerItemClickListener(getActivity(), new RecyclerItemClickListener.OnItemClickListener() { @Override public void onItemClick(View view, int position) { startActivityFromMain(mMovieArrayList.get(position)); } })); } //Setting cursor adapter public void setCursorAdapter(Cursor cursor){ progressBar.setVisibility(View.GONE); cursorAdapter.swapCursor(cursor); mRecyclerView.setAdapter(cursorAdapter); mRecyclerView.addOnItemTouchListener(new RecyclerItemClickListener(getActivity(), new RecyclerItemClickListener.OnItemClickListener() { @Override public void onItemClick(View view, int position) { startActivityFromMain(mMovieArrayList.get(position)); } })); } //To start detail activity public void startActivityFromMain(Movie movie){ Intent intent = new Intent(getActivity(), DetailActivity.class); intent.putExtra(Utility.MOVIE, movie); if(Utility.getPreferenceValue(getActivity()).equalsIgnoreCase(getString(R.string.pref_fav))) { startActivityForResult(intent, Utility.FAV_CODE); }else { startActivityForResult(intent, Utility.OTHERS_CODE); } } @Override public void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); if (requestCode==Utility.SETTINGS_CODE||requestCode==Utility.FAV_CODE){ updateData(); } } @Override public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) { super.onCreateOptionsMenu(menu, inflater); inflater.inflate(R.menu.menu_main, menu); } @Override public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()){ case R.id.action_settings: Intent intent = new Intent(getActivity(), SettingsActivity.class); startActivityForResult(intent, Utility.SETTINGS_CODE); return true; } return onOptionsItemSelected(item); } //Interface method @Override public void onTaskCompleted(ArrayList<Movie> movie) { mMovieArrayList = movie; setAdapter(); } @Override public Loader<Cursor> onCreateLoader(int id, Bundle args) { return new CursorLoader(getActivity(), MovieContract.FavoriteMovies.CONTENT_URI, null, MovieContract.FavoriteMovies.COLUMN_FAVORITES + "=?", new String[]{Integer.toString(1)}, null); } @Override public void onLoadFinished(Loader<Cursor> loader, Cursor data) { getDataFromDb(data); } @Override public void onLoaderReset(Loader<Cursor> loader) {} } 一起使用它会在某个地方快速有效。

MainFragment.java

loader

getDataFromDb完成后,我调用setCursorAdapter方法调用cursor方法,该方法为recycler view交换detail fragment。问题是当我从<{1}}加载程序onFinished()方法未被调用。

的ContentProvider:

public class MovieProvider extends ContentProvider{
//constants for uri matcher
private static final int TABLE = 1;
private static final int TABLE_ID = 2;
private static final UriMatcher mUriMatcher;
private SQLiteOpenHelper mOpenHelper;

static {
    mUriMatcher = new UriMatcher(UriMatcher.NO_MATCH);
    mUriMatcher.addURI(MovieContract.AUTHORITY, FavoriteMovies.TABLE_NAME, TABLE );
    mUriMatcher.addURI(MovieContract.AUTHORITY, FavoriteMovies.TABLE_NAME + "/#", TABLE_ID);

}

@Override
public boolean onCreate() {
    mOpenHelper = new MovieDatabase(getContext());
    return true;
}

@Override
public Cursor query(@NonNull Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) {
    SQLiteDatabase db = mOpenHelper.getReadableDatabase();
    Cursor cursor;
    switch (mUriMatcher.match(uri)){
        case TABLE:{
            cursor = db.query(FavoriteMovies.TABLE_NAME,projection,selection,selectionArgs,null,null,sortOrder);
            break;
        }
        default:
            throw new IllegalArgumentException("Unsupported Uri" + uri);
    }
    if(getContext()!=null)
        getContext().getContentResolver().notifyChange(uri, null);
    return cursor;
}

@Override
public String getType(@NonNull Uri uri) {
    switch (mUriMatcher.match(uri)){
        case TABLE:
            return FavoriteMovies.CONTENT_TYPE;
        case TABLE_ID:
            return FavoriteMovies.CONTENT_ITEM_TYPE;
        default:
            throw new IllegalArgumentException("Unsupported Uri" + uri);
    }
}

@Override
public Uri insert(@NonNull Uri uri, ContentValues values) {
    SQLiteDatabase db = mOpenHelper.getWritableDatabase();
    Uri uri1=null;
    switch (mUriMatcher.match(uri)){
        case TABLE:{
            long id = db.insert(FavoriteMovies.TABLE_NAME, null, values);
            if(id!=-1){
                uri1 = MovieContract.getUriForId(id);
            }
            break;
        }
        default:{
            throw new IllegalArgumentException("Unsupported Uri For Insertion " + uri);
        }
    }
    if(getContext()!=null)
        getContext().getContentResolver().notifyChange(uri, null);
    return uri1;
}

@Override
public int delete(@NonNull Uri uri, String selection, String[] selectionArgs) {
    SQLiteDatabase db = mOpenHelper.getWritableDatabase();
    int delete;
    switch (mUriMatcher.match(uri)){
        case TABLE_ID:{
            delete = db.delete(FavoriteMovies.TABLE_NAME, selection, selectionArgs);
            break;
        }
        case TABLE:{
            delete = db.delete(FavoriteMovies.TABLE_NAME, selection, selectionArgs);
            break;
        }
        default:
            throw new IllegalArgumentException("Unsupported Uri For Deletion " + uri);
    }
    if(getContext()!=null&&delete!=0)
        getContext().getContentResolver().notifyChange(uri, null);
    return delete;
}

@Override
public int update(@NonNull Uri uri, ContentValues values, String selection, String[] selectionArgs) {
    SQLiteDatabase db = mOpenHelper.getWritableDatabase();
    int update;
    switch (mUriMatcher.match(uri)){
        case TABLE_ID:{
            update = db.update(FavoriteMovies.TABLE_NAME, values, selection, selectionArgs);
            break;
        }
        default:
            throw new IllegalArgumentException("Unsupported Uri For Updating " + uri);
    }
    if(getContext()!=null)
        getContext().getContentResolver().notifyChange(uri, null);
    return update;
}
}

0 个答案:

没有答案