Rushabh
Rushabh

Reputation: 385

Adapt ArrayList into ListView

I have a XML File and I had parsed the data into the textView successfully now I want to bind that data into the ArrayList or List and display it in ListView.

But I don't know how to bind arraylist data into the ListView. I have added all data into the arraylist successfully as mentioned in the below code.

List al = new ArrayList();
al.add(parser.getAttributeValue(null, "firstnames"));

Kindly please help me with the code syntax for the above issue.

Regards .

Thanks in advace

Upvotes: 0

Views: 2689

Answers (2)

G_S
G_S

Reputation: 7110

please have a look at the sample at http://codinglookseasy.blogspot.in/2012/07/android-list-view-sample.html Instead of this

aa = new ArrayAdapter(this,
android.R.layout.simple_list_item_1, months);
setListAdapter(aa);

use this in your case

aa = new ArrayAdapter(this,
android.R.layout.simple_list_item_1, al);
setListAdapter(aa);

Upvotes: 2

Sam
Sam

Reputation: 86958

You need to use an Adapter to bind a List to a ListView, like this:

List<String> list = new ArrayList<String>();
// add data to list

ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, list);
ListView listView = (ListView) findViewById(R.id.listView);
listView.setAdapter(adapter);

Notice that the subtype of List (which is String) matches the subtype of the ArrayAdapter (also String). The layout android.R.layout.simple_list_item_1 defines how the String is displayed in every row. You can look up the specifics of this layout in your SDK, if you want you can also use you own layout. Hope that helps, good luck learning Android!

Upvotes: 1

Related Questions