2009-09-27 76 views
42

我在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="" /> 

帮助非常感谢!

莱纳斯

回答

89

尝试调用setAdapter()之后移动调用setSelection()

+2

太容易了! ;)感谢commonsware,再次为我节省了一些时间! – aspartame 2009-09-27 21:39:19

+0

令人惊叹!解决了我的问题! – user712051 2012-05-22 15:57:06

+0

谢谢,在那里为我节省了几分钟;) – 2013-09-10 12:39:11

59

我有类似的问题。在我的情况下,setAdapersetSelection的顺序是正确的!执行表格onCreate工作,但从onResume执行时没有效果。

solution是致电setSelection(my_pos, true)。注意第二个参数。

+6

谢谢!我永远不会想象一个名为'animate'的参数会产生这种效果... – Timores 2011-10-12 20:29:53

+0

谢谢!我从来没有尝试过 - 浪费了那么多时间...... – 2011-11-11 15:46:22

+0

非常感谢!我不会那样做的。在找到你的帖子之前,我花了两个小时。 – Paul 2012-02-16 19:24:53

17

在我的情况没有答案的工作,所以我通过处理程序

new Handler().postDelayed(new Runnable() {   
    public void run() { 
     mSpinner.setSelection(1); 
    } 
    }, 100); 

做上速度较慢的设备上运行时,这可能会导致问题排队为setSelection,但我正在为一个特定的设备所以它的可以使用这个黑客

+0

+1,这解决了我的情况下,只有以前的不。我在Android 2.3.3的onCreate()方法,设置适配器 – 2012-11-16 21:04:33

+0

+ 1后,我发现这是让它在OnItemSelectedListener中工作的唯一方法。 – cdavidyoung 2012-11-23 05:27:14

+1

谢谢,因为没有其他工作......但它给我一个坏感觉 – Steff 2016-07-20 18:24:46

1

解决的办法是调用setSelection(my_pos,true)。注意第二个参数。

唐`忘了,如果你调用动画,设置布局PARAMS然后:) 举例:需要手动reseted

1

LinearLayout.LayoutParams spinnerLp = (LinearLayout.LayoutParams) spinner.getLayoutParams(); 
spinner.setSelection(selectedPositionAge, true); 
spinnerLp.gravity = Gravity.CENTER; 
spinner.setLayoutParams(spinnerLp); 

手动设置好的垫衬,以微调在fragment : setSelection内的微调器的相同问题在活动的第一次启动时onCreate期间正常工作,但在旋转屏幕时没有发生。我通过在片段的onViewStateRestored方法内调用setSelection来解决它,而不是在onCreate方法中调用它。我不确定,但我认为在视图完全加载之前您不能使用setSelection,即使您可以使用findViewById它。

+0

这是我的情况下的解决方案。基本上,在'onCreate'不是第一个,视图状态然后由系统恢复。那里的setSelection会被状态恢复机制恢复。解决方案是在状态恢复发生后运行'setSelection' – entropy 2014-07-07 14:24:35

+0

这看起来很有前途,但是当我在'onViewStateRestored()'方法内调用'Spinner mySpinner =(Spinner)getView()。findViewById(R.id.mySpinner)它返回null(当我在'onCreate()'中调用它时它不返回null)。 – 2015-08-02 16:50:41

27

你可以尝试

mSpinner.post(new Runnable() {   
    public void run() { 
     mSpinner.setSelection(1); 
    } 
    }); 

这将发布可运行操作的视图创建了以上的回答

+2

毫不迟疑地更好,它似乎也起作用。 – Joqn 2013-11-07 11:25:57

+0

很好的答案,谢谢 – 2017-06-05 11:35:33

1

没有工作对我来说,就立即执行。但是,工作的是在我的片段的onCreateView()方法中创建实例变量mSpinner(或者我期望的是,您的活动的'onCreate()'方法),然后在我的onLoadFinished()方法中执行此操作...

@Override 
public void onLoadFinished(Loader<Cursor> loader, Cursor cursor) { 

    adapter.swapCursor(cursor); 
    mSpinner.setSelection(mSelectedIndex); 

} 
0

试试这个,它的工作对我来说:

Spinner destSpinner = (Spinner)dialogView.findViewById(R.id.edit_event_destination); 
destSpinner.setSelection(0); 
String dest = events.get(pos).getDestination(); 
int routesPos = routes.indexOf(dest); 
destSpinner.setAdapter(aa); 
Log.d(TAG, "Dest: " + dest + ", pos: " + routesPos); 
destSpinner.setSelection(routesPos); 
1

Spinner.setSelection()如果你以前Spinner.setAdapter()

调用它不工作尝试调用setAdapter后调用setSelection()()。

故此背后:当你的适配器设置为前调用Spinner.Selection()只是意味着你正在试图通过为setSelection()来设置微调器,以自定义索引时,它不包含任何数据,或者我们可以说,这就是微调有最大的项目= 0。

so setSelection(1)表示对于max item = 0的微调器,将索引设置为1;虽然微调自己处理这个outBoundIndex,所以你的应用程序不会崩溃。

呼叫SetSelection()应setAdapter后()只

此外,如果有一个Spinner.SetOnItemSelectedListener()和你有问题,即onItemSelected(AdapterView<?> parent, View view, int position, long id)被tiggered与位置值= 0时的活性负载和则应使用这种模式。

Spinner.SetAdapter() 
Spinner.setSelection(); 
Spinner.setOnItemSelectedListener(); 
0

使用本

sp2.setAdapter(sp2.getAdapter()); 
    sp2.getAdapter().notifyDataSetChanged(); 
    sp2.setSelection(0, false); 
相关问题