Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Populating a ListView using an ArrayList?

My Android app needs to populate the ListView using the data from an ArrayList.

I have trouble doing this. Can someone please help me with the code?

like image 930
kAnNaN Avatar asked Feb 21 '11 20:02

kAnNaN


People also ask

How do you populate a ListView?

You need to do it through an ArrayAdapter which will adapt your ArrayList (or any other collection) to your items in your layout (ListView, Spinner etc.). This is what the Android developer guide says: A ListAdapter that manages a ListView backed by an array of arbitrary objects.

How do you populate an ArrayList?

Populating the ArrayListUse the add method to append a value to the ArrayList : dynamicArray. add(10); dynamicArray. add(12); dynamicArray.

Can you cast a list to an ArrayList in Java?

Convert list To ArrayList In Java. ArrayList implements the List interface. If you want to convert a List to its implementation like ArrayList, then you can do so using the addAll method of the List interface.

Is ArrayAdapter used to handle ArrayList?

The simplest adapter to use is called an ArrayAdapter because the adapter converts an ArrayList of objects into View items loaded into the ListView container.


2 Answers

You need to do it through an ArrayAdapter which will adapt your ArrayList (or any other collection) to your items in your layout (ListView, Spinner etc.).

This is what the Android developer guide says:

A ListAdapter that manages a ListView backed by an array of arbitrary objects. By default this class expects that the provided resource id references a single TextView. If you want to use a more complex layout, use the constructors that also takes a field id. That field id should reference a TextView in the larger layout resource.

However the TextView is referenced, it will be filled with the toString() of each object in the array. You can add lists or arrays of custom objects. Override the toString() method of your objects to determine what text will be displayed for the item in the list.

To use something other than TextViews for the array display, for instance ImageViews, or to have some of data besides toString() results fill the views, override getView(int, View, ViewGroup) to return the type of view you want.

So your code should look like:

public class YourActivity extends Activity {      private ListView lv;      public void onCreate(Bundle saveInstanceState) {          setContentView(R.layout.your_layout);           lv = (ListView) findViewById(R.id.your_list_view_id);           // Instanciating an array list (you don't need to do this,           // you already have yours).          List<String> your_array_list = new ArrayList<String>();          your_array_list.add("foo");          your_array_list.add("bar");           // This is the array adapter, it takes the context of the activity as a           // first parameter, the type of list view as a second parameter and your           // array as a third parameter.          ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(                  this,                   android.R.layout.simple_list_item_1,                  your_array_list );           lv.setAdapter(arrayAdapter);      } } 
like image 121
Amokrane Chentir Avatar answered Nov 03 '22 00:11

Amokrane Chentir


tutorial

Also look up ArrayAdapter interface:

ArrayAdapter(Context context, int textViewResourceId, List<T> objects) 
like image 24
Sonny Saluja Avatar answered Nov 03 '22 00:11

Sonny Saluja