views:

179

answers:

1

Right now, I get my Models in a nice object-oriented form. In order to bind them to my List, I have to use the listAdapter. Can I only fill this listAdapter with stupid ArrayLists? Because that means, I have to iterate over my ModelCollection and pull all the data out of my Models again.

So, I detach my data from the models and I cant easily refresh the data in my listView, if something chances in the modelCollection (like becoming bigger through new Items / pagination).

Does a more intelligent way exist, than I use right now? Can I bind my ModelCollection more driectly to the listView?

    ModelCollection modelCollection = ModelCategory.findAll();

    /*
     * Prepare Data for Adapter
     */
    ArrayList<String> itemTitles = new ArrayList<String>();

    // Iterate over my ModelCollection and pull all the Data from each Model
    for (int i = 0; i < modelCollection.items.size(); i++) {

      if (modelCollection.items.get(i) != null) {

        // TODO: Cant I bind my Models directly to the List without creating this ArrayList? 
        itemTitles.add(((ModelCategory) modelCollection.items.get(i)).getTitle());       

      }
    }

    /*
     * Create Adapter and bind Array
     */
    adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, itemTitles  );
    setListAdapter(adapter);

One more question aside: How do I have to extend my code, when I want to add small subtitles underneath the title to the list?

A: 

Does a more intelligent way exist, than I use right now?

Write your own custom ListAdapter implementation, perhaps extending BaseAdapter, that works with your existing collection.

One more question aside: How do I have to extend my code, when I want to add small subtitles underneath the title to the list?

Your getView() in your custom ListAdapter implementation can format your rows however you wish, including using multiple pieces of data.

CommonsWare
I recommend this tuturial: http://www.softwarepassion.com/android-series-custom-listview-items-and-adapters/
OneWorld