包含图库图像的ListView自定义适配器 [英] ListView Custom Adapter that includes Images from gallery
问题描述
在创建自定义 ListView
适配器时,通常我从 Array适配器< String>
扩展它,但是我想制作一个 ListView
,其中包含手机图库中的照片。
When creating a custom ListView
adapter, usually I extend it from Array Adapter<String>
but I want to make a ListView
containing photos from the Gallery of the phone.
我设法从Gallery中获得了位图
,它是参考用户选择的图片并将其放入常规图片中的 ImageView
,但是,我真的不知道如何做 ListView
的适配器,以显示用户选择的照片。照片是位图
,有帮助吗?
I managed to get the Bitmap
from the Gallery referring to the picture the user chose and put it in a regular ImageView
but, I don't really know how to do an adapter of a ListView
displaying the photos the user choose. The photos are Bitmap
, any help?
推荐答案
您将执行此操作
首先,您可能想创建一个代表列表中项目的类(也许您想添加一些内容)。更多数据(例如ID或名称),例如:
First you might want to create a class that represents an item in your list (maybe you want to add some more data, like an ID or a name), like:
class ItemInMyList {
Bitmap image;
String title;
Integer id;
}
然后只需创建一个扩展ArrayAdapter的新类:
Then just create a new class that extends ArrayAdapter:
public class MyAdapter extends ArrayAdapter<ItemInMyList> {
private final Context context;
private final List<ItemInMyList> values;
private int layout;
public MyAdapter(Context context, List<ItemInMyList> values, int layout) {
super(context, layout, values);
this.context = context;
this.values = values;
this.layout = layout;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
// When convertView is not null, we can reuse it directly, there is no need
// to reinflate it. We only inflate a new View when the convertView supplied
// by ListView is null.
if (convertView == null) {
convertView = inflater.inflate(layout, null);
// Creates a ViewHolder and store references to the two children views
// we want to bind data to.
holder = new ViewHolder();
holder.name= (TextView) convertView.findViewById(R.id.name);
holder.image = (ImageView) convertView.findViewById(R.id.image);
// Bind the data efficiently with the holder.
convertView.setTag(holder);
} else {
// Get the ViewHolder back to get fast access to the TextView
// and the ImageView.
holder = (ViewHolder) convertView.getTag();
}
try {
holder.text.setText(values.get(position).title);
// Set your image to the ImageView in your list layout
holder.image.setImageBitmap(values.get(position).image);
} catch (NullPointerException e) {
e.printStackTrace();
}
return convertView;
}
static class ViewHolder {
TextView name;
ImageView image;
}
}
现在,您只需要创建一个表示您的ListView中的一行。在此示例中,您可能会向LinearLayout添加ImageView(图像)和TextView(名称)。
Now you just need to create a layout that represents a row in your ListView. In this example you would likely add an ImageView (image) and a TextView (name) to a LinearLayout.
然后实例化适配器时,只需为其指定行的布局即可:
Then when you instanciate the adapter, just give it the layout for the row:
new MyAdapter(this, data, R.layout.rowlayout);
基本上就是这样。
这篇关于包含图库图像的ListView自定义适配器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!