FelixA
FelixA

Reputation: 127

How to convert from drawable to int-array

I have an int-array like this:

private int icons[] = new int[] {R.drawable.itr300, R.drawable.itr3500};

But I want to place it in the array.xml.

If I do it like this:

<resources>
<array name="icons">
    <item>@drawable/itr300</item>
    <item>@drawable/itr3500</item>
</array>
</resources>

private TypedArray images = getResources().obtainTypedArray(R.array.icons);
private int icons[] = images...

Now I want to convert Drawable into the int-array. But I don't know how to do it. Can anybody help?

Comment:

I think your answer were useful. But I have not found the answer yet. Can you help me again, to find the syntax errors? I seems good, but I will not work.

   private TypedArray images = getResources().obtainTypedArray(R.array.icons);
   private int[] icons = new int[images.length()];  
    for (int i = 0; i < icons.length(); i++) {
        icons[i] = images.getResourceId(i, 0);
    }

Upvotes: 3

Views: 4906

Answers (3)

user3693644
user3693644

Reputation: 1

do it like this

        public override View GetView(int position, View convertView, ViewGroup parent)
    {
        View row = null;

        if(convertView == null)
        {
            LayoutInflater i = (LayoutInflater)context.GetSystemService(Context.LayoutInflaterService);
            row = i.Inflate(Resource.Layout.custom_menu, parent, false);
        }
        else
        {
            row = convertView;
        }

        TextView title = row.FindViewById<TextView>(Resource.Id.menu_title);
        ImageView icon = row.FindViewById<ImageView>(Resource.Id.menu_icon);


        title.Text = menuTitle[position];
        //icon.SetImageResource(menuIcon[position]);
        icon.SetImageResource(Icon.GetResourceId(position,-1));

        return row; 
    }

Upvotes: 0

jennifer
jennifer

Reputation: 8261

public int getInt (int index, int defValue)   : Retrieve the integer value for the attribute at index.

Parameters

index Index of attribute to retrieve.

defValue Value to return if the attribute is not defined.

public int getInteger (int index, int defValue)

Parameters

index Index of attribute to retrieve.

defValue Value to return if the attribute is not defined or not a resource

for (int i = 0; i < icons.length; i++) {

        icons[i]  = images.getInt (i, int defValue) 
    }

Upvotes: 0

Reno
Reno

Reputation: 33792

Using getResourceId()

Hint: Use a loop

Upvotes: 3

Related Questions