无法从Custom SimpleCursorAdapter填充Gridview

时间:2013-10-18 17:40:53

标签: android gridview android-gridview simplecursoradapter mediastore

我正在尝试创建一个GalleryView。我从SimpleCursor Adapter的Custom实现填充GridView。将适配器设置为gridview后,我没有看到任何内容。我只能看到一个白色的屏幕。

我认为我没有正确地约束视图。 GalleryView是一个使用CustomGalleryAdapter的片段。

public class GalleryView extends BaseFragment implements LoaderManager.LoaderCallbacks<Cursor> {

    private String[] projection = { MediaStore.Images.Thumbnails._ID };
    private CustomGalleryAdapter mCustomGalleryAdapter;
    private String[] selectionArgs = null;
    private String selection = "";
    private GridView mGridView;

    @Override
    public View onCreateView(LayoutInflater pInflater, ViewGroup pContainer, Bundle pSavedInstanceState) {
        mGridView = new GridView(getActivity());
        getLoaderManager().initLoader(0, null, this);

        return mGridView;
    }

    @Override
    public Loader<Cursor> onCreateLoader(int pId, Bundle pArgs) {

        return new android.support.v4.content.CursorLoader(getActivity(), MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI,
                                 null, null, null, null);
    }

    @Override
    public void onLoadFinished(Loader<Cursor> pLoader, Cursor pData) {
        mCustomGalleryAdapter = new CustomGalleryAdapter(getActivity(), R.layout.gallery_item, pData, projection, new int[] { R.id.galleryImageView }, 0);
        mGridView.setAdapter(mCustomGalleryAdapter);
    }

    @Override
    public void onLoaderReset(Loader<Cursor> pLoader) {

    }

}

以下是适配器。

/**
 * @author Syed Ahmed Hussain
 */
public class CustomGalleryAdapter extends SimpleCursorAdapter {

    private Context mContext;
    private LayoutInflater mLayoutInflater;
    private int mLayout;
    private Cursor mCursor;
    ImageView mImageView;
    ContentResolver mContentResolver;

    public CustomGalleryAdapter(Context pContext, int pLayout, Cursor pCursor, String[] pFrom, int[] pTo, int pFlags) {
        super(pContext, pLayout, pCursor, pFrom, pTo, pFlags);
        mContext = pContext;
        mLayoutInflater = LayoutInflater.from(mContext);
        mLayout = pLayout;
        mCursor = pCursor;
        mContentResolver = mContext.getContentResolver();
    }

    @Override
    public View newView(Context pContext, Cursor pCursor, ViewGroup pParent) {
        return mLayoutInflater.inflate(mLayout, null);
    }

    @Override
    public void bindView(View pView, Context pContext, Cursor pCursor) {
        super.bindView(pView, pContext, pCursor);
        if (pView instanceof ImageView) {
            mImageView = (ImageView) pView;
        }

        int id = pCursor.getInt(0);

        mImageView.setImageBitmap(MediaStore.Images.Thumbnails.getThumbnail(mContentResolver, id, MediaStore.Images.Thumbnails.MINI_KIND, new Options()));

    }
}

GalleryItem Layout -

<?xml version="1.0" encoding="utf-8"?>
<ImageView
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:id="@+id/galleryImageView"
  android:layout_width="match_parent"
  android:layout_height="match_parent" />

2 个答案:

答案 0 :(得分:0)

您没有正确获取图片ID,请尝试:

int index = pCursor.getColumnIndex(MediaStore.Images.Media._ID);
long id = pCursor.getLong(index);

你也应该在不同的线程上获取缩略图,因为它正在阻止

答案 1 :(得分:0)

所以正确的答案是,

pCursor.getColumnIndex(MediaStore.Images.Thumbnail.IMAGE_ID);

YAY !!!!对我来说10分:D