我有一个类,它在数据库中获得两个字段(名称和图像),并将名称设置为文本视图,图像设置为图像视图 现在我有图像问题,如何通过存储在数据库中的图像名称将图像设置为图像视图? 请帮帮我!
我的源代码:
public class ListMovie extends BaseAdapter {
private Resources res = null;
private Activity activity;
private ArrayList<HashMap<String, String>> data;
private LayoutInflater inflate;
ViewHolder holder;
public ListMovie(Activity a, ArrayList<HashMap<String, String>> d) {
// TODO Auto-generated constructor stub
activity = a;
data = d;
inflate = (LayoutInflater) activity
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return data.size();
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public View getView(int arg0, View view, ViewGroup arg2) {
// TODO Auto-generated method stub
View v1 = view;
if (v1 == null) {
v1 = inflate.inflate(R.layout.item, null);
holder = new ViewHolder();
holder.txt_name = (TextView) v1.findViewById(R.id.item_name);
holder.img = (ImageView) v1.findViewById(R.id.item_img);
v1.setTag(holder);
} else {
holder = (ViewHolder) v1.getTag();
}
HashMap<String, String> song = new HashMap<String, String>();
song = data.get(arg0);
holder.txt_name.setText(song.get("name"));
return v1;
}
static class ViewHolder {
TextView txt_name;
ImageView img;
}
}
答案 0 :(得分:0)
答案 1 :(得分:0)
如果你的图像在/ res文件夹中,你可以在getView中使用它:
int resourceId = res.getIdentifier(name, "drawable",
context.getPackageName());//initialize res and context in adapter's contructor
img.setImageResource(resourceId);
完整代码:
class ListMovie extends BaseAdapter {
private Resources res = null;
private Activity activity;
private ArrayList<HashMap<String, String>> data;
private LayoutInflater inflate;
ViewHolder holder;
public ListMovie(Activity a, ArrayList<HashMap<String, String>> d) {
activity = a;
data = d;
inflate = (LayoutInflater) activity
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
res = a.getResources();
}
@Override
public int getCount() {
return data.size();
}
@Override
public HashMap<String, String> getItem(int position) {
return data.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = convertView;
if (view == null) {
view = inflate.inflate(R.layout.item, null);
holder = new ViewHolder();
holder.txt_name = (TextView) view.findViewById(R.id.item_name);
holder.img = (ImageView) view.findViewById(R.id.item_img);
view.setTag(holder);
} else {
holder = (ViewHolder) view.getTag();
}
HashMap<String, String> song = new HashMap<String, String>();
song = data.get(position);
holder.txt_name.setText(song.get("name"));
int resourceId = res.getIdentifier(image_name, "drawable",
activity.getPackageName());//initialize res and context in adapter's contructor
img.setImageResource(resourceId);
return view;
}
static class ViewHolder {
TextView txt_name;
ImageView img;
}
}
按图片名称更改image_name