I want to populate a listview from sql database. Most examples use simple_list_item_1 which only show one column. Where can I get example of multiple columns listview?
Asked
Active
Viewed 2,936 times
1

ErasmoOliveira
- 1,416
- 2
- 20
- 40

user2911996
- 63
- 1
- 2
- 10
-
you want to have multiple items in the row or multiple columns? – Fahim Mar 04 '15 at 13:48
-
Create your own list_item layout, and add as many TextViews there as you want – Rick Sanchez Mar 04 '15 at 13:57
-
yes, multiple items for the fields. is there difference between multiple columns? it would be good if the listview row can be scrolled horizontally too when there are too many fields to show – user2911996 Mar 05 '15 at 01:51
2 Answers
0
You have to use a custom Adapter
with a custom layout. Your adapter class has to extend ArrayAdapter<?>
in which your will replace the ?
with your sql database object class (example: class MyAdapter extends ArrayAdapter<MyObject>
).
Inside this class you have to set the items of the adapter to the values of your database items.
Here you can find an example and explanation of custom adapters: https://github.com/codepath/android_guides/wiki/Using-an-ArrayAdapter-with-ListView
And here the official documentation: http://developer.android.com/reference/android/widget/ArrayAdapter.html

Giorgio Antonioli
- 15,771
- 10
- 45
- 70
-
The sample code in the webpage of custom adapters make use of android studio which I cannot compile at the moment. – user2911996 Mar 04 '15 at 15:10
0
If you just need to display text then you can use a HashMap like in this tutorial
Otherwise, you're best bet would be to create a custom adapter, see this SO example

Community
- 1
- 1

peterrhodesdev
- 241
- 4
- 14
-
I will want to display from a sqlite database. And later I will want to insert item to the listview when new row is inserted to it too. – user2911996 Mar 04 '15 at 15:11