0

in my activity, there are two Spinner for Count of Questions and also Show Time of Questions which must chosen between 20 to 40 from Spinner drop down list.
but when I run the function only one spinner item is showed as a result in my Toast.

AppView

here is the Code:

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

    } 

also here is my 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;
}

and at the end my 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();

}
Phantômaxx
  • 37,901
  • 21
  • 84
  • 115

2 Answers2

0

But when I run the function only one spinner item is showed as a result in my Toast.

If I get the answer correct, you expect to see two Toast messages appearing. But that cannot happen, because you are showing them simultaneously, and therefore they are being shown one atop the other, thus you cannot see both of them.

You can change either one's duration to Toast.LENGTH_LONG and you'll see the next Toast message.

azizbekian
  • 60,783
  • 13
  • 169
  • 249
0

Thanks for your answers about toast command but my problem was related to Spinner. so after many investigation I could found the solution by removing inner class related to Spinner and adding the commands into onCreate method.
here is the modified code:

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

            }
        });

    }