Custom listview adapter -Cannot figure out how to set the image resource

323 views Asked by At

So I am creating a custom adapter where each row has an ImageView and a TextView. The TextView works fine but I am having trouble with the ImageView. I have a typed array of image resources in an xml file. When I try to set the image resource in the adapter class, I get:

setImageResource cannot be applied to android.content.res.typedarray

How do I get around this? I've been trying to figure this out to no avail.

here's my adapter class

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

My main activity

//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);

and my xml layout for the custom row

<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>

thank you for any help

3

There are 3 answers

4
Mrinmoy On

Try this,

holder.routeImg.setImageResource(imageId.getDrawable(position));
0
Rahul On

Use below code to set Image from TypeArray -

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

Your code sample is inconsistent, but you should not use any [] with TypedArray. You have a single TypedArray, not an array of TypedArray.

Given a single TypedArray, rahul's answer should do the trick.