2016-07-06 6 views
1

国とフラグのピッカーを作成します。私は旗のイメージを設定したいが、私はそのイメージをその名前に基づいて選びたい。 BAscially私は電話番号のユーザーの前にISDコードを追加するためにそれを使用できるようにアンドロイド用のフラグとISDコードピッカーを作成しようとしています。アンドロイドでイメージリソースを取得するにはどうすればいいですか?

ImageView img1 = (ImageView)findViewById(R.id.imgFlag); 
img1.setBackgroundResource(R.drawable.India_91); 

アダプタのようなものです。

//India_91 is an image India_91.png, 
//suppose if I have USA_1.png as image then i should be able to use 
//string "USA_1" to get the related resource and set it as 
//BackgroundResource. 

    @Override 
public View getView(int position, View convertView, ViewGroup parent) { 
     long resId = getResourceIdUsingResourceString(arrayOfStrings[position]); 
     // I should get the Id of the image 
     // resource named as"USA_91" 
    } 

    //This is a utility function outside adapter class 
    long getResourceIdUsingResourceString(string resourceName){ 
     //Here i want to add a code which can check for 
     //resource name and then get id of it 
     //which can then be used by callee function/block 
     //to fetch the correct resource and display it 
    } 

答えて

3

あなたが探している欠けている部分はResources.getIdentifier()です。名前をStringとして渡し、整数の識別子を取得できます。

Resources | Android Developers

ex。

long resId = parent.getResources().getIdentifier(arrayOfStrings[position], "drawable", mApplicationContext.getPackageName()); 
関連する問題