More Related Content Similar to Adapter & ListView & ExpandalbeListView Similar to Adapter & ListView & ExpandalbeListView (9) More from Yuki Anzai (11) Adapter & ListView & ExpandalbeListView3. Adapter
●
GoF (Gang of Four) によって定義されたデザイン
パターンの一つ
●
既存のクラスに修正不要
●
インタフェースを変更可能
●
ListView の adapter は委譲のパターン
7. SimpleAdapter
•
What is
–
static data を XML ファイルで
定義された TextView や
ImageView に紐づけるための簡
単な adapter
•
What views can use?
–
TextView
–
ImageView
–
ChackedTextView
•
Way to bind
1. SimpleAdapter.ViewBinder が
セットされていたら、このクラ
スの setViewValue メソッドが
呼ばれる
2. 1. が false の場合、setTextView
が呼ばれる
3. 2. が false の場
合、setImageView が呼ばれる
* setViewBinder() で独自の
SimpleAdapter.ViewBinder を
セットすれば、TextView,
ImageView 以外の View を使え
る
8. 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)
9. 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
10. 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
12. 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
13. 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)
17. SimpleCursorAdapter
•
What is
–
Cursor から取得したカラムを
XML ファイルで定義された
TextView や ImageView に紐づけ
るための簡単な adapter
•
What views can use?
–
TextView
–
ImageView
–
ChackedTextView
•
Way to bind
1. SimpleAdapter.ViewBinder が
セットされていたら、このクラ
スの setViewValue メソッドが
呼ばれる
2. 1. が false の場合、setTextView
が呼ばれる
3. 2. が false の場
合、setImageView が呼ばれる
* setViewBinder() で独自の
SimpleAdapter.ViewBinder を
セットすれば、TextView,
ImageView 以外の View を使
える
18. 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)
21. ArrayAdapter
•
What is
–
任意の Object の配列を
ListView に紐づけるための
adapter
•
What views can use?
–
TextView (デフォルト)
–
任意の View
•
Way to bind
1. デフォルトは 1行 = TextView
2. id を指定すれば、レイアウト
内の任意の TextView に紐づけ
可能
3. 任意の Object の配列を紐づけ
る場合、toString() を override
すれば、その戻り値が
TextView に入る
4. ImageView など toString() を
戻り値がふさわしくない場合
、getView(int, View,
ViewGroup) を override する
24. 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)
26. 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)
28. 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)
30. ArrayAdapter
•
Constructors
–
parameters
• context :
ArrayAdapter が紐付く View が動作する Context
this, getApplicationContext() など
• resource :
TextView が1つだけ含まれるレイアウトファイルの resource Id
レイアウトファイルの root view が TextView でなければならない
• Object :
各 View に紐づける T クラス (String, 独自クラスなど) の List
toString() の戻り値が TextView に入れられる
ArrayAdapter(Context context, int resource, List<T> Object)
32. 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)
34. 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)
35. 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>
Note
List の 1行は TextView になる
TextView に表示される文字は resource の string-
array で定義した各文字列
textview
37. 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;
}
38. 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) を使う
39. 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;
}
}
44. Adaptears for ExpandableList
•
public interface
–
ExpandableListAdapter
–
HeterogeneousExpandableList
•
public abstruct class
–
BaseExpandableListAdapter
–
CursorTreeAdapter
–
ResourceCursorTreeAdapter
•
public class
–
SimpleExpandableListAdapter
46. 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)
48. 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
50. 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)
52. SimpleExpandableListAdapter
●
Notes
●
異なるデータ型を一緒に含むことができない
●
文字列(String)と resource ID(Integer)を一緒にできない
●
データ型を Object にすればできます!!
imageview
textview
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);