Paul
Paul

Reputation: 47

on spinner item selected in activity one, textview in the second activity must be changed

I'm a beginner to android and i got stuck with a problem. i have spinner in Main Activity, and on item selection from spinner, the text in the Text View of second Activity must be changed according to the item selected. Here everything is fine after using On Click Listener, and set text in Main Activity with if statements with content Equals. spinner is loading and on item selected, app is crashing. So what should i write in Second Activity?

below is my Main Activity

public void onItemSelected(AdapterView<?> parent, View view, int i, long id) {

        String s = String.valueOf(s.getSelectedItem());



        if (s.contentEquals("A")) {

            textToChange.setText(R.string.PH);
            Intent intent = new Intent(MainActivity.this, Main2Activity.class);
            startActivity(intent);

        }
        else {
            textToChange.setText(R.string.PH1);
            Intent intent = new Intent(MainActivity.this, Main2Activity.class);
            startActivity(intent);

        }

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

        }

    };

Upvotes: 0

Views: 94

Answers (1)

Abhinav Suman
Abhinav Suman

Reputation: 992

if (s.contentEquals("A")) {

        textToChange.setText(R.string.PH);
        Intent intent = new Intent(MainActivity.this, Main2Activity.class);
        startActivity(intent);

    }
    else {
        textToChange.setText(R.string.PH1);
        Intent intent = new Intent(MainActivity.this, Main2Activity.class);
        startActivity(intent);

    }

At first, you have to pass the data to next activity using intent. you are doing wrong here in the above statement textToChange.setText(R.string.PH);.

Use the intent.putString("DataString") to transfer the String to the Next activity and use the same to set to the Text in TextView.

Upvotes: 3

Related Questions