调用setImageResource不显示res / drawable中的图像

时间:2013-02-22 11:12:29

标签: android android-listview android-imageview

我在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的来源,我尝试将图片动态加载到ImageView标识的R.id.list_image

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());
    }
}

任何人都可以告诉我为什么图像没有正确加载以及我如何解决它?

1 个答案:

答案 0 :(得分:1)

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

尝试省略扩展名。