I am new to Android development. I am unable to pass an ArrayList from a class to my main activity. The following code snippet retrieves the elements from an RSS feed and stores them to an ArrayList. I would like to access this ArrayList in my activity in order to display the titles. I am working from the Android tutorial on ListViews.
DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
URL u = new URL("http://sports.espn.go.com/espn/rss/news");
Document doc = builder.parse(u.openStream());
NodeList nodes = doc.getElementsByTagName("item");
ArrayList<String> titles = new ArrayList<String>();
for(int i=0;i<nodes.getLength();i++)
{
Element element = (Element)nodes.item(i);
titles.add(getElementValue(element,"title"));
}
Bundle value = new Bundle();
value.putStringArrayList("titles", titles);
The above code works and stores the titles into an ArrayList. However, I am unable to access the list by using a Bundle in the following code from the Activity.
public class ExampleActivity extends ListActivity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setListAdapter(new ArrayAdapter<String>(this, R.layout.list_item, titles));
ListView lv = getListView();
lv.setTextFilterEnabled(true);
lv.setOnItemClickListener(new OnItemClickListener()
{
public void onItemClick(AdapterView<?> parent, View view, int position, long id)
{
// When clicked, show a toast with the TextView text
Toast.makeText(getApplicationContext(), ((TextView) view).getText(),
Toast.LENGTH_SHORT).show();
}
});
}
String[] titles = Bundle.getStringArray("titles");
}