Android 自定义listview适配器-无法确定如何设置映像资源

Android 自定义listview适配器-无法确定如何设置映像资源,android,listview,custom-adapter,Android,Listview,Custom Adapter,因此,我正在创建一个自定义适配器,其中每一行都有一个ImageView和一个TextView。TextView工作正常,但我在使用ImageView时遇到了问题。我在xml文件中有一个类型化的图像资源数组。当我尝试在适配器类中设置映像资源时,我得到: setImageResource无法应用于android.content.res.typedarray 我该怎么做?我一直想弄明白这件事,但没有用 这是我的适配器类 public class CustomAdapter extends BaseAd

因此,我正在创建一个自定义适配器,其中每一行都有一个
ImageView
和一个
TextView
TextView
工作正常,但我在使用
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>

谢谢你的帮助

试试这个


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

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

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


给定一个
TypedArray
,rahul的答案应该可以做到这一点。

改用TypedArray试试这个。setImageResource(imageMain.getResourceId(position,-1));你为什么要用typedarray,有什么具体原因吗?我建议在列表中添加所有图片,然后你们可以像以前一样继续,如果有帮助的话,请点击这个链接。谢谢。我会浏览这个链接,如果我能让它工作的话,我会向你们汇报。我有一个大约200个图像的列表,它们在XML文件中比在代码中添加它们更容易管理。
holder.routeImg.setImageResource(imageId.getResourceId(i, defaultValue));