O К
O К

Reputation: 15

How to use drawables in int array from resources

I have a piece of code with int array hard coded, this works correctly. But now I want to move it to an external ressource file and take images from there.

I already used the same for string array, to move hard code and use ressources, it works (see commented lines), but for int I don't have any errors but also don't have any images in app (see commented lines). Here is my int array format:

<integer-array name="mImageIds1">
<item>@drawable/ic_android_black_24dp</item>
<item>@drawable/ic_android_black_24dp</item>
<item>@drawable/ic_android_black_24dp</item>

What's wrong?

 public class MainActivity extends ListActivity {

    private String[] mText = {"First", "Second", "Third", "Fourth", "Fifth", "Sixth",
            "Seventh", "Eighth", "Ninth", "Tenth", "Eleventh", "Twelfth"};



    int[] mImageIds = {R.drawable.ic_android_black_24dp, R.drawable.ic_android_blue_24dp,
            R.drawable.ic_android_black_24dp, R.drawable.ic_android_blue_24dp...
            };

    private myAdapter mAdapter;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mAdapter = new myAdapter(this);
        setListAdapter(mAdapter);
    }

    private class myAdapter extends BaseAdapter {
        private LayoutInflater mLayoutInflater;

        myAdapter(Context context) {
            mLayoutInflater = LayoutInflater.from(context);
        }

        @Override
        public int getCount() {
            return mText.length;
        }

        @Override
        public Object getItem(int position) {
            return position;
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            if (convertView == null)
                convertView = mLayoutInflater.inflate(R.layout.activity_main, null);

            int[] mArrayImageIds=getResources().getIntArray(R.array.mImageIds1);


            ImageView image = (ImageView) convertView.findViewById(R.id.imageViewIcon);
            if (1==1)
                image.setImageResource( mArrayImageIds[position]);
               // image.setImageResource( mImageIds[position]);



            TextView signTextView = (TextView) convertView.findViewById(R.id.textViewSign);
            signTextView.setText(mText[position]);


            String[] zooString=getResources().getStringArray(R.array.text1);

            TextView dateTextView = (TextView) convertView.findViewById(R.id.textViewDate);
            //dateTextView.setText(mSignatures[position]);
            dateTextView.setText(zooString[position]);


            return convertView;
        }

        String getString(int position) {
            return mText[position] + " (" + mSignatures[position] + ")";
        }
    }
}

Upvotes: 1

Views: 126

Answers (1)

ismail alaoui
ismail alaoui

Reputation: 6063

I think TypedArray is what you are looking for. I have samples using it. If you are interested, take a look at codes below:

TypedArray tArray = getResources().obtainTypedArray(R.array.frag_home_ids);
int count = tArray.length();
int[] mArrayImageIds = new int[count];
for (int i = 0; i < mArrayImageIds .length; i++) {
    mArrayImageIds[i] = tArray.getResourceId(i, 0);
}

//Recycles the TypedArray, to be re-used by a later caller. 
//After calling this function you must not ever touch the typed array again.
tArray.recycle();

Upvotes: 1

Related Questions