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
  • 77
  • 2
  • 13

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,239
  • 16
  • 25
Jaykishan Sewak
  • 822
  • 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,744
  • 15
  • 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,388
  • 2
  • 35
  • 45
  • How would you go about getting the string from the listview? – Antonio Apr 01 '15 at 17:26
  • where do you store the strings ? – Heshan Sandeepa Apr 01 '15 at 17:28
  • I'm assuming when you pass this on to the second activity you need to do something list this: `Intent intent = getIntent(); valueId = intent.getStringExtra("value");` – Antonio Apr 01 '15 at 17:30
  • of course you need, how else you can get the transferred data ? – Heshan Sandeepa Apr 01 '15 at 17:33
  • Basically what i'm doing is downloading a list of user from a database. Convert it to string and displaying it on a list view. Up to that bit is fine. When i click on a specific user to covert it to string and attempt to use it in another activity I keep getting the message that the variable is null. – Antonio Apr 01 '15 at 17:34
  • Obviously I understand you have to do that @David I was just wondering if the formatting was okay. – Antonio Apr 01 '15 at 17:37
  • as you said you got a list , you have to put it into intent.putExtra() and get value from getExtra() – Heshan Sandeepa Apr 01 '15 at 17:38
  • i have edit the anwser and "value" is not a keyword, you can put any string you need – Heshan Sandeepa Apr 01 '15 at 17:43