我从数据库中获取图像的名称,例如字符串Arraylist中的 R.drawable.abc 为此,我使用以下代码在列表视图中设置图像:
int id = getResources().getIdentifier("com.mypackage:drawable/" + arr_img.get(position),
null, null);
imageview.setImageResource(id);
这里arr_img是ArrayList类型,其中包含所有名称。 但是图片无法看到..请帮忙谢谢。
----- CODE --------
private class MyCustomAdapter extends ArrayAdapter<String> {
private ArrayList<String> myAdptr;
public MyCustomAdapter(Context context, int textViewResourceId,
ArrayList<String> sList) {
super(context, textViewResourceId, sList);
this.myAdptr = new ArrayList<String>();
this.myAdptr.addAll(stg1);
}
private class ViewHolder {
TextView code;
CheckBox name;
ImageView imageview;
}
@Override
public View getView(final int position, View convertView,
ViewGroup parent) {
ViewHolder holder = null;
Log.v("ConvertView", String.valueOf(position));
if (convertView == null) {
LayoutInflater vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = vi.inflate(R.layout.new_search_adptr, null);
holder = new ViewHolder();
holder.code = (TextView) convertView
.findViewById(R.id.adapterText1);
holder.name = (CheckBox) convertView
.findViewById(R.id.checkBox1);
holder.imageview = (ImageView) convertView
.findViewById(R.id.imageView1);
convertView.setTag(holder);
holder.name.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
CheckBox cb = (CheckBox) v;
if (cb.isChecked()) {
// User checked (selected) a car
// *********************************
if (cntr > 1) {
// More than two cars selected
Toast.makeText(getApplicationContext(),
"Please select only two cars!",
Toast.LENGTH_LONG).show();
cb.setChecked(false);
} else {
// car selected
if (cntr == 0) {
c1_make = arr_make.get(position);
c1_model = arr_model.get(position);
c1_ver = arr_ver.get(position);
}
if (cntr == 1) {
c2_make = arr_make.get(position);
c2_model = arr_model.get(position);
c2_ver = arr_ver.get(position);
}
cntr++;
}
} else {
// User Unchecked (de - selected) a car
// *********************************
cntr--;
if (cntr == 0) {
c1_make = null;
c1_model = null;
c1_ver = null;
}
if (cntr == 1) {
c2_make = null;
c2_model = null;
c2_ver = null;
}
}
}
});
} else {
holder = (ViewHolder) convertView.getTag();
}
String state = myAdptr.get(position);
holder.code.setText(stg1.get(position).toString());
for (int m = 0; m < arr_img.size(); m++) {
System.out.println(m+" --- "+arr_img.get(m));
}
int id = getResources()
.getIdentifier(
"com.carlist:drawable/" + arr_img.get(position),
null, null);
holder.imageview.setImageResource(id);
return convertView;
}
}
答案 0 :(得分:0)
只是改变它可能会对你有帮助。
for (int m = 0; m < arr_img.size(); m++) {
int id = getResources()
.getIdentifier(
"com.carlist:drawable/" + arr_img.get(m),
null, null);
holder.imageview.setImageResource(id);
}
答案 1 :(得分:0)
当您获得图像的文件名时,可以使用以下代码部分之一将图像设置为图像视图。
String imageName = "picture"
int resID = getResources().getIdentifier(imageName, "drawable", "package.name");
ImageView image;
imageview.setImageResource(resID );
或者您也可以尝试以下代码
String imageId = "imageId"
int resID = getResources().getIdentifier(imageString , "id", "package.name");
ImageView image = (ImageView) findViewById(resID);