AppCompatSpinner.setSelection第二次不起作用

时间:2016-03-23 14:36:31

标签: android android-spinner

我在对话框上放了一个AppCompatSpinner。使用setSelection()设置选择首次使用,但是当再次显示对话框时,setSelection()不起作用。为什么呢?

例如,在下面的示例中,如果我先按Homer按钮,则微调器显示Homer,但是如果我按Marge按钮,则微调器仍显示Homer,即使我看到调试消息“Setting to marge”。

如果我先按Marge,那么即使我稍后按Homer,旋转器总会显示Marge。

MainActivity.java

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;

public class MainActivity extends AppCompatActivity
{
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Button homer = (Button)findViewById(R.id.homer);
        homer.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                Bundle args = new Bundle();
                args.putString("name", "homer");
                MyDialogue dlg = MyDialogue.getInstance(args);
                dlg.show(getSupportFragmentManager(), dlg.getClass().getName());
            }
        });

        Button marge = (Button)findViewById(R.id.marge);
        marge.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                Bundle args = new Bundle();
                args.putString("name", "marge");
                MyDialogue dlg = MyDialogue.getInstance(args);
                dlg.show(getSupportFragmentManager(), dlg.getClass().getName());
            }
        });
    }
}

MyDialogue.java

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatDialogFragment;
import android.support.v7.widget.AppCompatSpinner;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class MyDialogue extends AppCompatDialogFragment
{
    static MyDialogue instance;

    public static MyDialogue getInstance(Bundle args)
    {
        if(instance == null)
            instance = new MyDialogue();
        instance.setArguments(args);
        return instance;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
    {
        View v = inflater.inflate(R.layout.dialogue, container, false);
        AppCompatSpinner family = (AppCompatSpinner) v.findViewById(R.id.family);

        Bundle args = getArguments();
        final String name = args.getString("name");

        if(name.equals("homer"))
        {
            Log.d("spin", "Setting to homer");
            family.setSelection(0);
        }
        else if(name.equals("marge"))
        {
            Log.d("spin", "Setting to marge");
            family.setSelection(1);
        }
        else
        {
            Log.d("spin", "Setting to bart");
            family.setSelection(2);
        }

        return v;
    }
}

activity_main.xml中

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal"
    >

    <Button
        android:text="Homer"
        android:id="@+id/homer"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>

    <Button
        android:text="Marge"
        android:id="@+id/marge"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>
</LinearLayout>

dialogue.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:orientation="vertical"
              android:layout_width="match_parent"
              android:layout_height="match_parent">
    <android.support.v7.widget.AppCompatSpinner
        android:entries="@array/list_items"
        android:id="@+id/family"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

    </android.support.v7.widget.AppCompatSpinner>

</LinearLayout>

的strings.xml

<resources>
    <string name="app_name">Test2</string>
    <string-array name="list_items">
        <item>Homer</item>
        <item>Marge</item>
        <item>Bart</item>
    </string-array>
</resources>

1 个答案:

答案 0 :(得分:0)

将您的setSelection电话转移至onResume,如下所示:

@Override 
public void onResume() {
  super.onResume();
  Bundle args = getArguments();
  final String name = args.getString("name", "");

  if (name.equals("homer")) {
    Log.d("spin", "Setting to homer");
    family.setSelection(0);
  } else if (name.equals("marge")) {
    Log.d("spin", "Setting to marge");
    family.setSelection(1);
  } else {
    Log.d("spin", "Setting to bart");
    family.setSelection(2);
  }
}

每当我与setSelection合作时,它总是很好看。我的猜测是,由于您设置了android:entries而不是自己创建适配器,这可能是您setSelection次调用后重置适配器的原因,导致它重置为位置0(在这种情况下荷马)。将选择移动到onResume将通过等待所有初始化完成来解决此问题。