我在Spinner的setSelection上遇到了一些问题。当代码中显示微调器时,我将该值设置为预选,但它不起作用,列表中的第一个替代项始终处于选中状态。代码如下所示:Android:setSelection对Spinner没有任何影响
LayoutInflater li = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
final View dialogView = li.inflate(R.layout.edit_event, null);
...
ArrayList<String> routes = new ArrayList<String>();
// routes filled with values at runtime
...
ArrayAdapter<String> aa = new ArrayAdapter<String>(GOFdroid.this, android.R.layout.simple_spinner_item, routes);
aa.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
Spinner destSpinner = (Spinner) dialogView.findViewById(R.id.edit_event_destination);
String dest = events.get(pos).getDestination();
int routesPos = routes.indexOf(dest);
Log.d(TAG, "Dest: " + dest + ", pos: " + routesPos);
destSpinner.setSelection(routesPos);
destSpinner.setAdapter(aa);
的代码按预期工作除了为setSelection部分,我只是想不通为什么。
微调的XML布局看起来是这样的(不是整个布局,只是微调部分):
// DESTINATION
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Destination:" />
<Spinner
android:id="@+id/edit_event_destination"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:prompt="@string/choose_dest"
android:layout_marginBottom="10dip"
android:text="" />
帮助非常感谢!
莱纳斯
太容易了! ;)感谢commonsware,再次为我节省了一些时间! – aspartame 2009-09-27 21:39:19
令人惊叹!解决了我的问题! – user712051 2012-05-22 15:57:06
谢谢,在那里为我节省了几分钟;) – 2013-09-10 12:39:11