さて、今回はリストビューのカスタマイズについて少し触れてみます。
こんな感じのリストビューの使い方です。
まずは、layoutを2つ使っています。
こちらがリストビューの入ったxmlです。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android
"
android:layout_width="fill_parent" android:layout_height="fill_parent"
android:background="@drawable/washiback">
<LinearLayout android:id="@+id/linearLayout2"
android:layout_height="fill_parent" android:orientation="vertical"
android:layout_width="fill_parent">
<RelativeLayout android:id="@+id/relativeLayout1"
android:layout_height="wrap_content" android:layout_width="fill_parent"
android:background="@drawable/maplewood">
<ImageButton android:layout_height="wrap_content"
android:layout_width="wrap_content" android:layout_alignParentRight="true"
android:id="@+id/addNoteBtn" android:background="@drawable/ic_addnote"></ImageButton>
</RelativeLayout>
<ListView android:layout_width="fill_parent" android:id="@+id/android:list"
android:layout_weight="1" android:layout_height="fill_parent"
android:background="#00000000">
</ListView>
<TextView android:layout_width="wrap_content" android:id="@+id/android:empty"
android:text="@string/no_notes" android:layout_height="wrap_content">
</TextView>
</LinearLayout>
</LinearLayout>
こちらがリストに表示するためのxmlです。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android
"
android:orientation="horizontal" android:gravity="right"
android:layout_height="wrap_content" android:layout_width="fill_parent"
android:background="@color/list_item">
<LinearLayout android:layout_width="wrap_content"
android:layout_weight="1" android:orientation="vertical" android:id="@+id/linearLayout1"
android:layout_height="wrap_content">
<LinearLayout android:layout_width="fill_parent"
android:layout_height="wrap_content" android:id="@+id/linearLayout2"
android:layout_weight="1">
<TextView android:layout_height="wrap_content"
android:layout_weight="1" android:padding="10dip" android:maxLines="1"
android:layout_width="wrap_content" android:textColor="#000000"
android:id="@+id/text1" android:textSize="20sp"></TextView>
<TextView android:layout_height="wrap_content"
android:layout_width="wrap_content" android:textColor="#000000"
android:textAppearance="?android:attr/textAppearanceSmall"
android:id="@+id/hiduke"></TextView>
</LinearLayout>
<TextView android:layout_height="wrap_content"
android:layout_weight="1" android:layout_width="fill_parent"
android:id="@+id/bodyText" android:maxLines="1"></TextView>
</LinearLayout>
<ImageView android:src="@drawable/green_arrow"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:layout_gravity="center_vertical" />
</LinearLayout>
このように、リストビューをカスタマイズする時には
layoutのxmlを2つ作成し、ひとつはリストの受け皿として、もう一つはリストの中身として
使う方法がよく使われているようです。
便利だ・・・
それで、xml2つ作ったは良いけどもどうやって結合するんだ!!?
ってところで躓きました。
えー。基本的にはリストの中身として作成したxmlをアダプターに引数としてセットして、そのアダプターをListViewにセットしてあげれば良いだけだったりします
こんな感じで。
まずはアダプターを作成します。
SimpleAdapter adapter;
アダプターにリストの中身として作成したxmlをセットします。
adapter = new SimpleAdapter(this, myData, R.layout.notes_row, new String[] { mDbHelper.KEY_TITLE,
mDbHelper.KEY_BODY, mDbHelper.KEY_DATE }, new int[] { R.id.text1, R.id.bodyText, R.id.hiduke });
SimpleAdapter(Context context, List<? extends Map<String, ?>> data, int resource, String[] from, int[] to)
context: The context where the View associated with this SimpleAdapter is running
ここでは、コンテキストは自分自身を渡してあげれば良いのでthisでおっけーです。
data: A List of Maps. Each entry in the List corresponds to one row in the list. The Maps contain the data for each row, and should include all the entries specified in "from"
えー、ここではmapを使います。今回はこのような形でmapをいれるarraylistを作成しています。
List<HashMap<String, Object>> myData = new ArrayList<HashMap<String, Object>>();
で、mapはキーと値で作成されていますから、そのキーを後のfromで配列として渡してあげます。
fromで渡すキーはdataに格納されていなければいけないよってことですね。
resource: Resource identifier of a view layout that defines the views for this list item. The layout file should include at least those named views defined in "to"
ここのresourceにR.layout.notes_rowですね。リストの中身として作成したxmlです。
で、このxmlの中には、後ろのtoで配列として渡してあげるidを持ったviewが入ってないといけないよってことです。
fromとdata
toとresource
この2つは関係があるんですね。
from: A list of column names that will be added to the Map associated with each item.
上で説明したとおり、使いたいキーの配列ですね。
to: The views that should display column in the "from" parameter. These should all be TextViews. The first N views in this list are given the values of the first N columns in the from parameter.
fromで設定したキーの値を表示するためのviewのidですね。
textViewを使うようですね。
上で、fromとdata、toとresourceは関係があると書きましたが、fromとtoも関係があるんですね。
正確に言うと・・・fromで並べた順番とtoの並び順に関係があります。
今回の例で見ると
new SimpleAdapter(this, myData, R.layout.notes_row, new String[] { mDbHelper.KEY_TITLE,
mDbHelper.KEY_BODY, mDbHelper.KEY_DATE }, new int[] { R.id.text1, R.id.bodyText, R.id.hiduke });
KEY_TITLEの値がtext1に入って
KEY_BODYの値がbodyTextに入って
KEY_DATEの値がhidukeに入ります。
ちなみに、このアクティビティはonCreateでリストの受け皿のxmlをセットしてます。
setContentView(R.layout.notes_list);
ですね。受け皿は普通にセットして、アダプターに色々セットしてあげる。。。。
おっと、アダプターをリストにセットしてませんね。
setListAdapter(adapter);
はい。これでオッケーです。
こんな感じでカスタマイズをしました。
参考になれば幸いです。
方法はいろいろあるみたいなんで、これも一つということで。










