【问题标题】:Call to setImageResource not showing image in res/drawable调用 setImageResource 不在 res/drawable 中显示图像
【发布时间】:2013-02-22 11:12:29
【问题描述】:

我在res/drawable 中有许多 jpeg 图像,我正在尝试根据数据库行 ID 动态查找图像,并在“ListView”内的ImageView 中显示相应的图像。

这是ListView每一行的xml布局:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/list_selector"
    android:orientation="horizontal"
    android:padding="5dip" >

   <!--  ListRow Left side Thumbnail image -->
    <LinearLayout android:id="@+id/thumbnail"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="3dip"
        android:layout_alignParentLeft="true"
        android:background="@drawable/image_bg"
        android:layout_marginRight="5dip">

        <ImageView
            android:id="@+id/list_image"
            android:layout_width="50dip"
           android:layout_height="50dip"/>

    </LinearLayout>

<!-- Title -->
<TextView
    android:id="@+id/title"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignTop="@+id/thumbnail"
    android:layout_toRightOf="@+id/thumbnail"
    android:textColor="#040404"
    android:typeface="sans"
    android:textSize="15dip"
    android:textStyle="bold"/>

<!-- Author -->
<TextView
    android:id="@+id/author"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_below="@id/title"
    android:textColor="#343434"
    android:textSize="10dip"
    android:layout_marginTop="1dip"
    android:layout_toRightOf="@+id/thumbnail" />


 <!-- Rightend Arrow -->
 <ImageView android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/arrow"
    android:layout_alignParentRight="true"
    android:layout_centerVertical="true"/>

这里是ArrayAdapter 的来源,我试图将图像动态加载到R.id.list_image 标识的ImageView 中。

public class TitleListingArrayAdapter extends ArrayAdapter<Title> {

    private List<Title> items;
    private Context context;

    public TitleListingArrayAdapter(Context context, int textViewResourceId, List<Title> items)  {
        super(context, textViewResourceId, items);
        this.items = items;
        this.context = context;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View view = convertView;
        if (view == null) {
            LayoutInflater inflater = (LayoutInflater)  context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            view = inflater.inflate(R.layout.titlelisting_single_row, null);
        }
        Title item = items.get(position);
        if (item!= null) {
            TextView titleView = (TextView) view.findViewById(R.id.title);            
            if (titleView != null) {
                titleView.setText(item.getName());
            }
            TextView authorView = (TextView) view.findViewById(R.id.author);
            if (authorView != null) {
                authorView.setText(item.getAuthor());
            }
            ImageView iconView = (ImageView) view.findViewById(R.id.list_image);
            iconView.setImageResource(lookupResourceId(context, item.getID()));            
        }
        return view;
    }

    private int lookupResourceId(Context context, String id) {
        String resourceName =  "thumb_"+id+".jpeg";
        return context.getResources().getIdentifier(resourceName, "drawable", context.getPackageName());
    }
}

谁能告诉我为什么图片没有正确加载以及如何修复它?

【问题讨论】:

  • lookupResourceId 是否返回有效的资源 ID?你检查了吗?

标签: android android-listview android-imageview


【解决方案1】:
private int lookupResourceId(Context context, String id) {
        String resourceName =  "thumb_"+id;
        return context.getResources().getIdentifier(resourceName, "drawable", context.getPackageName());
    }

尝试省略扩展名。

【讨论】:

  • 你看准了,Aashish!它现在正在处理这种变化。 :) 谢谢。我很好奇 android 如何区分仅扩展名不同的文件名?
  • 嗯,这真的很有趣,会在一天结束前对其进行一些研究,并一定会在这里发布。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-12-13
  • 2015-01-12
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多