如何在资源中使用int数组中的可绘制对象

时间:2019-05-29 16:03:21

标签: android

我有一段带有int数组硬编码的代码,这可以正常工作。 但是现在我想将其移至外部资源文件并从那里获取图像。

我已经使用相同的字符串数组来移动硬代码并使用资源,它可以工作(请参见注释行),但是对于int来说我没有任何错误,但是在应用程序中没有任何图像(请参见注释行)。 这是我的int数组格式:

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

怎么了?

 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] + ")";
        }
    }
}

1 个答案:

答案 0 :(得分:1)

我认为您正在寻找TypedArray。我有使用它的样品。如果您有兴趣,请查看以下代码:

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