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
Create a
ListViewin yourlayout.xmlfile, and retrieve it in java by doing aand populate the listview by doing this:
EDIT
Say for instance that you have a method where you add some object to an
ArrayListAnd in you
onCreatemethodRemeber that this only works for List with one
TextView, if you need more data in your list, you should create your own class which extendsBaseAdapter. An example on this can be found hereNothing can be unclear now? If so, just add a comment.