0

I have a listview which is simply populated this way:

setListAdapter(new ArrayAdapter<String>(this,
                android.R.layout.simple_list_item_multiple_choice, aux))

'aux' it's static String[] aux = null;which is populated via Web Service.

I made a theme and tested with static listview elements, file lista.xml -> you can see it here (if you look in the bottom you will see I put "list item 1, list item 2", so I need to populate those values dinamically...)

My question is, how can I apply the theme to my listadapter?

I believe I start with :

setListAdapter(new ArrayAdapter(this, R.layout.lista, aux))

But I have two problems.

(1) I dont know how to work with XML and populating from java (setlistadapter) (2) I believe I will lose checkboxes.

However I appreciate if anyone can help in (1) first :) I really don't know what am doing now!

Tiago
  • 1,116
  • 5
  • 25
  • 39

1 Answers1

2
setListAdapter(new ArrayAdapter(this, R.layout.lista, aux))

is the start, you are right, but when you want a custom ListItem Layout you have to write a custom ListAdapter. You extend ArrayAdapter<YourItemClass> for example. There you override the method getView() and in this method you populate your custom Layout with your Values of your Aux Objects.

for an ArrayAdapter you have to set a id in your layout for your listview items

<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent" android:layout_height="fill_parent"
    android:id="@+id/textview" android:layout_weight="1">

</TextView>

with id/textview you say your listAdapter to render into this View.

edit: oh and you have to learn and obey the holder concept of Lists. here: How to load the Listview "smoothly" in android

Community
  • 1
  • 1
Michele
  • 6,126
  • 2
  • 41
  • 45
  • Thanks for your help, but I have a problem... Based on your suggestion, I quickly replace the android:text for android:id (as you state), and had to replace the adapter for setListAdapter(new ArrayAdapter(this,R.layout.lista, R.id.textview,aux)); The thing is: for every listview element is creating all other elements of the layout, so it's all messed up. How can I solve this? – Tiago Sep 11 '11 at 19:36
  • Perhaps your underlying modeldata is a "all in one" object, so you have to define the getItem(int) method in your Customized ListAdapter. In this method you define how to get one item from your ItemObject(aux). – Michele Sep 11 '11 at 20:19