spinner.OnItemClickSelected从两个Spinner恢复一个字符串而不是两个分隔的字符串

时间:2017-04-15 11:19:39

标签: android spinner

在我的活动中,有两个Spinner用于问题数以及显示问题时间,必须在Spinner下拉列表中从20到40之间选择。
但是当我运行该函数时,我的Toast中只显示了一个微调器项目。

AppView

这是代码:

public class Setting extends Activity {

    private Spinner SpinCount, SpinTime;
    private String SelectedItem, Username;
    private int QuestionCount, QuestionTime;
    private UserDBHandler MyUDB;


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.setting);

        SpinCount = (Spinner) findViewById(R.id.SpinCount);
        SpinTime = (Spinner) findViewById(R.id.SpinTime);
        Button SpinButtun = (Button) findViewById(R.id.SpinButtun);

        SpinnerActivity(SpinCount);
        SpinnerActivity(SpinTime);

    } 

这里也是我的SpinnerActivity:

public String SpinnerActivity(final Spinner MySpin) {

    MySpin.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
            SelectedItem = String.valueOf(MySpin.getSelectedItem());
        }

        @Override
        public void onNothingSelected(AdapterView<?> parent) {
            SelectedItem = "Nothing is Selected";
        }
    });
    return SelectedItem;
}

最后我的Button onClickListener:

public void SpinClick(View view) {

    QuestionCount = Integer.parseInt(SpinnerActivity(SpinCount));
    QuestionTime = Integer.parseInt(SpinnerActivity(SpinTime));

    Toast.makeText(getApplicationContext(), QuestionCount + "", Toast.LENGTH_SHORT).show();
    Toast.makeText(getApplicationContext(), QuestionTime + "", Toast.LENGTH_SHORT).show();

}

3 个答案:

答案 0 :(得分:1)

你的吐司可能会发生碰撞。你可以这样阻止他们:

例如,在onCreate之前,我们要声明祝酒词:

  

私人Toast myToast;

并且在onCreate中我们必须使用makeToast初始化它(否则我们会收到错误):

  

myToast = Toast.makeText(getApplicationContext(),null,   Toast.LENGTH_SHORT);

每当我们想要展示敬酒时,我们只需致电:

  

myToast.setText(&#34; some text&#34;);

     

myToast.show();

这将正确取代之前的吐司。

答案 1 :(得分:0)

  

但是当我运行该函数时,我的Toast中只显示了一个微调器项目。

如果我的答案正确无误,您可能会看到两条Toast条消息。但这不可能发生,因为你是同时展示它们,因此它们会在另一个上面显示出来,因此你无法看到它们。

您可以将其中一个的持续时间更改为Toast.LENGTH_LONG,然后您会看到下一个Toast消息。

答案 2 :(得分:0)

感谢您对吐司命令的回答,但我的问题与Spinner有关。经过多次调查后,我可以通过删除与Spinner相关的内部类并将命令添加到onCreate方法中来找到解决方案 这是修改后的代码:

public class Setting extends Activity {

    private Spinner SpinCount, SpinTime;
    private String SelectedCount, SelectedTime, Username;
    private UserDBHandler MyUDB;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.setting);

        MyUDB = new UserDBHandler(this);
        Username = getIntent().getExtras().getString("Username");

        Button SpinButtun = (Button) findViewById(R.id.SpinButtun);

        SpinCount = (Spinner) findViewById(R.id.SpinCount);
        SpinCount.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {

                SelectedCount = String.valueOf(SpinCount.getSelectedItem());
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {
            }
        });

        SpinTime = (Spinner) findViewById(R.id.SpinTime);
        SpinTime.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                SelectedTime = String.valueOf(SpinTime.getSelectedItem());
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });

    }