-1

I want to pass string from one activity to another.In the second activity the string should be readable.

listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
First actvity code
        @Override
        public void onItemClick(AdapterView<?> arg0, View arg1, int position,
                                long id) {

            // TODO Auto-generated method stub

            Intent dash_des = new Intent(getApplicationContext(),Dashboard_Description__page.class);

           //here should be t my string


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

        //here i want to read the string data
        }
pawan kumar
  • 61
  • 10

4 Answers4

2

You should probably read about Intents in Android.

You can achieve this by using:

Intent myIntent = new Intent(FirstActivity.this,SecondActivity.class);
myIntent.putExtra("TestString","TestValue");
startActivity(myIntent);

The above code will launch SecondActivity. Now in onCreate() of second activity you need to:

String stringCameFromFirstAcvitity = getIntent().getStringExtra("TestString");

The value of stringCameFromFirstAcvitity will be "TestValue"

In your case

listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
//First actvity code
    @Override
    public void onItemClick(AdapterView<?> arg0, View arg1, int position,
                            long id) {

        // TODO Auto-generated method stub

    Intent dash_des = new Intent(getApplicationContext(),Dashboard_Description__page.class);
    Object obj = listview.getAdapter().getItem(position);
    String value = obj.toString();
    dash_des .putExtra("TestString",value);
    startActivity(dash_des );
    }
});

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

    //here i want to read the string data
    }

Code for secondActivity remains same. Hope it helps

Alok Gupta
  • 1,353
  • 1
  • 13
  • 29
0
Intent dash_des = new Intent(getApplicationContext(),Dashboard_Description__page.class);

dash_des.putString("your_key", your_string_variable);
startActivity(dash_des);

In second activity:

Bundle bundle = getIntent().getExtras();
String text = bundle.getString("your_key");
Bui Quang Huy
  • 1,784
  • 2
  • 17
  • 50
0

When there are multiple strings that u need to pass from one activity to another its always a best practice to bundle them and pass it to the next activity. Something like this:

Intent in = new Intent(Firstactivity.this, Secondactivity.class);
    Bundle mbundle = new Bundle();
    mbundle.putString("name", name);
    mbundle.putString("phonenumber", phonenumber);
    in.putExtras(mbundle);

And they same can be retrieved from the secondactivity as follows,

 Bundle bundle = getIntent().getExtras();
 String name= bundle.getString("name");
 String phoneNumber= bundle.getString("phonenumber");
UserName_Untold
  • 165
  • 1
  • 13
0

In the intent put the extring as extra..

and then hold it back in the activity2

Example:

Intent intent = new Intent(current.this, Activity2.class);
intent.putExtra("key","value");
startActivity(intent);

in the Activity2:

String data = getIntent().getExtras().getString("key");
Community
  • 1
  • 1
ΦXocę 웃 Пepeúpa ツ
  • 47,427
  • 17
  • 69
  • 97