54
Adapter & ListView 2010.10.23

Adapter & ListView & ExpandalbeListView

Embed Size (px)

Citation preview

Page 1: Adapter & ListView & ExpandalbeListView

Adapter & ListView

2010.10.23

Page 2: Adapter & ListView & ExpandalbeListView

agenda• Adapter - one of the GOF design pattern –• ListView• ExpadableListView

Page 3: Adapter & ListView & ExpandalbeListView

Adapter● GoF (Gang of Four) によって定義されたデザイン

パターンの一つ● 既存のクラスに修正不要● インタフェースを変更可能● ListView の adapter は委譲のパターン

Page 4: Adapter & ListView & ExpandalbeListView

ListView● 1行が1つの View● チェックボタンやラジオボタンを付けられる

Page 5: Adapter & ListView & ExpandalbeListView

Adapters for List• interface

– ListAdapter– SpinnerAdapter– WarrerListAdapter

• abstruct class– BaseAdapter– CursorAdapter– ResourceCursorAdapter

• class– ArrayAdapter– HeaderViewListAdapter– SimpleAdapter– SimpleCursorAdapter

Page 6: Adapter & ListView & ExpandalbeListView

SimpleAdapter

Page 7: Adapter & ListView & ExpandalbeListView

SimpleAdapter• What is

– static data を XML ファイルで定義された TextView や ImageView に紐づけるための簡単な adapter

• What views can use?– TextView– ImageView– ChackedTextView

• Way to bind1. SimpleAdapter.ViewBinder が

セットされていたら、このクラスの setViewValue メソッドが呼ばれる

2. 1. が false の場合、setTextView が呼ばれる

3. 2. が false の場合、setImageView が呼ばれる

* setViewBinder() で独自の SimpleAdapter.ViewBinder をセットすれば、TextView, ImageView 以外の View を使える

Page 8: Adapter & ListView & ExpandalbeListView

SimpleAdapter• Constructors

– parameters• context :

SimpleAdapter が紐付く View が動作する Context this, getApplicationContext() など

• data : Map の List. List の 1 エントリ が ListView の 1行に相当する

Map は from で指定した各文字列がキーになったdata を持ってなければならない

• resource : ListView の 1行のレイアウト XML ファイルの Resource ID

このレイアウトには、 to で指定した各 Id の View が含まれていなければならない

• from : 各 View に紐づける Map data のキー

• to : from パラメータの各 data を紐づける View の Id

SimpleAdapter(Context context, List<? extends Map<String, ?>> data, int resource, String[] from, int[] to)

Page 9: Adapter & ListView & ExpandalbeListView

SimpleAdapter• Constructors

SimpleAdapter( this, data, R.layout.list_item, from, to)

res/layout/list_item.xml<LinearLayout android:orientation=“horizontal” > <TextView android:id=“@+id/textview1” /> <TextView android:id=“@+id/textview2” /></LinearLayout>

int[] to = {R.id.textview1, R.id.textview2};

textview2

textview1

Page 10: Adapter & ListView & ExpandalbeListView

SimpleAdapter• Constructors

SimpleAdapter( this, data, R.layout.list_item, from, to)

static final List<HashMap<String, String> data = Arrays.asList( new HashMap<String, String>() { put(“Title”, “ 吾輩は猫である”); put(“Author”, “夏目漱石”); }, … new HashMap<String, String>() { put(“Title”, “ 走れメロス”); put(“Author”, “太宰治”); });

String[] from = {“Title”, “Author”};

textview2

textview1

Page 11: Adapter & ListView & ExpandalbeListView

SimpleAdapter• Constructors

SimpleAdapter( this, data, R.layout.list_item, from, to)

textview2

textview1

Page 12: Adapter & ListView & ExpandalbeListView

SimpleAdapter• Constructors

SimpleAdapter( this, data, R.layout.list_item, from, to)

res/layout/list_item.xml<LinearLayout android:orientation=“horizontal” > <ImageView android:id=“@+id/imageview1” /> <ImageView android:id=“@+id/imageview2” /></LinearLayout>

int[] to = {R.id.imageview1, R.id.imageview2};

imageview2

imageview1

Page 13: Adapter & ListView & ExpandalbeListView

