2

How is it possible to transfer a string from a listView on click and use the string saved in a variable in a different activity?

Antonio
  • 67
  • 1
  • 13
  • around four people have spent their time in order to answer your question, mark the first and most appropriate one as the answer , it encourage people in order to spent their time on helping others. – Heshan Sandeepa Apr 01 '15 at 18:12

4 Answers4

3

You can do this by writing code for calling next activity in listview's item click event

Intent intent= new Intent(getBaseContext(),AnotherActivity.class);
            intent.putExtra("ANY_KEY", "YOUR STRING VALUE");
            startActivity(intent);

Then in other activity's on create method get value of your string by

String str=getIntent().getStringExtra("ANY_KEY");
Ramesh Bhati
  • 1,230
  • 16
  • 25
Jaykishan Sewak
  • 813
  • 6
  • 13
2

Yes, use a code like this:

lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                    Intent i = new Intent(getBaseContext(), Activity2.class);
                    i.putExtra("var", (String)lv.getAdapter().getItem(position));
                    startActivity(i);
                }
            });

And in the Activity2 get the var

Bundle bundle = getIntent().getExtras();
String var = bundle.getString("var");
Santiago
  • 1,664
  • 14
  • 23
1
Intent intent = new Intent(fisrtActivity.this, secondActivity.class);
intent.putExtra(name of extra,String);
startActivity(intent);

To get in secondActivity:

Intent i = getIntent()
String s = i.getStringExtra(name of extra);
Qwertenx
  • 23
  • 7
0

modify your adapter class as follows and try ,

        @Override
        public void onItemClick(AdapterView<?> arg0, View v, int position,
            long arg3) {
        Intent intent = new Intent(context/getApplicationContext(),SecondActivity.class);
        intent.putExtra("value", your_list.get(position));
        context/getApplicationContext().startActivity(intent);
    }

in order to get the value ,

String yourString  = getIntent().getExtras().getString("value");
Heshan Sandeepa
  • 3,049
  • 1
  • 32
  • 43