11

I want to show these itens at my ListView

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

I have a method that gets these values.

public static Collection getBookmarks(Context context) {
    Collection bookmarks = new Collection();

    String[] titles  = context.getResources().getStringArray(R.array.bookmark_titles);

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

    return bookmarks;
}

How can I call the method getBookmarks in my main.java to fill the ListView?

I have already created a ListView. It is:

<ListView
    android:id="@+id/my_listView"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:divider="#ECECEC"
    android:dividerHeight="1sp" />

main.java I am trying to do something like this:

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

ArrayList<Bookmark> my_array = BookmarkCollection.getTestBookmarks(context);
adapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, my_array);    
lv.setAdapter(adapter);

2 Answers 2

16

You can create your adapter with a one liner, check out the static method ArrayAdapter createFromResource(Context context, int textArrayResId, int textViewResId)

The first argument will probably be your Activity, the second is R.array.bookmark_titles, and the third is a layout to use.

To clarify based on the comments, the method accepts int, which is exactly what the constants in your generated R class are stored as.

Here's a complete example, assuming this is being called from an Activity:

ArrayAdapter<CharSequence> aa = ArrayAdapter.createFromResource(this, R.array.bookmark_titles, android.R.layout.simple_list_item_1);
myListView.setAdapter(aa);

In this case, android.R.layout.simple_list_item_1 refers to an XML layout that is provided by the Android SDK. You can change this if needed.

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

5 Comments

And then attach the return value from ArrayAdapter.createFromResource() to the ListView with lv.setAdapter().
I will try. I am using the ArrayAdapter() method, but it is not working.
textArrayResId The identifier of the array to use as the data source. textViewResId The identifier of the layout used to create views. What this mean? They are int. Where I get these values? Thank you.
See my updated answer, I attempted to clarify the int parameters.
By this way it works properly. Thank you. But just another question. Inside my Collection class I have the getBookmarks() method. Can I do the same thing using this method?
3

You can do this with an ArrayAdapter and have it use either an Array or a List to give it data

lv.setAdatper(new ArrayAdapter<String>(context, R.layout.some_layout_to_use, R.id.some_textview_in_layout, listData);

1 Comment

Sorry, but I didn't understand. Could you give an example, please? My code, now, contains a way that I am trying to do. Thank you!

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.