Multicolumn ListView in Android
출처 :
http://www.heikkitoivonen.net/blog/2009/02/15/multicolumn-listview-in-android/ Ever since I started programming on the Android platform, I have been wondering when the SDK would include a ready-made multicolumn
ListView
(or listbox as it is often called in other frameworks). One could of course construct such a thing by slapping regular ListViews side by side and then painfully adding code to keep them all in sync when scrolled and so on. It felt such a hack that I used GridView
instead. GridView
is not really a great list, though, because the cells will all be formatted the same. There are other issues besides that, but for me that was the main one.
I finally saw a blog post which customized ListView
to put two TextView
s vertically into one line. It was pretty simple going from there to one that would display three TextView
s side-by-side. The main layout XML file could define ListView
something like this:
Here is the XML for each row, main point being that we put three TextView
s in LinearLayout
with horizontal
orientation:
Notice how you can format each TextView
separately. There is no column separator, but something decent should not be too hard to whip up if desired. My understanding is that the screen width is supposed to be 160 dip
, but for some reason I had to use width values that actually add up to more than that, as well as using layout weight to grow some fields proportionally so that when you switch to landscape mode things are still nicely aligned. I would have expected specifying widths in dip would have automatically done that.
Here is how you could populate that ListView
with data:
The main point here is that the SimpleAdapter
requires the data to be in a List
, where each entry is a Map
. In my case, I simulate the columns by putting in three entries into each of the maps, and the maps each have the same keys. The adapter then maps the key values (like "train"
) to the corresponding TextView
(R.id.TRAIN_CELL
).
Putting the above pieces of code into Caltroid produces results that look like this:
There is quite a bit of work to create the data structure for the adapter in this way, so for anything involving more than a trivial amount of data this is probably going to be too slow. With more data, a database is almost certainly involved and theSimpleCursorAdapter
works almost exactly like SimpleAdapter
, except that SimpleCursorAdapter
maps column names from the query results to the appropriate views.
Similar Posts:
'Android > Tip&Tech' 카테고리의 다른 글
Export an Android SQLite db to an XML file on the SD Card (0) | 2012.01.20 |
---|---|
리스트뷰에 텍스트 먼저 이미지 늦게~ (0) | 2012.01.18 |
ProgressBar 커스터 이미징하기 (0) | 2012.01.17 |
cursor 관련 팁 블로그 (0) | 2012.01.11 |
Separating Lists with Headers in Android 0.9 (1) | 2012.01.01 |