Reputation: 51
Am trying to attach a resource string name to an expression but can't get going. My code is below:The erorr occurs specifically in the createFromResource
method's argument R.array.+ precedingDigitsIdentifier
. Any workaround?
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();
}
}
Upvotes: 0
Views: 37
Reputation: 41271
You can't concatenate a variable name and expect it to work. Rather, you'll need to use a different method:
ArrayAdapter.createFromResource(getActivity(),
getResources().getIdentifier(precedingDigitsIdentifier, "drawable", getPackageName()),
android.R.layout.simple_spinner_item);
This will get the resource ID by a lookup rather than using the Java identifier, which allows you to use a String that is the result of a concatenation.
Upvotes: 1