SimpleAdapter• Constructors

static final List<HashMap<String, Integer> data = Arrays.asList( new HashMap<String, Integer>() { put(“Icon1”, R.drawable.alermclock); put(“Icon2”, R.id.bookopen); }, … new HashMap<String, Integer>() { put(“Icon1”, R.id.calculator); put(“Icon2”, R.id.camera); });

String[] from = {“Icon1”, “Icon2”};

imageview2

imageview1

SimpleAdapter( this, data, R.layout.list_item, from, to)

Page 14: Adapter & ListView & ExpandalbeListView

SimpleAdapter• Constructors

imageview2

imageview1

SimpleAdapter( this, data, R.layout.list_item, from, to)

Page 15: Adapter & ListView & ExpandalbeListView

SimpleAdapter● Notes

● 異なるデータ型を一緒に含むことができない● 文字列(String)と resource ID(Integer)を一緒にできない● データ型を Object にすればできます!!

imageview

textview

static final List<HashMap<String, Object> data = Arrays.asList( new HashMap<String, String>() { put(“Icon”, R.id.soseki); put(“Author”, “夏目漱石”); }, … new HashMap<String, String>() { put(“Icon”, R.id.dazai); put(“Author”, “太宰治”); });

Page 16: Adapter & ListView & ExpandalbeListView

SimpleCursorAdapter

Page 17: Adapter & ListView & ExpandalbeListView

SimpleCursorAdapter• What is

– Cursor から取得したカラムを XML ファイルで定義された TextView や ImageView に紐づけるための簡単な adapter

• What views can use?– TextView– ImageView– ChackedTextView

• Way to bind1. SimpleAdapter.ViewBinder が

セットされていたら、このクラスの setViewValue メソッドが呼ばれる

2. 1. が false の場合、setTextView が呼ばれる

3. 2. が false の場合、setImageView が呼ばれる

* setViewBinder() で独自の SimpleAdapter.ViewBinder をセットすれば、TextView, ImageView 以外の View を使える

Page 18: Adapter & ListView & ExpandalbeListView

SimpleCursorAdapter• Constructors

– parameters• context :

SimpleAdapter が紐付く View が動作する Context this, getApplicationContext() など

• c : データベースの cursor

cursor がまだ使えないときは null でも OK• resource :

ListView の 1行のレイアウト XML ファイルの Resource IDこのレイアウトには、 to で指定した各 Id の View が

含まれていなければならない• from :

各 View に紐づける cursor data の column name cursor がまだ使えないときは null でも OK

• to : from パラメータの各 data を紐づける View の Id

SimpleAdapter( Context context, int resource, Cursor c, String[] from, int[] to)

Page 19: Adapter & ListView & ExpandalbeListView

SimpleCursorAdapter● Notes

● List<? extends Map<String, ?>> data が Cursor c になっただけで、 SimpleAdapter と同じ

● もちろん、異なるデータ型を含むことができる

imageview

textview

できる! ^ ^

Page 20: Adapter & ListView & ExpandalbeListView

ArrayAdapter

Page 21: Adapter & ListView & ExpandalbeListView

ArrayAdapter• What is

– 任意の Object の配列を ListView に紐づけるための adapter

• What views can use?– TextView (デフォルト)– 任意の View

• Way to bind1. デフォルトは 1行 = TextView

2. id を指定すれば、レイアウト内の任意の TextView に紐づけ可能

3. 任意の Object の配列を紐づける場合、toString() を override すれば、その戻り値が TextView に入る

4. ImageView など toString() を戻り値がふさわしくない場合、getView(int, View, ViewGroup) を override する

Page 22: Adapter & ListView & ExpandalbeListView

ArrayAdapter• Constructors

– parameters• context :

ArrayAdapter が紐付く View が動作する Context this, getApplicationContext() など

• resource: TextView が1つだけ含まれるレイアウトファイルの resource Id

レイアウトファイルの root view が TextView でなければならない

ArrayAdapter(Context context, int resource)

Page 23: Adapter & ListView & ExpandalbeListView

ArrayAdapter• Constructors

ArrayAdapter(this, R.layout.list_item)

NoteList の 1行は TextView になる

紐づけるデータは add(T object) で追加する

