0

在此处输入图像描述

我的自定义列表适配器代码...

private final Activity context;

private List<String> list;

public CustomListAdapter(Activity context, List<String> list) {
    super(context, R.layout.list_single);

    this.context = context;
    this.list = list;
}

@Override
public View getView(int position, View view, ViewGroup parent) {
    LayoutInflater inflater = context.getLayoutInflater();
    View rowView = inflater.inflate(R.layout.list_single, null, true);
    TextView txtTitle = (TextView) rowView.findViewById(R.id.txt);


    if (list != null) {
        for (int i = 0; i < list.size(); i++) {

            File imgFile = new File(list.get(i));

            if (imgFile.exists()) {

                ImageView imageView = (ImageView) rowView.findViewById(R.id.img);
                Bitmap myBitmap = BitmapFactory.decodeFile(imgFile.getAbsolutePath());
                imageView.setImageBitmap(myBitmap);
            }
        }
    }

    return rowView;
}

@Override
public int getCount() {
    return list.size();
}

}

我收到了一个 imagePath 列表,例如

d:阵列路径/存储/sdcard/DCIM/Camera/IMG_20170210_163055.jpg

d:阵列路径/存储/sdcard/DCIM/Camera/IMG_20170210_163037.jpg

d:阵列路径/存储/sdcard/DCIM/Camera/IMG_20170217_175804.jpg

d:阵列路径/存储/sdcard/DCIM/Camera/IMG_20170217_175802.jpg

d:阵列路径/存储/sdcard/DCIM/Camera/IMG_20170210_163056.jpg

现在我想使用自定义适配器或使用 ArrayAdapter 将这些路径显示到列表视图。

4

1 回答 1

1

假设您要发送一个 ArrayList 路径 tp 您的自定义适配器。现在在自定义适配器的 getView() 方法中,添加以下内容:

File imgFile = new  File(myList.get(position));

if(imgFile.exists()){

    Bitmap myBitmap = BitmapFactory.decodeFile(imgFile.getAbsolutePath());

    ImageView myImage = (ImageView) convertView.findViewById(R.id.imageviewTest);
    //the imageview of your custom list item

    myImage.setImageBitmap(myBitmap);

}

如果您需要自定义适配器方面的帮助,请告诉我

注意:如果您有很多图像,我建议您使用PicassoGlide之类的库

不要在 getview 中使用 for 循环。每次创建行时都会调用 GetView。

View rowView = inflater.inflate(R.layout.list_single, null, true);
TextView txtTitle = (TextView) rowView.findViewById(R.id.txt);


if (list != null) {

        File imgFile = new File(list.get(position));

        if (imgFile.exists()) {

            ImageView imageView = (ImageView) rowView.findViewById(R.id.img);
            Bitmap myBitmap = BitmapFactory.decodeFile(imgFile.getAbsolutePath());
            imageView.setImageBitmap(myBitmap);
        }
}
return rowView;
于 2017-04-17T06:15:41.807 回答