John
John

Reputation: 127

Android Spinner Doesn't Return Id Properly

I'm having a difficulty in spinner android, more specifically in the data saving part.

First, I'm going to tell you what is the purpose behind the code below.

I want to create a spinner that user can choose which planet to buy, then when he/she hits the submit button, a toast will appear saying "You wanted to buy: ...." then followed by a purchase summary like username & price.

However, in the middle of the process I'm having a hard time to save the id of each spinner's element.

Every time I chose "Earth" (id= 1) & hit submit, the toast will always refer back to "Mars" which has id = 0. So it seems that the problem is my submit button will always reset the chosen id.

I intentionally set planetSpinner() method to return int, thus I could always track down the "id" being returned. But again, the submit button would always make it shows to id = 0.

Can anyone help me? I would really appreciate that. Thank you.

Spinner Code (MainActivity.java)

public int planetSpinner(){
        context = this;
        List<String> categories = new ArrayList<String>();
        categories.add("Mars($12 billions)"); //0
        categories.add("Earth ($99 billions)"); //1
        categories.add("Jupiter ($13 billions)"); //2

        cSpinner = (Spinner) findViewById(R.id.coffee_spinner);
        ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, categories);
        dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        cSpinner.setAdapter(dataAdapter);

        cSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                Toast.makeText(MainActivity.this, "You selected: " + cSpinner.getItemAtPosition(position), Toast.LENGTH_LONG).show();
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {
                Toast.makeText(MainActivity.this, "Nothing selected", Toast.LENGTH_LONG).show();
            }
        });

        return cSpinner.getSelectedItemPosition();
    }

Submit Purchase Code (MainActivity.java)

public void submitOrder(View view){

        Toast.makeText(MainActivity.this, "You wanted to buy: " + planetSpinner(), Toast.LENGTH_LONG).show();

        displayMessage(confirmationOrder(userName, price));
    }

Upvotes: 2

Views: 68

Answers (3)

Amit Ranjan
Amit Ranjan

Reputation: 567

public class MySpinner extends Activity implements OnItemSelectedListener{
   @Override
   public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.main);
      Spinner spinner = (Spinner) findViewById(R.id.coffee_spinner);
      spinner.setOnItemSelectedListener(this);
      List<String> categories = new ArrayList<String>();
        categories.add("Mars($12 billions)"); //0
        categories.add("Earth ($99 billions)"); //1
        categories.add("Jupiter ($13 billions)"); //2
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, categories);

      // Drop down layout style - list view with radio button
      dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

      // attaching data adapter to spinner
      spinner.setAdapter(dataAdapter);
 }
 @Override
   public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
      // On selecting a spinner item
      String item = parent.getItemAtPosition(position).toString();

      // Showing selected spinner item
      Toast.makeText(parent.getContext(), "Selected: " + item, Toast.LENGTH_LONG).show();
   }
   public void onNothingSelected(AdapterView<?> arg0) {
      // TODO Auto-generated method stub
   }
}

Upvotes: 1

Android.K.Doe
Android.K.Doe

Reputation: 136

It is because the spinner was reinitialized everytime you call the function. Thus, the selected value was the first item, that is why your function always returns 0. Hope that helps.

Upvotes: 1

K.Sopheak
K.Sopheak

Reputation: 23144

Have you try this:

Toast.makeText(MainActivity.this, "You selected: " + categories.get(position), Toast.LENGTH_LONG).show();

Upvotes: 1

Related Questions