textview

Page 24: Adapter & ListView & ExpandalbeListView

ArrayAdapter• Constructors

– parameters• context :

ArrayAdapter が紐付く View が動作する Context this, getApplicationContext() など

• resource : ListView の 1行のレイアウト XML ファイルの Resource ID

このレイアウトには、 textViewResourceId で指定した Id の TextView が含まれていなければならない

• textViewResourceId : TextView の resource Id

ArrayAdapter(Context context, int resource, int textViewResourceId)

Page 25: Adapter & ListView & ExpandalbeListView

ArrayAdapter• Constructors

ArrayAdapter(this, R.layout.list_item, R.id.textview)

NoteList の 1行のどこかに adapter と紐付く TextView が含まれる

紐づけるデータは add(T object) で追加する

textview

Page 26: Adapter & ListView & ExpandalbeListView

ArrayAdapter• Constructors

– parameters• context :

ArrayAdapter が紐付く View が動作する Context this, getApplicationContext() など

• resource : TextView が1つだけ含まれるレイアウトファイルの resource Id

レイアウトファイルの root view が TextView でなければならない• Object :

各 View に紐づける T クラス (String, 独自クラスなど) の配列toString() の戻り値が TextView に入れられる

ArrayAdapter(Context context, int resource, T[] Object)

Page 27: Adapter & ListView & ExpandalbeListView

ArrayAdapter• Constructors

ArrayAdapter(this, R.layout.list_item, data)

String[] data = {“夏目漱石”, “太宰治”, … };

NoteList の 1行は TextView になるTextView に表示される文字は toString() の戻り値

textview

Page 28: Adapter & ListView & ExpandalbeListView

ArrayAdapter• Constructors

– parameters• context :

ArrayAdapter が紐付く View が動作する Context this, getApplicationContext() など

• resource : ListView の 1行のレイアウト XML ファイルの Resource ID

このレイアウトには、 textViewResourceId で指定した Id の TextView が含まれていなければならない

• textViewResrouceId : TextView の resource Id

• Object : 各 View に紐づける T クラス (String, 独自クラスなど) の配列

toString() の戻り値が TextView に入れられる

ArrayAdapter(Context context, int resrouce, int textViewResourceId, T[] Object)

Page 29: Adapter & ListView & ExpandalbeListView

ArrayAdapter• Constructors

ArrayAdapter(this, R.layout.list_item, R.id.textview, data)

String[] data = {“夏目漱石”, “太宰治”, … };

NoteList の 1行のどこかに adapter と紐付く TextView が含まれるTextView に表示される文字は toString() の戻り値

textview

Page 30: Adapter & ListView & ExpandalbeListView

ArrayAdapter• Constructors

– parameters• context :

ArrayAdapter が紐付く View が動作する Context this, getApplicationContext() など

• resource : TextView が1つだけ含まれるレイアウトファイルの resource Id

レイアウトファイルの root view が TextView でなければならない• Object :

各 View に紐づける T クラス (String, 独自クラスなど) の ListtoString() の戻り値が TextView に入れられる

ArrayAdapter(Context context, int resource, List<T> Object)

Page 31: Adapter & ListView & ExpandalbeListView

ArrayAdapter• Constructors

ArrayAdapter(this, R.layout.list_item, data)

List<String> data = new ArrayList<String>();data.add (“夏目漱石”);data.add(“太宰治”);

NoteList の 1行は TextView になるTextView に表示される文字は toString() の戻り値

textview

Page 32: Adapter & ListView & ExpandalbeListView

ArrayAdapter• Constructors

– parameters• context :

ArrayAdapter が紐付く View が動作する Context this, getApplicationContext() など

• resource : ListView の 1行のレイアウト XML ファイルの Resource ID

このレイアウトには、 textViewResourceId で指定した Id の TextView が含まれていなければならない

• textViewResrouceId : TextView の resource Id

• Object : 各 View に紐づける T クラス (String, 独自クラスなど) の List

toString() の戻り値が TextView に入れられる

ArrayAdapter(Context context, int resource, int textViewResourceId, List<T> Object)

Page 33: Adapter & ListView & ExpandalbeListView

ArrayAdapter• Constructors

