我在Spinner上遇到setSelection问题.我在代码中显示微调器时将值设置为预选,但它没有任何效果,并且始终选择列表中的第一个选项.代码如下所示:
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);
Run Code Online (Sandbox Code Playgroud)
除了setSelection-part之外,代码按预期工作,我无法弄清楚原因.
微调器的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="" />
Run Code Online (Sandbox Code Playgroud)
非常感谢帮助!
莱纳斯