1

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

0

3 Answers 3

1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

The ListView was already created at my layout.xml and it is retrieved in my java. And I make your sugestion, but it does not work, yet.
The point is that I have an XML file that contains some fields, and these are the fields that must appear at the ListView. I've edited my code. Now it contains the XML and the solution that I've writed, but it is not working properly.
1

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)

Comments

0

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.

1 Comment

Sorry! I mean ListView. I've changed the question.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.