Populating android spinner with dynamic array name
i want to do something like this,Plz help me
Spinner spinner = (Spinner) findViewById(R.id.spinner5
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
this,**R.array.country_array**, android.R.开发者_运维知识库layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter5);
i need to make R.array.country_array to be dynamic for example to change country_code to another array name like city_array or like stat_array.
i tried to write "R.array."+dynamic_variable_array_name
but i failed..any help
Why don't you initialize your array before you refer it to the ArrayAdapter?
Array myArray = getMyArray();
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
this, myArray, android.R.layout.simple_spinner_item);
[Update]
Ok, i've just noticed ArrayAdapter.createFromResource
doesn't work with non-resources. But this will work:
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, myArray);
Try This
Spinner countrySpinner = (Spinner) findViewById(R.id.search_country);
CharSequence[] myArray = new CharSequence[]{
"Qatar","UAE","India","America"
};
ArrayAdapter<CharSequence> countryAdapter = new ArrayAdapter<>(getApplicationContext(), android.R.layout.simple_spinner_item,myArray);
countryAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
countrySpinner.setAdapter(countryAdapter);
精彩评论