自定义列表视图适配器 - 无法弄清楚如何设置图像资源

时间:2016-12-28 05:02:27

标签: android listview custom-adapter

所以我创建了一个自定义适配器,其中每行都有ImageViewTextViewTextView工作正常,但我遇到了ImageView的问题。我在xml文件中有一个类型化的图像资源数组。当我尝试在适配器类中设置图像资源时,我得到:

  

setImageResource无法应用于android.content.res.typedarray

我如何解决这个问题?我一直试图弄明白这一点无济于事。

这是我的适配器类

public class CustomAdapter extends BaseAdapter{
CharSequence [] result;
Context context;
TypedArray[] imageId;
MyDBHandler dbHandler;
private static LayoutInflater inflater=null;
public CustomAdapter(MainActivity mainActivity, CharSequence[] routes, TypedArray[] routeNum) {

    //dbHandler = new MyDBHandler(null, null, null, 1);
    // TODO Auto-generated constructor stub
    result=routes;
    context=mainActivity;
    imageId=routeNum;
    inflater = ( LayoutInflater )context.
            getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public int getCount() {
    // TODO Auto-generated method stub
    return result.length;
}

@Override
public Object getItem(int position) {
    // TODO Auto-generated method stub
    return position;
}

@Override
public long getItemId(int position) {
    // TODO Auto-generated method stub
    return position;
}

public class Holder
{
    TextView routeText;
    ImageView routeImg;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
    // TODO Auto-generated method stub
    Holder holder=new Holder();
    View customView;
    customView = inflater.inflate(R.layout.custom_row, null);
    holder.routeText=(TextView) customView.findViewById(R.id.routeText);
    holder.routeImg=(ImageView) customView.findViewById(R.id.routeImage);
    holder.routeText.setText(result[position]);
    holder.routeImg.setImageResource(imageId[position]);
     return customView;
}

我的主要活动

//fill list view with xml array of routes
    final CharSequence[] routeListViewItems = getResources().getTextArray(R.array.routeList);
    //fills route detail image view with xml array of images
    final TypedArray image = getResources().obtainTypedArray(R.array.routeImages);


    ListView routeListView;
    routeListView=(ListView) findViewById(R.id.routeListView);
    routeListView.setAdapter(new CustomAdapter(this,routeListViewItems, image);

和我的自定义行的xml布局

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="horizontal" android:layout_width="match_parent"
android:layout_height="match_parent">


<ImageView
    android:layout_width="35dp"
    android:layout_height="35dp"
    android:id="@+id/routeImage"
    android:layout_gravity="center_vertical"/>
<TextView
    android:text="TextView"
    android:layout_width="wrap_content"
    android:layout_height="51dp"
    android:id="@+id/routeText"
    android:layout_weight="0.62"
    android:textSize="18sp"
    android:gravity="center_vertical"
    android:textColor="@android:color/black"
    android:typeface="normal"
     />

<ImageView
    android:layout_width="35dp"
    android:layout_height="35dp"
    android:id="@+id/checkImageView"
    android:layout_gravity="center_vertical"/>

</LinearLayout>

感谢您的帮助

3 个答案:

答案 0 :(得分:0)

  

试试这个,

holder.routeImg.setImageResource(imageId.getDrawable(position));

答案 1 :(得分:0)

使用以下代码从TypeArray设置图像 -

holder.routeImg.setImageResource(imageId.getResourceId(i, defaultValue));

答案 2 :(得分:0)

您的代码示例不一致,但不应将任何[]TypedArray一起使用。您只有一个TypedArray,而不是TypedArray的数组。

鉴于单TypedArray,rahul的答案应该可以解决问题。