2015-06-22 38 views
0

我試圖將資源字符串名稱附加到表達式,但無法繼續。我的代碼如下:錯誤發生在createFromResource方法的參數R.array.+ precedingDigitsIdentifier中。任何解決方法?android將字符串值名稱連接到表達式

public void createPrecedingDigitsSpinner(String selectedCountry){ 

     String selectedCntry =selectedCountry.toLowerCase(); 

     /**confirm that value of this 
     * country exist in countries.xml**/ 
     ArrayAdapter<CharSequence> adapter; 
     String precedingDigitsIdentifier = selectedCntry + "_preceding_digits"; 

     try{ 
      ArrayAdapter.createFromResource(getActivity(),*R.array.+ precedingDigitsIdentifier*, 
        android.R.layout.simple_spinner_item); 
     }catch (Resources.NotFoundException e){ 


      CharSequence text = "the selected country contains no preceding digits data try another time"; 
    Toast toast = Toast.makeText(context, text, duration); 
      toast.show(); 

     } 
    } 
+0

你得到什麼錯誤? – Paradox

+0

在代碼中,我看到* R.array而不是「R.array – Paradox

回答

1

您不能連接變量名稱並期望它工作。相反,你需要使用不同的方法:

ArrayAdapter.createFromResource(getActivity(), 
    getResources().getIdentifier(precedingDigitsIdentifier, "drawable", getPackageName()), 
    android.R.layout.simple_spinner_item); 

這將讓通過查找資源ID,而不是使用Java標識符,它可以讓你用一個字符串,是一個串聯的結果。

+0

非常感謝。 –

相關問題