Как конфертировать из drawable в int-array - PullRequest
3 голосов
/ 21 ноября 2011

У меня есть такой int-массив:

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

Но я хочу поместить его в array.xml.

Если я сделаю это так:

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

Теперь я хочу преобразовать Drawable в int-массив. Но я не знаю, как это сделать. Кто-нибудь может помочь?

Комментарий:

Я думаю, что ваш ответ был полезен. Но я еще не нашел ответ. Можете ли вы помочь мне снова, чтобы найти синтаксические ошибки? Кажется, хорошо, но я не буду работать.

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

Ответы [ 3 ]

3 голосов
/ 21 ноября 2011

Использование getResourceId()

Подсказка: используйте цикл

0 голосов
/ 04 мая 2016

сделай так

        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; 
    }
0 голосов
/ 21 ноября 2011
public int getInt (int index, int defValue)   : Retrieve the integer value for the attribute at index.

Параметры

index Индекс атрибута для извлечения.

defValue Значение, возвращаемое, если атрибут не являетсяопределены.

public int getInteger (int index, int defValue)

Параметры

index Индекс атрибута для получения.

defValueВозвращаемое значение, если атрибут не определен или не является ресурсом

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

        icons[i]  = images.getInt (i, int defValue) 
    }
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...