0

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

下面是 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我试图将图像动态加载到ImageViewR.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());
    }
}

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

4

1 回答 1

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

尝试省略扩展名。

于 2013-02-22T11:30:30.100 回答