0

我想創建一個GalleryView。 Im從SimpleCursor Adapter的自定義實現中填充GridView。將適配器設置爲gridview後,我什麼都看不到。我只能看到一個白色的屏幕。無法從CustomSimpleCursorAdapter poulate 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佈局 -

<?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" /> 
+0

我會添加日誌語句來查看問題出在哪裏。檢查你的光標是否爲空或空等。 –

+0

不要調用super.bindView。並且不需要檢查pView是否是ImageView的一個實例,因爲您專門爲ImageView充氣 – dymmeh

+0

謝謝你們。但我認爲這不是真正的問題。 – Ahmed

回答

0

所以正確的答案是,

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

YAY !!!! 10分對我來說:D

+0

所以問題不在於我如何選擇價值。問題是我試圖得到的價值。 – Ahmed

0

你沒有得到的圖片ID正確嘗試:

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

你也應該得到一個不同的線程的縮略圖,因爲它的阻止

+0

謝謝肯尼C.但它沒有工作。在這種情況下,ID總是一個 – Ahmed

+1

找到它:D。它的MediaStore.Images.Thumbnail.IMAGE_ID – Ahmed