0

I've got a listview and I want to start a new activity when clicking on an item in that listview. How do I access the variables of that item from the new activity?

Edit: My listview is populated via an Asynctask and an RSS Feed. I want to access one of the items in the array and read out its url.

TemporaryName
  • 487
  • 5
  • 16

1 Answers1

1

Method 1:

you can store it in an object of a custom class (which implements Serilizable) on click of listview and pass that object via intent. Make sure :

  //to pass :
  intent.putExtra("MyClass", obj);  

 // to retrieve object in second Activity
getIntent().getSerializableExtra("MyClass");

Method 2(not recommended):

Create a static custom class and store the variables in onclick to that class instance.

for Static Class A {...}

//To pass
A.variable = "stored value";

//retrieve
 String s = A.variable;

Method 3

Use SharePreferences. Recommended only if you need to remember what user clicked last time he was using the app.

Karan
  • 2,120
  • 15
  • 27
  • I worded my question wrongly, I know how to do that and transfered the list item position. But I have to access the array which fills the listview. – TemporaryName Apr 04 '15 at 13:52
  • you can even store/retrieve arrays in the same above way. – Karan Apr 04 '15 at 13:56
  • I figured out a different way to do it. I made the array public in the class with the AsyncTask and Intent, then I was able to transfer the variable. Thank you anyway! – TemporaryName Apr 04 '15 at 13:59