Upload
yuki-anzai
View
21.725
Download
0
Embed Size (px)
Citation preview
Adapter & ListView
2010.10.23
agenda• Adapter - one of the GOF design pattern –• ListView• ExpadableListView
Adapter● GoF (Gang of Four) によって定義されたデザイン
パターンの一つ● 既存のクラスに修正不要● インタフェースを変更可能● ListView の adapter は委譲のパターン
ListView● 1行が1つの View● チェックボタンやラジオボタンを付けられる
Adapters for List• interface
– ListAdapter– SpinnerAdapter– WarrerListAdapter
• abstruct class– BaseAdapter– CursorAdapter– ResourceCursorAdapter
• class– ArrayAdapter– HeaderViewListAdapter– SimpleAdapter– SimpleCursorAdapter
SimpleAdapter
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 を使える
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)
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
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
SimpleAdapter• Constructors
SimpleAdapter( this, data, R.layout.list_item, from, to)
textview2
textview1
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
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)
SimpleAdapter• Constructors
imageview2
imageview1
SimpleAdapter( this, data, R.layout.list_item, from, to)
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”, “太宰治”); });
SimpleCursorAdapter
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 を使える
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)
SimpleCursorAdapter● Notes
● List<? extends Map<String, ?>> data が Cursor c になっただけで、 SimpleAdapter と同じ
● もちろん、異なるデータ型を含むことができる
imageview
textview
できる! ^ ^
ArrayAdapter
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 する
ArrayAdapter• Constructors
– parameters• context :
ArrayAdapter が紐付く View が動作する Context this, getApplicationContext() など
• resource: TextView が1つだけ含まれるレイアウトファイルの resource Id
レイアウトファイルの root view が TextView でなければならない
ArrayAdapter(Context context, int resource)
ArrayAdapter• Constructors
ArrayAdapter(this, R.layout.list_item)
NoteList の 1行は TextView になる
紐づけるデータは add(T object) で追加する
textview
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)
ArrayAdapter• Constructors
ArrayAdapter(this, R.layout.list_item, R.id.textview)
NoteList の 1行のどこかに adapter と紐付く TextView が含まれる
紐づけるデータは add(T object) で追加する
textview
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)
ArrayAdapter• Constructors
ArrayAdapter(this, R.layout.list_item, data)
String[] data = {“夏目漱石”, “太宰治”, … };
NoteList の 1行は TextView になるTextView に表示される文字は toString() の戻り値
textview
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)
ArrayAdapter• Constructors
ArrayAdapter(this, R.layout.list_item, R.id.textview, data)
String[] data = {“夏目漱石”, “太宰治”, … };
NoteList の 1行のどこかに adapter と紐付く TextView が含まれるTextView に表示される文字は toString() の戻り値
textview
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)
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
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)
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
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)
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
extends ArrayAdapter
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;
}
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) を使う
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;
}
}
extends ArrayAdapter● Point
● 複雑なレイアウトでもOK● いろんな View に別々の型のデータを紐づけられる● データ用のクラスの List (or 配列)の要素数と ListView の
行数は同じ
imageviewtextview
extends BaseAdapter
extends BaseAdapter● Point
● 複雑なレイアウトでもOK● データのリストを管理を自分でできる
ExpandableListView
Adaptears for ExpandableList• public interface
– ExpandableListAdapter– HeterogeneousExpandableList
• public abstruct class– BaseExpandableListAdapter– CursorTreeAdapter– ResourceCursorTreeAdapter
• public class– SimpleExpandableListAdapter
SimpleExpandableListAdapter
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)
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
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
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)
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)
SimpleExpandableListAdapter• Constructors
SimpleExpandableListAdapter(this, groupData, R.layout.list_item1, groupFrom, groupTo, childData, R.layout.list_item2, childFrom, childTo)
textview2
textview1
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);
extends BaseExpandableListAdapter
extends BaseExpandableListAdapter
● Point● 複雑なレイアウトでもOK● データのリストを管理を自分でできる