简体   繁体   English

Android:setSelection对Spinner没有影响

[英]Android: setSelection having no effect on Spinner

I'm having some problem with setSelection on a Spinner. 我在Spinner上遇到setSelection问题。 I set the value to be pre-selected when the spinner is shown in code, but it has no effect and the first alternative in the list is always selected. 我在代码中显示微调器时将值设置为预选,但它没有任何效果,并且始终选择列表中的第一个选项。 The code looks like this: 代码如下所示:

    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);

The code works as intended except for the setSelection-part, and I just can't figure out why. 除了setSelection-part之外,代码按预期工作,我无法弄清楚原因。

The XML-layout of the spinner looks like this (not the entire layout, only the spinner 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="" />

Help is very much appreciated! 非常感谢帮助!

Linus 莱纳斯

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

I had similar problem. 我有类似的问题。 In my case setAdaper and setSelection were in correct order! 在我的情况下, setAdapersetSelection顺序正确! Executed form onCreate worked, but when executed from onResume had no effect. 已执行的表单onCreate工作,但从onResume执行时没有任何效果。

The solution is to call setSelection(my_pos, true) . 解决方案是调用setSelection(my_pos, true) Notice the second parameter. 注意第二个参数。

You might try 你可以试试

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

this will post the runnable action to run as soon as the view is created 这将在创建视图后立即发布runnable操作

In my case none of the answers worked, so I queued the setSelection through a Handler 在我的情况下,没有一个答案有效,所以我通过Handler将setSelection排队

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

Doing this could cause problems when running on slow devices, but I'm working for a specific device so it's ok to use this hack 这样做可能会导致在慢速设备上运行时出现问题,但我正在为特定设备工作,因此可以使用此hack

The solution is to call setSelection(my_pos, true). 解决方案是调用setSelection(my_pos,true)。 Notice the second parameter. 注意第二个参数。

Don`t forget, if you call animate, setup layout params then :) Example: 不要忘记,如果你调用动画,然后设置布局参数:)示例:

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

manually setted paddings to spinner needs to be reseted manually 需要手动重置手动设置的填充到微调器

I had the same problem with a spinner inside a fragment : setSelection works correctly during the onCreate at first start of the activity, but not when I rotate the screen. 我在fragment : setSelection使用了一个微调器时遇到了同样的问题fragment : setSelection在第一次启动活动时在onCreate期间正常工作,但在旋转屏幕时却没有。 I solved it by calling setSelection within the onViewStateRestored method of the fragment instead of calling it inside the onCreate method. 我通过在片段的onViewStateRestored方法中调用setSelection而不是在onCreate方法中调用它来解决它。 I'm not sure but I think that you can't use setSelection until the view is fully loaded, even if you can findViewById it. 我不确定,但我认为在视图完全加载之前你不能使用setSelection ,即使你可以findViewById它。

None of the previous answers worked for me. 之前的答案都不适合我。 What did work, though, was creating the instance variable mSpinner in the onCreateView() method of my fragment (or in the onCreate() method of your activity), then doing this in my onLoadFinished() method... 但是,在我的片段的onCreateView()方法中(或在你的活动的onCreate()方法中onCreate()创建了实例变量mSpinner ,然后在我的onLoadFinished()方法中执行此操作...

@Override
public void onLoadFinished(Loader<Cursor> loader, Cursor cursor) {
    adapter.swapCursor(cursor);
    //mSpinner.setAdapter(adapter);
    mSpinner.setSelection(mSelectedIndex);
}

Spinner.setSelection() do not work if you call it before Spinner.setAdapter() Spinner.setSelection()如果你之前调用它不工作Spinner.setAdapter()

Try calling setSelection() after the call to setAdapter(). 尝试调用setAdapter()后调用setSelection() ()。

Reason Behind this : when you call Spinner.Selection() before setting an adapter to it simply means that you are trying to set spinner to custom index by setSelection() when it doesn't contain any data or we can say thats spinner has max item =0. 原因在此背后 :当你在设置适配器之前调用Spinner.Selection() ,只是意味着当它不包含任何数据时我们试图通过setSelection()将微调器设置为自定义索引,或者我们可以说这个微调器具有最大值item = 0。

so setSelection(1) means setting index to 1 for spinner which has max item =0; 所以setSelection(1)表示对于具有max item = 0的微调器,将index设置为1; Although spinner itself handles this outofBoundIndex so your app does not crashes. 虽然微调器本身处理这个outofBoundIndex,所以你的应用程序不会崩溃。

call to SetSelection() should be after setAdapter() only SetSelection()调用应仅在setAdapter()之后

Also if you have a Spinner.SetOnItemSelectedListener() and you have problem that onItemSelected(AdapterView<?> parent, View view, int position, long id) is tiggered with position value=0 when activity load and then you should use this pattern. 此外,如果你有一个Spinner.SetOnItemSelectedListener()并且你有问题onItemSelected(AdapterView<?> parent, View view, int position, long id)在活动加载时tiggered与位置值= 0然后你应该使用这种模式。

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

use this 用这个

    sp2.setAdapter(sp2.getAdapter());
    sp2.getAdapter().notifyDataSetChanged();
    sp2.setSelection(0, false);

try this, it worked for me: 试试这个,它对我有用:

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);

Sometimes, we may not set listeners because the spinner may be set to a certain value, and disabled as per requirement. 有时,我们可能不会设置侦听器,因为可以将微调器设置为某个值,并根据需要禁用。

This can lead to setSelection() not selecting a value, since it needs a listener. 这可能导致setSelection()没有选择值,因为它需要一个监听器。

Make sure that the Spinner's setOnItemSelectedListener() is set to a custom listener like below. 确保Spinner的setOnItemSelectedListener()设置为自定义侦听器,如下所示。

Even if spinner is disabled, we must set a listener like below, so that setSelection() method works. 即使禁用了微调器,我们也必须设置一个类似于下面的监听器,以便setSelection()方法起作用。

spinnerListener.setOnItemSelectedListener(spinnerListener);
AdapterView.OnItemSelectedListener spinnerListener = new 
AdapterView.OnItemSelectedListener() 
{
    @Override
    public void onItemSelected(AdapterView<?> adapterView, View view, int position, long l) {
      //Your code
    }
}
spinnerListener.setSelection(0);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM