-1

I want to pass different string values to another activity based upon the selection of the item in the list. This should do be done in only one textview. As I am unable to find as it's showing that the textview id is already assigned to another one. How can I pass multiple strings to a single textview of another activity?

Here is the code:

In Main Activity:

listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int i, long id) {
            switch (i){
                case 0:
                    Intent intent = new Intent(MainActivity.this, intent_activity.class);
                    intent.putExtra("first", FourDos);
                    startActivity(intent);
                    break;
                case 1:
                    Intent intent1 = new Intent(MainActivity.this, intent_activity.class);
                    intent1.putExtra("second", FourGL);
                    startActivity(intent1);
                    break;

                case 2:
                    Intent intent2 = new Intent(MainActivity.this, intent_activity.class);
                    intent2.putExtra("second", FourTest);
                    startActivity(intent2);
                    break;
            }

        }
    });
    listView.setAdapter(itemsAdapter);

In another activity:

 protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_intent_activity);

    Intent intent= getIntent();
    String message = intent.getStringExtra("first");
    TextView text = (TextView) findViewById(R.id.intent_text);
    text.setText(message);

    Intent intent1= getIntent();
    String message1 = intent1.getStringExtra("second");
    TextView text1 = (TextView) findViewById(R.id.intent_text);
    text1.setText(message1);

    Intent intent2= getIntent();
    String message2 = intent2.getStringExtra("third");
    TextView text2 = (TextView) findViewById(R.id.intent_text);
    text2.setText(message2);

}

Here are the screenshots

enter image description here

denvercoder9
  • 2,979
  • 3
  • 28
  • 41
  • 1
    Possible duplicate of [How to use putExtra() and getExtra() for string data](https://stackoverflow.com/questions/5265913/how-to-use-putextra-and-getextra-for-string-data) – kandroidj Sep 12 '19 at 13:59
  • Why do you use different key ("first", "second") in firstActivity? If you use same key, then in the second Activity you just check for that key. – Ali Sep 12 '19 at 14:07
  • I thought the key may be unique for each string passing.But it worked when using a same key in each intent.Thank you very much! –  Sep 12 '19 at 14:17

1 Answers1

0

In mainActivity :

listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int i, long id) {
          Intent intent = new Intent(MainActivity.this, intent_activity.class);  
          switch (i) {
                case 0:
                    intent.putExtra("KEY", FourDos);
                    break;
                case 1:
                    intent.putExtra("KEY", FourGL);
                    break;
                case 2:
                    intent.putExtra("KEY", FourTest);
                    break;
            }
            startActivity(intent);
        }
    });

And in another Activity :

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_intent_activity);

    Intent intent= getIntent();
    String message = intent.getStringExtra("KEY");
    TextView text = (TextView) findViewById(R.id.intent_text);
    text.setText(message);
}
Ali
  • 9,800
  • 19
  • 72
  • 152