ArrayAdapter(this, R.layout.list_item, R.id.textview, data)

List<String> data = new ArrayList<String>();data.add (“夏目漱石”);data.add(“太宰治”);

NoteList の 1行のどこかに adapter と紐付く TextView が含まれるTextView に表示される文字は toString() の戻り値

textview

Page 34: Adapter & ListView & ExpandalbeListView

ArrayAdapter• Get static adapter

– parameters• context :

ArrayAdapter が紐付く View が動作する Context this, getApplicationContext() など

• textArrayResourceId : 文字列の配列が定義されている resource Id

res/values/arrays.xml など• resource :

TextView が1つだけ含まれるレイアウトファイルの resource Idレイアウトファイルの root view が TextView でなければならない

– Returns• static ArrayAdapter<CharSequence>

ArrayAdapter.createFromResource(Context context, int textArrayResrouceId, int resource)

Page 35: Adapter & ListView & ExpandalbeListView

ArrayAdapter• Get static adapter

ArrayAdapter(this, R.array.authors, R.layout.list_item, data)

res/values/arrays.xml<string-array name=“authors”> <item>夏目漱石</item> <item>太宰治</item></string-array>

NoteList の 1行は TextView になるTextView に表示される文字は resource の string-array で定義した各文字列

textview

Page 36: Adapter & ListView & ExpandalbeListView

extends ArrayAdapter

Page 37: Adapter & ListView & ExpandalbeListView

extends ArrayAdapter• Point

– getView() を Override する– ViewHolder を使う

• Example – ImageView and TextView– ListView に紐づけるデータ用のクラスを定義– ViewHolder 用のクラスを定義class bindData {

String text;int imageResourceId;

}

static class ViewHolder {

TextView textView;

ImageView imageView;

}

Page 38: Adapter & ListView & ExpandalbeListView

extends ArrayAdapter

public class MyAdapter extends ArrayAdapter<bindData> {

private LayoutInflater inflater;

public MyAdapter(Context context, List<ImageAndString> objects) {

super(context, 0, objects);

this.inflater = (LayoutInflater) context

.getSystemService(Context.LAYOUT_INFLATER_SERVICE);}

• Point– View を生成するための LayoutInflater を取得する– 親クラス (ArrayAdapter) のコンストラクタの textViewResoruceId

には 0 を渡す

• Example – ImageView and TextView– getSystemService(Context.LAYOUT_INFLATER_SERVICE) を使う

Page 39: Adapter & ListView & ExpandalbeListView

extends ArrayAdapter@Override

public View getView(int position, View convertView, ViewGroup parent) {

ViewHolder holder;

if (convertView == null) {convertView = inflater.inflate(R.layout.list_item, parent, false);

holder = new ViewHolder();

holder.textView = (TextView) convertView.findViewById(R.id.textview);

holder.imageView = (ImageView) convertView.findViewById(R.id.imageview);

convertView.setTag(holder);

} else {

holder = (ViewHolder) convertView.getTag();

}

bindData data= getItem(position);

holder.textView.setText(data.text);

holder.imageView.setImageResource(data.imageResourceId);

return convertView;

}

}

Page 40: Adapter & ListView & ExpandalbeListView

extends ArrayAdapter● Point

● 複雑なレイアウトでもOK● いろんな View に別々の型のデータを紐づけられる● データ用のクラスの List (or 配列)の要素数と ListView の

行数は同じ

imageviewtextview

Page 41: Adapter & ListView & ExpandalbeListView

extends BaseAdapter

Page 42: Adapter & ListView & ExpandalbeListView

extends BaseAdapter● Point

● 複雑なレイアウトでもOK● データのリストを管理を自分でできる

Page 43: Adapter & ListView & ExpandalbeListView

ExpandableListView

Page 44: Adapter & ListView & ExpandalbeListView

Adaptears for ExpandableList• public interface

– ExpandableListAdapter– HeterogeneousExpandableList

• public abstruct class– BaseExpandableListAdapter– CursorTreeAdapter– ResourceCursorTreeAdapter

• public class– SimpleExpandableListAdapter

Page 45: Adapter & ListView & ExpandalbeListView

SimpleExpandableListAdapter

Page 46: Adapter & ListView & ExpandalbeListView

