我想做这样的事情,Plz帮助我
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);
我需要让R.array.country_array成为动态的,例如将country_code更改为另一个数组名称,如city_array或类似stat_array。
我试着写“R.array。”+ dynamic_variable_array_name
但是我失败了..没什么帮助
答案 0 :(得分:4)
为什么不在>>之前初始化数组将其引用到ArrayAdapter?
Array myArray = getMyArray();
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
this, myArray, android.R.layout.simple_spinner_item);
[更新]
好的,我刚刚注意到ArrayAdapter.createFromResource
不适用于非资源。但这会奏效:
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, myArray);
答案 1 :(得分:0)
试试这个
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);