2015-02-06 49 views
0

我可以找到很多關於如何在BaseAdapter類中使用ArrayList來填充GridView的示例,但我很難通過 傳遞正確的值。這在我剛使用光標位置時工作正常,但我現在需要使用ArrayList。據我所知,解決方案在於正確填充getItem和getItemID。哪裏有錯誤 - 有人可以幫忙嗎?相關代碼如下:從適配器提取ArrayList值到GridView

@Override 
protected void onCreate(Bundle savedInstanceState) { 
super.onCreate(savedInstanceState); 
setContentView(R.layout.activity_main); 

allImages = Images.Media.EXTERNAL_CONTENT_URI; 
ArrayList<String> imageCollection = new ArrayList<String>(); 

String[] projection = { 
     MediaStore.Images.Media._ID, 
     MediaStore.Images.Media.DATA 
}; 
getCursor = getContentResolver().query(
     allImages, 
     projection, 
     null, 
     null, 
     null 
); 
columnIndex = getCursor.getColumnIndexOrThrow(projection[0]); //-- ID number. 
arrayIndex = getCursor.getColumnIndexOrThrow(projection[1]); //-- Filepath. 
imageCollection.add(String.valueOf(columnIndex)); 

GridView gridView = (GridView) findViewById(R.id.gridview); 
gridView.setAdapter(new ImageAdapter(this, imageCollection)); 
} //---End of OnCreate -- 

public class ImageAdapter extends BaseAdapter { 
    private Context context; 
    private int colCount; 
    private ArrayList<String> tempCollection; 

    public ImageAdapter(Context c, ArrayList<String> anyCollection) { 
     context = c; 
     tempCollection = anyCollection; 
     colCount = tempCollection.size(); 
    } 
    //---Returns the number of images--- 
    public int getCount() { 
     return colCount; 
    } 
    //---Returns the item--- 
    public Object getItem(int position) { 
     return tempCollection.get(position); 

    } 
    //---Returns the ID of one item. 
    public long getItemId(int position) { 
     return position; 
    } 

public View getView(int position, View convertView, ViewGroup parent) { 
    ImageView imageView; 
    if (convertView == null) { //---If it's not recycled, initialize some attributes -- 
     imageView = new ImageView(context); 
    } else { 
     imageView = (ImageView) convertView; 
    } 
    String imageID = String.valueOf(position); //--- FAILS: always 0, causing FileNotFound Exception below.!!! 
    imageView.setLayoutParams(new GridView.LayoutParams(85, 85)); 
    imageView.setScaleType(ImageView.ScaleType.CENTER_CROP); 
    imageView.setPadding(5,5,5,5); 
    imageView.setCropToPadding(true); 
    imageView.setImageURI(    //--- Now create each individual display image -- 
     Uri.withAppendedPath(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, imageID) 
    ); 
    return imageView; 
} //---End of ImageAdapter class-- 

回答

0

在活動中,使用下面的代碼將ImageId保存到ArrayList。

if (getCursor != null) { 
    while (getCursor.moveToNext()) { 
     columnIndex = getCursor.getColumnIndexOrThrow(projection[0]); 
     imageCollection.add(String.valueOf(getCursor.getLong(columnIndex)); 
    } 
} 

在ImageAdapter,getView方法,通過使用下面的代碼獲取圖像標識:

String imageID = tempCollection.get(position); 
+0

在原來的職位,我認爲ArrayList中被完全填充_before_試圖提取圖像標識;並且仍然不清楚,所以在GetView類中沒有cursor.moveToPosition指令是正確的。很抱歉,但可以在任何地方找到任何文檔。上面的代碼看起來很有希望,現在就試試吧。 – Archdeacon 2015-02-06 13:32:57