Buvana Megan
Buvana Megan

Reputation: 73

spinner value not passing from one activity to another

ViewToken.class:

    spinnerGenre = (Spinner) findViewById(R.id.spinnerGenres);
    spinnerGenre1 = (Spinner) findViewById(R.id.spinner);

    docname = spinnerGenre1.getSelectedItem().toString();
    session = spinnerGenre.getSelectedItem().toString();

    next=(Button)findViewById(R.id.button4);
    next.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent next=new Intent(ViewToken.this,Tokens.class);
            next.putExtra("docname", docname.toString());
            next.putExtra("session", session.toString());
            startActivity(next);
        }
    });

Tokens.class

    Intent i2 = getIntent();
    final String docname = i2.getStringExtra("docname");
    final String session = i2.getStringExtra("session");

The spinner value from ViewToken.class is not passed to Tokens.class

Upvotes: 2

Views: 70

Answers (4)

jay
jay

Reputation: 1

int positionOfSelectedDataFromSpinner;
iv.putExtra("position", positionOfSelectedDataFromSpinner);

Create a New Method

spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
                positionOfSelectedDataFromSpinner= i;
            }

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

            }
        });

And In Second Activity

int positionToShowToSpinner = iv.getIntExtra("position",0);
spinner.setSelection(positionToShowToSpinner);

Upvotes: 0

Roy Parejo
Roy Parejo

Reputation: 341

get the values from the spinners just before send them.

next.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        docname = spinnerGenre1.getSelectedItem().toString();
        session = spinnerGenre.getSelectedItem().toString();

        Intent next=new Intent(ViewToken.this,Tokens.class);
        next.putExtra("docname", docname.toString());
        next.putExtra("session", session.toString());
        startActivity(next);
    }
});

Upvotes: 0

Dziugas
Dziugas

Reputation: 1570

The calls to getSelectedItem() should be made in the onClick listener so that it gets the most up-to-date values selected.

So instead the onClick() method will be:

@Override
public void onClick(View v) {
    docname = spinnerGenre1.getSelectedItem().toString();
    session = spinnerGenre.getSelectedItem().toString();
    Intent next=new Intent(ViewToken.this,Tokens.class);
    next.putExtra("docname", docname);
    next.putExtra("session", session);
    startActivity(next);
}

It is likely that the calls to getSelectedItem() where being made before anything was selected and therefore the values being put() inside the intent where incorrect.

Upvotes: 1

Huzaifa Iftikhar
Huzaifa Iftikhar

Reputation: 815

Change this in ViewToken.class

next.putExtra("docname", docname);
next.putExtra("session", session);

You can do the following in your Tokens.class

Bundle extras = getIntent().getExtras();
String docname = extras.getString("docname");
String session = extras.getString("session");

Upvotes: 1

Related Questions