SimpleExpandableListAdapter• Constructors

– parameters• groupData, groupLayout, groupFrom,

groupTo : group (= とじたとき) に関する引数

SimpleAdapter と同じ• childData, childLayout, childFrom, childTo :

Child (= 開いたとき) に関する引数SimpleAdapter と同じ

SimpleExpandableListAdapter(Context context, List<? extendsMap<String, ?>> groupData, int groupLayout, String[] groupFrom, int[] groupTo, List<? extends List<? extends Map<String, ?>>> childData, int childLayout, String[] childFrom, int[] childTo)

Page 47: Adapter & ListView & ExpandalbeListView

SimpleExpandableListAdapter• Constructors

SimpleExpandableListAdapter(this, groupData, R.layout.list_item1, groupFrom, groupTo, childData, R.layout.list_item2, childFrom, childTo)

res/layout/list_item1.xml<LinearLayout android:orientation=“horizontal” > <ImageView /> <TextView android:id=“@+id/textview” /></LinearLayout>

int[] groupTo = {R.id.textview};

textview

Page 48: Adapter & ListView & ExpandalbeListView

SimpleExpandableListAdapter• Constructors

static final List<HashMap<String, String> groupData = Arrays.asList( new HashMap<String, String>() { put(“Author”, “夏目漱石”); }, … new HashMap<String, String>() { put(“Author”, “太宰治”); });

String[] groupFrom = {“Author”};

SimpleExpandableListAdapter(this, groupData, R.layout.list_item1, groupFrom, groupTo, childData, R.layout.list_item2, childFrom, childTo)

textview

Page 49: Adapter & ListView & ExpandalbeListView

SimpleExpandableListAdapter• Constructors

res/layout/list_item2.xml<LinearLayout android:orientation=“horizontal” > <TextView android:id=“@+id/textview1” /> <TextView android:id=“@+id/textview2” /></LinearLayout>

int[] childTo = {R.id.textview1, R.id.textview2};

textview2

textview1

SimpleExpandableListAdapter(this, groupData, R.layout.list_item1, groupFrom, groupTo,childData, R.layout.list_item2, childFrom, childTo)

Page 50: Adapter & ListView & ExpandalbeListView

SimpleExpandableListAdapter• Constructors

List<List<HashMap<String, String> childData = new ArrayList<List<HashMap<String, String>();static final List<HashMap<String, String> firstChildData = Arrays.asList( new HashMap<String, String>() { put(“Title”, “吾輩は猫である”); put(“Date”, “1905年”); }, … new HashMap<String, String>() { put(“Title”, “坊ちゃん”); put(“Date”, “1906年”); });childData.add(firstChildData);

String[] childFrom = {“Title”, “Date”};

textview2textview2

textview1

SimpleExpandableListAdapter(this, groupData, R.layout.list_item1, groupFrom, groupTo, childData, R.layout.list_item2, childFrom, childTo)

Page 51: Adapter & ListView & ExpandalbeListView

SimpleExpandableListAdapter• Constructors

SimpleExpandableListAdapter(this, groupData, R.layout.list_item1, groupFrom, groupTo, childData, R.layout.list_item2, childFrom, childTo)

textview2

textview1

Page 52: Adapter & ListView & ExpandalbeListView

SimpleExpandableListAdapter● Notes

● 異なるデータ型を一緒に含むことができない● 文字列(String)と resource ID(Integer)を一緒にできない● データ型を Object にすればできます!!

imageviewtextview

List<List<HashMap<String, Object> childData = new ArrayList<List<HashMap<String, Object>();static final List<HashMap<String, Object> firstChildData = Arrays.asList( new HashMap<String, String>() { put(“Icon”, R.id.waganeko); put(“Title”, “吾輩は猫である”); }, … new HashMap<String, String>() { put(“Icon”, R.id.bochyan); put(“Title”, “坊ちゃん”); });childData.add(firstChildData);

Page 53: Adapter & ListView & ExpandalbeListView

extends BaseExpandableListAdapter

Page 54: Adapter & ListView & ExpandalbeListView

extends BaseExpandableListAdapter

● Point● 複雑なレイアウトでもOK● データのリストを管理を自分でできる