-1

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

Nick Friskel
  • 2,369
  • 2
  • 20
  • 33
zsh5032
  • 141
  • 1
  • 10
  • instead using TypedArray try this one https://developer.android.com/reference/android/content/res/Resources.html#getIntArray(int) – Ahad Dec 28 '16 at 05:06

3 Answers3

0

Try this,

holder.routeImg.setImageResource(imageId.getDrawable(position));
Mrinmoy
  • 1,370
  • 2
  • 18
  • 28
  • .setImageResource(imageMain.getResourceId(position, -1)); – Mrinmoy Dec 28 '16 at 05:14
  • why are you using typedarray, any specific reason? I would suggest add all images in List, then you can carry on as you were doing earlier – Mrinmoy Dec 28 '16 at 05:16
  • follow this link if it helps http://stackoverflow.com/a/20398556/5722385 – Mrinmoy Dec 28 '16 at 05:17
  • Thanks I'll look through that link and report back if I can get it working. I have a list of about 200 images and they're easier to manage in the XML file than to add them all in code. – zsh5032 Dec 28 '16 at 05:32
0

Use below code to set Image from TypeArray -

holder.routeImg.setImageResource(imageId.getResourceId(i, defaultValue));
Rahul
  • 10,457
  • 4
  • 35
  • 55
0

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.

j__m
  • 9,392
  • 1
  • 32
  • 56