【问题标题】:How to set Image local storage path to ListView in Android如何在Android中将Image本地存储路径设置为ListView
【发布时间】:2017-09-12 17:57:44
【问题描述】:

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

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 将这些路径显示到列表视图。

【问题讨论】:

标签: android listview android-studio custom-lists android-storage


【解决方案1】:

假设您正在向您的自定义适配器发送路径的 ArrayList。现在在自定义适配器的 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;

【讨论】:

  • 它对我有用。至少现在我可以看到图像,但我在 listView 中连续获取最后一张图像。
  • @Niprup 谢谢现在它完美了,我使用外观来填充集合列表到列表视图,这是错误的逻辑,你可以指定为什么这是错误的概念。
  • 每个适配器都有一个 getView() 方法。当 listview 需要填充行时,它会调用 getView 方法。在您的情况下,getView 方法将 list_single 布局膨胀到视图中,设置 textview 和 imageview 并返回膨胀的视图。现在 listview 会将这个膨胀的视图作为一行插入。您所做的是,您在 getView 中循环,因此对于每一行,for 循环都被迭代,并且列表中的最后一个路径被设置为图像。
  • @NipurIyer 在我的情况下需要更多帮助,如何从列表视图或网格视图中选择单个图像(在第一个选项卡中存在相同图像)并将该图像显示到另一个片段。
猜你喜欢
  • 1970-01-01
  • 2018-02-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-06-28
  • 1970-01-01
相关资源
最近更新 更多