[英]Refresh the current item of a Spinner
Consider the following combo list 考虑以下组合列表
comboList = new Spinner(this);
list_arr = new ArrayList<String>();
The ArrayList is filled with Strings from SharedPreferences and the Spinner is populated in this way ArrayList中填充了SharedPreferences中的字符串,并以此方式填充了Spinner
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, list_arr);
comboList.setAdapter(dataAdapter);
Then it gets updated in case of an event OnClickListener() 然后在发生事件OnClickListener()的情况下进行更新
list_arr.clear();
ArrayList<String> res = getMyLists();
for (int i = 0; i < res.size(); i++) {
list_arr.add(res.get(i));
}
How can I refresh also the already selected item programmatically? 如何以编程方式刷新已经选择的项目? From the GUI, I have to manually select another value from the list and then change it back. 从GUI,我必须从列表中手动选择另一个值,然后再将其更改回。
This could be a duplicate of this other question but it is very old and unanswered. 这可能是另一个问题的重复,但是它已经很老了,没有答案。
您可能必须调用dataAdapter.notifyDataSetChanged();
In this case, you'll need to re-create the spinner's adapter: 在这种情况下,您需要重新创建微调器的适配器:
list_arr.clear();
ArrayList<String> res = getMyLists();
for (int i = 0; i < res.size(); i++) {
list_arr.add(res.get(i));
}
dataAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, list_arr);
comboList.setAdapter(dataAdapter);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.