5

after creating some menus with simple_list_item_1 (which worked very fine) I tried to replace this by simple_list_item_2, but my system throws around with exceptions...

Now I'm wondering how to create such a two-different-sized-line-entry for my list...is there any trap for beginners? Can someone please help me fixing my (small!?) problem?

My code looks like this:

ListAdapter listAdapter = new ArrayAdapter<String>(this,
        android.R.layout.simple_list_item_2, fileNames);
setListAdapter(listAdapter);

My String[] fileNames stores all strings to display in ascending order (does this matter for the functionality?!)

After unsuccessfully searching in some forums I now hope that someone of you can give me an useful suggestion.

nice greetings, poeschlorn

poeschlorn
  • 12,230
  • 17
  • 54
  • 65

1 Answers1

10

simple_list_item_2 is different, instead of just a TextView it contains a TwoLineListItem containing two TextViews. ArrayAdapter is not going to work here, because the constructor you're using expects only a TextView; just look at the constructors. Instead you'll either have to create a custom adapter or use one that supports it like SimpleCursorAdapter or (I think) SimpleAdapter. This guy has a somewhat hacky solution that might work for you.

Community
  • 1
  • 1
Daniel Waechter
  • 2,574
  • 2
  • 21
  • 21
  • hi, thanks for your response :) I'm going to try this out and tell you if/how it works – poeschlorn Jun 08 '10 at 08:30
  • hi again, I tried it out and it worked very well...the only thing I don't understand is, why is there a Map (or HashMap) needed? – poeschlorn Jun 08 '10 at 10:37
  • 2
    I guess because the intended use is really to pull data from things that are already in key-value pairs, so it makes sense for that purpose. No reason there couldn't be, say, a 2dArrayAdapter or something, though. By the way, here's another good sample usage: http://stackoverflow.com/questions/2227154/how-do-i-fill-a-listview-in-android-with-xml-or-json-data/2227377#2227377 – Daniel Waechter Jun 08 '10 at 12:31
  • Hi, is it possible to give the simple_list_item_2 - objects an alternating background color? – poeschlorn Jun 10 '10 at 13:45
  • SPECIFY IN THE LAYOUT.simple_list_item_2 IS AVAILABLE in the SDK. Copy it. and modify it. Refer your adapter to this XML – Abhishek Susarla Oct 14 '11 at 08:59
  • An example of a custom Adapter (which extends ArrayAdapter) : http://stackoverflow.com/questions/4435645/proper-usage-of-twolinelistitem-android/12954944#12954944. You can replace the layout with a custom one. – Solostaran14 Oct 18 '12 at 14:01