下面是扩展BaseAdapter的ImageAdapter。它包含将图像位图设置为ImageView的代码,以及用于将文本设置为TextView的setText方法。但我仍然需要将TextView和ImageView绑定到适配器的父级。每个图像和文本将出现的实际gridView。这该怎么做?我在代码中遗漏了一些东西。
image_item是listView或gridView中单个视图的xml布局设计。它包含一个ImageView和一个TextView。基本上是一个jpeg图像,每个图像下面都会出现一个图像的标题。
public class ImageAdapter extends BaseAdapter {
private Context mContext;
public ImageAdapter(Context c) {
mContext = c;
}
public int getCount() {
return count;
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) mContext
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(R.layout.image_item, null);
} else {
view = convertView;
}
ImageView i = (ImageView) view.findViewById(R.id.adapterImageView);
i.setImageBitmap(bm); // bm = bitmap object
// bm is the bitmap returned by an intent get extras, that code not shown
TextView t = (TextView) view.findViewById(R.id.adapterTitleView);
t.setText("test text");
return view;
}
}