JetPro
JetPro

Reputation: 1054

How to retrieve String from a spinner using array?

I added arrays on the string.xml, and these strings are visible in my Spinner when I run the app. The problem is that the Toast does not display the value of the gender variable.

I made a separate class for this because I will also use Spinners for birthmoth, birthdate and birthyear.

public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.register);

    //SPINNERS
    spinner_gender = (Spinner) findViewById(R.id.reg_gender);
    ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,
            R.array.array_gender, android.R.layout.simple_spinner_item);
    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    spinner_gender.setAdapter(adapter);
    spinner_gender.setOnItemSelectedListener(new MyOnItemSelectedListener());

    public class MyOnItemSelectedListener implements OnItemSelectedListener
{

    public void onItemSelected(AdapterView<?> parent,
        View view, int pos, long id) 
    {
        genderString = parent.getItemAtPosition(pos).toString();
        Toast.makeText(getBaseContext(), parent.getItemAtPosition(pos).toString(), Toast.LENGTH_LONG);
    }

    public void onNothingSelected(AdapterView<?> parent) 
    {
      // Do nothing.
    }
}

Upvotes: 0

Views: 154

Answers (2)

Ratna Halder
Ratna Halder

Reputation: 2004

Try this..

Toast.makeText(parent.getContext(), "OnItemSelectedListener : " + parent.getItemAtPosition(pos).toString(), Toast.LENGTH_SHORT).show();

OR.. Simply you can use

Toast.makeText(getApplicationContext(), " Your Toast message in here",Toast.LENGTH_LONG).show();

Upvotes: 0

user
user

Reputation: 87064

The Toast will not appear if you don't call the method show() on it:

Toast.makeText(getBaseContext(), parent.getItemAtPosition(pos).toString(), Toast.LENGTH_LONG).show();

Upvotes: 1

Related Questions