我正在尝试从字符串数组中填充Spinner。我已经在SO上尝试了不同解决方案的各种方法,但每次微调器都是空的。
注意:我在actionbarsherlock中使用微调器(作为导航列表)
以下是代码:
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
super.onCreateOptionsMenu(menu, inflater);
inflater.inflate(R.menu.main, menu);
MenuItem menuItem = menu.findItem(R.id.menu_spinner);
IcsSpinner spinner = (IcsSpinner) menuItem.getActionView();
// Approach 1
// using Spinner Adapter
SpinnerAdapter mSpinnerAdapter;
if (Build.VERSION.SDK_INT <= 10) {
mSpinnerAdapter = ArrayAdapter.createFromResource(activity,
R.array.news_menu, android.R.layout.simple_spinner_item);
} else {
mSpinnerAdapter = ArrayAdapter.createFromResource(activity,
R.array.news_menu,
android.R.layout.simple_spinner_dropdown_item);
}
Log.d(Const.DEBUG, "Spinner Count = " + mSpinnerAdapter.getCount());
//Approach 2
//using ArrayAdapter
Resources res = getResources();
ArrayAdapter<String> adapter = new ArrayAdapter<String>(activity,
android.R.layout.simple_spinner_item,
res.getStringArray(R.array.news_menu));
Log.d(Const.DEBUG, "Adapter Count = " + adapter.getCount());
//Approach 3
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
activity, R.array.news_menu,
android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
//spinner.setSelection(0);
}
mSpinnerAdapter和adapter(array)都将计数显示为4.这是我的news_menu字符串数组
<string-array name="news_menu">
<item>News</item>
<item>Tour Dates</item>
<item>Festival News And Rumors</item>
<item>Late Night Performances</item>
</string-array>
答案 0 :(得分:0)
我习惯做类似的事情来从资源数组中填充微调器:
ArrayAdapter<String> adapter = ArrayAdapter.createFromResource(this,
R.array.news_menu, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
我希望它有所帮助。