使用多个CursorLoader / Content Provider

时间:2016-01-10 19:40:56

标签: android listview android-contentprovider android-cursorloader

我有来自两个不同ContentProviders的数据,我需要在ListView / ListViews中填充单个片段。

选项1:

使用来自两个不同CursorLoader的数据填充Listview。

选项2:

然而,尝试:我可以有两个Listview,它应该填充两个不同的CursorLoaders。但我不认为这是一个更好的选择,因为两个ListView都有自己的滚动区域。

我尝试通过自定义光标适配器根据游标列名称进行充气来尝试第一个选项。

遵循的步骤:

  1. 发起第一台装载机&第二个装载机
  2. 在OnCreateLoader中,基于加载器ID,我返回了不同的CursorLoaders
  3. 在自定义光标适配器的newView方法中,基于列名称我膨胀了不同的布局
  4. 与上面类似,我为bindView方法做了
  5. 在onLoadFinished方法中,我有一个声明: adapterObj.swapCursor(data)
  6. 在onLoaderReset方法中,我没有任何声明。
  7. 当我调试时,我观察到两个游标中的所有项目都能够填充。但我也观察到,一旦第一个装载机完成它就会重置并且只显示第二个装载机中的数据

    可能是什么问题(我怀疑是swapCursor())。我们也欢迎任何其他建议。

    getLoaderManager().initLoader(TRAILER_LOADER, null, this);
    getLoaderManager().initLoader(REVIEW_LOADER, null, this);
    
    @Override
    public Loader<Cursor> onCreateLoader(int id, Bundle args) {
    
        switch (id){
            case TRAILER_LOADER:
                return new CursorLoader(getActivity(),
                        MovieDetailsContract.MovieTrailersEntry.CONTENT_URI,
                        new String[]{
                                MovieDetailsContract.MovieTrailersEntry._ID,
                                MovieDetailsContract.MovieTrailersEntry.COLUMN_KEY,
                                MovieDetailsContract.MovieTrailersEntry.COLUMN_NAME
                        },
                        MovieDetailsContract.MovieTrailersEntry.COLUMN_MOVIE_ID + "=?",
                        new String[]{mMovieId},
                        null);
            case REVIEW_LOADER:
                return new CursorLoader(getActivity(),
                        MovieDetailsContract.MovieReviewsEntry.CONTENT_URI,
                        new String[]{
                                MovieDetailsContract.MovieReviewsEntry._ID,
                                MovieDetailsContract.MovieReviewsEntry.COLUMN_AUTHOR,
                                MovieDetailsContract.MovieReviewsEntry.COLUMN_REVIEW,
                                MovieDetailsContract.MovieReviewsEntry.COLUMN_URL
                        },
                        MovieDetailsContract.MovieReviewsEntry.COLUMN_MOVIE_ID + "=?",
                        new String[]{mMovieId},
                        null);
        }
        return null;
    }
    
    @Override
    public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
    
        switch (loader.getId()){
    
            case TRAILER_LOADER:
                reviewAdapter.swapCursor(data);
                break;
    
            case REVIEW_LOADER:
                reviewAdapter.swapCursor(data);
                break;
        }
    
    }
    

    适配器:

    public class ReviewAdapter extends CursorAdapter {
    
    public ReviewAdapter(Context context, Cursor c, int flags) {
        super(context, c, flags);
    }
    
    @Override
    public View newView(Context context, Cursor cursor, ViewGroup parent) {
        View view;
        if(cursor.getColumnName(1).equals(MovieDetailsContract.MovieReviewsEntry.COLUMN_AUTHOR)) {
            view = LayoutInflater.from(context).inflate(R.layout.review_container, parent, false);
            ReviewViewHolder reviewViewHolder = new ReviewViewHolder(view);
            view.setTag(reviewViewHolder);
        }else{
            view = LayoutInflater.from(context).inflate(R.layout.trailer_container, parent, false);
            TrailerViewHolder viewHolder = new TrailerViewHolder(view);
            view.setTag(viewHolder);
        }
    
        return view;
    }
    
    @Override
    public void bindView(View view, Context context, Cursor cursor) {
    
        if(cursor.getColumnName(1).equals(MovieDetailsContract.MovieReviewsEntry.COLUMN_AUTHOR)) {
            ReviewViewHolder reviewViewHolder = (ReviewViewHolder) view.getTag();
            reviewViewHolder.reviewAuthorTxt.setText(cursor.getString(1));
            reviewViewHolder.reviewContentTxt.setText(cursor.getString(2));
            reviewViewHolder.reviewUrlTxt.setText(cursor.getString(3));
        }else{
            TrailerViewHolder trailerViewHolder = (TrailerViewHolder) view.getTag();
            trailerViewHolder.key = cursor.getString(1);
            trailerViewHolder.trailerNameTxt.setText(cursor.getString(2));
        }
    
    }
    
    public static class ReviewViewHolder {
        public final TextView reviewAuthorTxt;
        public final TextView reviewContentTxt;
        public final TextView reviewUrlTxt;
    
        public ReviewViewHolder(View v){
            reviewAuthorTxt = (TextView) v.findViewById(R.id.reviewAuthorTxt);
            reviewContentTxt = (TextView) v.findViewById(R.id.reviewContentTxt);
            reviewUrlTxt = (TextView) v.findViewById(R.id.reviewUrlTxt);
        }
    }
    
    public static class TrailerViewHolder {
        public final TextView trailerNameTxt;
        public String key;
    
        public TrailerViewHolder(View v){
            trailerNameTxt = (TextView) v.findViewById(R.id.trailerTxt);
        }
    }
    

    }

0 个答案:

没有答案