提取适配器的GridView的ArrayList值

提取适配器的GridView的ArrayList值

本文介绍了提取适配器的GridView的ArrayList值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以找到大量的关于如何使用ArrayList中的BaseAdapter类填充GridView的例子,但我有困难
传递正确的价值观。这是工作的罚款,而我只是使用光标的位置,但我需要使用
现在ArrayList的。据我知道解决的办法在于正确地填充的getItem和getItemID。哪里的错误 -
可有人帮忙吗?下面有关code:

  @覆盖
保护无效的onCreate(捆绑savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.activity_main);allImages = Images.Media.EXTERNAL_CONTENT_URI;
ArrayList的<串GT; imageCollection =新的ArrayList<串GT;();的String [] =投影{
      MediaStore.Images.Media._ID,
      MediaStore.Images.Media.DATA
};
getCursor = getContentResolver()查询。(
      allImages,
      投影,
      空值,
      空值,
      空值
);
参数:columnIndex = getCursor.getColumnIndexOrThrow(投影[0]); // - 身份证号。
arrayIndex = getCursor.getColumnIndexOrThrow(投影[1]); // - 文件路径。
imageCollection.add(将String.valueOf(参数:columnIndex));GridView控件的GridView =(GridView控件)findViewById(R.id.gridview);
gridView.setAdapter(新ImageAdapter(这一点,imageCollection));
} // --- OnCreate中结束 - 公共类ImageAdapter延伸BaseAdapter {
    私人上下文的背景下;
    私人诠释colCount;
    私人的ArrayList<串GT; tempCollection;    公共ImageAdapter(上下文C,ArrayList的<串GT; anyCollection){
        上下文= C;
        tempCollection = anyCollection;
        colCount = tempCollection.size();
    }
    // ---返回图像的数量---
    公众诠释的getCount(){
        返回colCount;
    }
    // ---返回该项目---
    公共对象的getItem(INT位置){
        返回tempCollection.get(位置);    }
    // ---返回一个项目的ID。
    众长getItemId(INT位置){
        返回的位置;
    }公共查看getView(INT位置,查看convertView,父母的ViewGroup){
    ImageView的ImageView的;
    如果(convertView == NULL){// ---如果不是回收,初始化一些属性 -
        ImageView的=新ImageView的(上下文);
    }其他{
        ImageView的=(ImageView的)convertView;
    }
    字符串imageID =将String.valueOf(位置); // ---失败:始​​终为0,低于造成FileNotFound异常!!!
    imageView.setLayoutParams(新GridView.LayoutParams(85,85));
    imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
    imageView.setPadding(5,5,5,5);
    imageView.setCropToPadding(真);
    imageView.setImageURI(// ---现在,创建每个显示画面 -
        Uri.withAppendedPath(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,imageID)
    );
    返回ImageView的;
} // --- ImageAdapter结束分类 -


解决方案

在活动中,请使用以下code用于保存ImageId到ArrayList

 如果(getCursor!= NULL){
    而(getCursor.moveToNext()){
      参数:columnIndex = getCursor.getColumnIndexOrThrow(投影[0]);
      imageCollection.add(将String.valueOf(getCursor.getLong(参数:columnIndex));
    }
 }

在ImageAdapter,getView方法,使用下面code得到ImageId:

 字符串imageID = tempCollection.get(位置);

I can find plenty of examples of how to use ArrayList in the BaseAdapter class to fill a GridView, but I am having difficultypassing the correct values. This was working fine while I was just using the cursor positions, but I need to use anArrayList now. As far as I know the solution lies in correctly populating the getItem and getItemID. Where is the mistake -can somebody help please? Relevant code below:

@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--
解决方案

In activity, use below code for saving the ImageId to the ArrayList.

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

In ImageAdapter, getView method, get the ImageId by using below code:

String imageID  = tempCollection.get(position);

这篇关于提取适配器的GridView的ArrayList值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-27 13:02