androidevil
androidevil

Reputation: 9171

How to create a ListView from an ArrayList?

I have, the following class.

public class BkmCollect extends ArrayList<Bkmark> {

    public static BkmCollect getTestBkm(Context context) {
        BkmCollect bookmarks = new BkmCollect ();

        String[] titles  = context.getResources().getStringArray(R.array.bookmark_titles);
        String[] urls    = context.getResources().getStringArray(R.array.bookmark_urls);
        TypedArray icons = context.getResources().obtainTypedArray(R.array.bookmark_icons);

        for (int i = 0; i < titles.length; i ++) {
            bookmarks.add(titles[i], urls[i], icons.getDrawable(i));
        }

        return bookmarks;
    }
}

The "add" method is implemented at this class. And the "Bkmark" class is another class created at my project. I want to create a ListView that will get the data from the BkmCollect.getTestBkm method. How can I do this inside my onCreate method? Thanks!

XML file that contain the fields to be inserted at the ListView:

<resources>
    <string-array name="bookmark_titles">
        <item>Google</item>
        <item>Bing</item>
        <item>Gmail</item>
    </string-array>

    <string-array name="bookmark_urls">
        <item>http://www.google.com</item>
        <item>http://www.bing.com/</item>
        <item>http://www.gmail.com</item>
    </string-array>

    <array name="bookmark_icons">
        <item>@drawable/google</item>
        <item>@drawable/bing</item>
        <item>@drawable/gmail</item>
    </array>
</resources>

I am trying to do something like this:

ListView lv = (ListView) findViewById(R.id.favoritos_listView);
ArrayAdapter adapter;
Context context = getApplicationContext();
ArrayList<Bkmark> my_array = BkmCollect .getTestBkm(context);
adapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, my_array);
lv.setAdapter(adapter);

enter code here

Upvotes: 1

Views: 790

Answers (3)

Tobias Moe Thorstensen
Tobias Moe Thorstensen

Reputation: 8981

Create a ListView in your layout.xml file, and retrieve it in java by doing a

ListView lv = (ListView)findViewById(R.id.your_list_view_id);

and populate the listview by doing this:

ArrayList<String> my_array_list = new ArrayList<String>();
new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, my_array_list);
lv.setAdapter(arrayAdapter); 

EDIT

Say for instance that you have a method where you add some object to an ArrayList

public ArrayList<String> getPopulatedArrayList() {
    ArrayList<String> myList = new ArrayList<String>();
    myList.add("One");
    myList.add("Two");
    myList.add("Three");

    return myList; 
}

And in you onCreate method

ArrayAdapter adapter;
ListView lv;
lv = (ListView)findViewById(R.id.my_list);

ArrayList<String> list = getPopulatedArrayList(); 
adapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, list);
lv.setAdapter(adapter); 

Remeber that this only works for List with one TextView, if you need more data in your list, you should create your own class which extends BaseAdapter. An example on this can be found here

Nothing can be unclear now? If so, just add a comment.

Upvotes: 1

Jscti
Jscti

Reputation: 14450

You should use A ListView associated to an ArrayAdapter

See http://www.ezzylearning.com/tutorial.aspx?tid=1763429 for a good tutorial (with performance tweaks)

Upvotes: 1

Joe Plante
Joe Plante

Reputation: 6368

A viewlist? Do you mean you want to have a bunch of views added to the screen?

If so, there's functions in every layout type to do this. In LinearLayout, there's .addView(). If you want to do something fancier, you can create LinearLayout.LayoutParams and adjust those. Many Android programmers don't make views manually.

If the views aren't going to change a lot or if you can hide parts of them, you can also consider a ListView and/or ListAdapter.

Upvotes: 0

Related Questions