0

I have a simple question, I know you can use:

setListAdapter(new ArrayAdapter<String> (getActivity(), 
                R.layout.your_layout,
                R.id.your_text,
                stringArray));  

inside a ListFragment on your onActivityCreated() method. This works well for me so far, but only covers one item. For instance, say if in the the layout, I had two TextViews: sms_text, and sms_timestamp; both to be filled by smsTextArray [] and smsTimeStampArray [] respectively. How would that call be? I tried:

setListAdapter(new ArrayAdapter<String> (getActivity(), 
                R.layout.tweet_list_item,
                R.id.sms_text,
                smsTextArray,
                R.id.sms_timestamp,
                smsTimeStampArray,
                ));     

That did not work :(

I also tried calling setListAdapter() twice in the same onActivityCreated(), but although that worked, it only displayed the second call of setListAdapter() in the list items.

I also tried creating my own Adapter but I kind of got lost in that mess. Can you guys point me in the right direction? I do not need the exact code, just a link to a library I could read, or perhaps it is a simple as just a code call. Who knows, maybe I am just using the wrong parameters, any ideas?

daniel_c05
  • 11,438
  • 17
  • 60
  • 78
  • I think this happens to cover exactly what you're looking for. http://stackoverflow.com/questions/8166497/custom-adaptor-for-list-view – farkerhaiku Sep 10 '12 at 00:28
  • What I wonder is, and I had seen a similar tutorial, how can I go around that without an actual ListView, but instead a FragmentList? They are slightly different, or could I just add a ListView to that Fragment? – daniel_c05 Sep 10 '12 at 00:31
  • @daniel_c05 a ListFragment uses a ListView internally, so there is not really a major difference. The only thing you have to change is: You have to use `setListAdapter(yourAdapterInstance)` instead of `yourListView.setAdapter(yourAdapterInstance)` to connect the adapter with the view when you use a ListFragment. That's about it, extending one of the adapter classes to build your own is the same in both cases. –  Sep 10 '12 at 01:06

1 Answers1

1

You can't use a simple ArrayAdapter and display more than one piece of data. An adapter maps one element in the list to one element in the listView. To have data come from two different pieces you need to either create your own adapter (simpler than it sounds). Or alternatively you can use a list of Maps in a SimpleAdapter (see this for an example).

Personally, I'd recommend going for the custom Adapter. You're going to have to code one eventually, and there's no time like the present. Plus, you're code will be more readable and obvious.

dmon
  • 30,048
  • 8
  • 87
  • 96