如何通过 android 数据库中存储的图像名称将图像设置为图像视图?
how to set image to image view by image name that stored in database in android?
我有一个 class 在数据库中获取两个字段(名称和图像)并将名称设置为文本视图并将图像设置为图像视图
现在我的图像有问题,如何通过存储在数据库中的图像名称将图像设置为图像视图?
请帮助我!
我的源代码:
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;
}
}
您可以使用 Android Universal Image Loader library or ion 动态加载图像。
编辑:
试试这个 link。它有一个很好的例子来说明如何通过代码加载图像。
如果你的图片在 /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
我有一个 class 在数据库中获取两个字段(名称和图像)并将名称设置为文本视图并将图像设置为图像视图 现在我的图像有问题,如何通过存储在数据库中的图像名称将图像设置为图像视图? 请帮助我!
我的源代码:
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;
}
}
您可以使用 Android Universal Image Loader library or ion 动态加载图像。
编辑:
试试这个 link。它有一个很好的例子来说明如何通过代码加载图像。
如果你的图片在 /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