I have a list with RSS feed. If I write this:
List<String> titles = new ArrayList<String>(messa开发者_StackOverflowges.size());
for (Message msg : messages){
titles.add(msg.getDate());
titles.add(msg.getTitle());
I get the title and the date in separate strings.
I tried to change it to:
titles.add(msg.getDate()+msg.getTitle());
but this returns date and titles with no space.
I would like to have date and below it the title, something like:
Thu 27 Jan 2011
Bla bla bla..
How could I do it? Also, may I have another text color or size in the date and other in the title?
An alternative approach, although a little more complex, is to use a layout xml file to specify the layout of the row and an adapter to bind each specific piece of info of each message (like the date and title) to a specific widget in the row layout.
The reason this may be better is that this way is that:
- the layout info (stacking date over the message) stays in the XML and out of the code
- it also makes it easier to use more complex layouts in the future (how about showing the author too? categories?),
- it makes it easier to modify the layout in the future. and
- it allows you to use more complex widgets and widget features, like specifying a scrolling marque text view for when descriptions are long
This example uses SimpleAdapter
to bind a List
of Map
ped data to the complex rows of a ListView.
http://www.vbsteven.be/blog/using-the-simpleadapter-with-a-listview-in-android/
// get messages from service
ArrayList smsMessages = service.getSmsCalls();
// initialize the List of Maps
List<map> list = new ArrayList<map>();
// iterate over all messages
// create a map for each message
// fill the map with data
for (Call c: smsMessages) {
Map map = new HashMap();
map.put("number", c.getTo());
map.put("date", DateParser.getTimeString(c.getBegin()));
map.put("price", "€ " + c.getPrice());
list.add(map);
}
// the from array specifies which keys from the map
// we want to view in our ListView
String[] from = {"number", "date", "price"};
// the to array specifies the TextViews from the xml layout
// on which we want to display the values defined in the from array
int[] to = {R.id.callog_detail_sms_number, R.id.callog_detail_sms_date, R.id.callog_detail_sms_price};
// get a reference to the ListView
ListView lv = (ListView)findViewById(R.id.callog_detail_listview);
// create the adapter and assign it to the listview
SimpleAdapter adapter = new SimpleAdapter(this.getApplicationContext(), list, R.layout.callog_detail_sms, from, to);
lv.setAdapter(adapter);
Unfortunately, the article doesn't show the layout xml, but its going to be similar to the next example below. If you really only have 2 fields, a LinearLayout is fine. More than 2 in a more complex layout, start looking at RelativeLayout:
http://android-developers.blogspot.com/2009/02/android-layout-tricks-1.html
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="?android:attr/listPreferredItemHeight"
android:padding="6dip">
<ImageView
android:id="@+id/icon"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_alignParentTop="true"
android:layout_alignParentBottom="true"
android:layout_marginRight="6dip"
android:src="@drawable/icon" />
<TextView
android:id="@+id/secondLine"
android:layout_width="fill_parent"
android:layout_height="26dip"
android:layout_toRightOf="@id/icon"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:singleLine="true"
android:ellipsize="marquee"
android:text="Simple application that shows how to use RelativeLayout" />
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/icon"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_above="@id/secondLine"
android:layout_alignWithParentIfMissing="true"
android:gravity="center_vertical"
android:text="My Application" />
</RelativeLayout>
titles.add(msg.getDate()+" "+msg.getTitle());
... or, if you want to have an ENTER between those two... you will have to wrap it in a HTML string:
titles.add(msg.getDate()+"<br/>"+msg.getTitle());
// and somewhere else, when you are going to draw it:
textView.setText(Html.fromHtml(title.get(position)));
精彩评论