如何在listview android中动态显示图像?

时间:2013-01-12 02:44:22

标签: android image listview

(我有res / drawable中的图像,数据库中的图像名称) 我已经完成了从res / drawable到listview显示图像,但图像显示相同的图片.. 我希望显示其他人的差异图像......

如何从drawable文件夹中更改图像ID(使用setImageResource())。我的drawable文件夹包含多个图像,我想使用已存储的数据库中的名称更改这些图像..

这是我的教程代码......

  

//来自数据库的结果(image2.jpg)

public KontakItemListBaseAdapter(Context context, ArrayList<KontakItemDetails> results) {
    itemDetailsrrayList = results;
    l_Inflater = LayoutInflater.from(context);
}

public int getCount() {
    return itemDetailsrrayList.size();
}

public Object getItem(int position) {
    return itemDetailsrrayList.get(position);
}

public long getItemId(int position) {
    return position;
}

public View getView(int position, View convertView, ViewGroup parent) {
    try{
    ViewHolder holder;
    if (convertView == null) {
        convertView = l_Inflater.inflate(R.layout.kontak_details_view, null);
        holder = new ViewHolder();
        holder.txt_itemName = (TextView) convertView.findViewById(R.id.name);
        holder.txt_itemDescription = (TextView) convertView.findViewById(R.id.itemDescription);
        holder.itemImage = (ImageView) convertView.findViewById(R.id.photo);
        convertView.setTag(holder);
    } else {
        holder = (ViewHolder) convertView.getTag();
    }

    holder.txt_itemName.setText(itemDetailsrrayList.get(position).getName());
    holder.txt_itemDescription.setText(itemDetailsrrayList.get(position).getItemDescription());


    holder.itemImage.setImageResource(R.drawable.iddy);

    }catch (Exception ex) {
        throw new Error("kontakitemlistadapter :"+ex.toString());

    }
    return convertView;
}

static class ViewHolder {
    TextView txt_itemName;
    TextView txt_itemDescription;
    TextView txt_itemPrice;
    ImageView itemImage;
}

我认为我的问题在这里..

  
    

holder.itemImage.setImageResource(R.drawable.iddy);

  

如何将R.drawable.iddy更改为路径名?

2 个答案:

答案 0 :(得分:1)

你可以将Arrays中的所有Drawables id作为:

private Integer[] images;

Field[] list = R.drawable.class.getFields();
int count = 0, index = 0, j = list.length;

images = new Integer[count];
try {
   for(int i=0; i < j; i++)
    images[index++] = list[i].getInt(null);
 } catch(Exception e) {}

现在使用images Array在Adapter的getView方法中将Image src设置为:

holder.itemImage.setImageResource(images[position]);

答案 1 :(得分:0)

根据上下文更改可绘制参考

holder.itemImage.setImageResource(R.drawable.iddy);