回答編集履歴
4
Contextを修正したときの,修正不足分を修正.
test
CHANGED
@@ -198,7 +198,7 @@
|
|
198
198
|
|
199
199
|
// productData = objects;
|
200
200
|
|
201
|
-
mLayoutInflater = (LayoutInflater)
|
201
|
+
mLayoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
|
202
202
|
|
203
203
|
}
|
204
204
|
|
3
引用マークが上手くついていなかったので修正
test
CHANGED
@@ -6,7 +6,7 @@
|
|
6
6
|
|
7
7
|
|
8
8
|
|
9
|
-
>ちなみに上記のコードのようにListFragmentを継承したIPCartFragmentのフィールドにstaticなカスタムアダプターの変数を置き、他のクラスでListViewのデータを変更した時にIPCartFragmentのstaticなカスタムアダプターの変数を参照して更新をかけるというやり方は正しいやり方でしょうか?
|
9
|
+
> ちなみに上記のコードのようにListFragmentを継承したIPCartFragmentのフィールドにstaticなカスタムアダプターの変数を置き、他のクラスでListViewのデータを変更した時にIPCartFragmentのstaticなカスタムアダプターの変数を参照して更新をかけるというやり方は正しいやり方でしょうか?
|
10
10
|
|
11
11
|
正しくありません.今回のように Adapter 内のクラス View.OnClickListener などから,notifyDataSetChanged() を呼ぶときには,親を参照するために IPCartProductAdapter.this を使って参照します.
|
12
12
|
|
2
Contextでも十分なので,指摘を追加
test
CHANGED
@@ -136,7 +136,7 @@
|
|
136
136
|
|
137
137
|
// 外部から参照しないのであればこれで十分でしょう
|
138
138
|
|
139
|
-
setListAdapter(new IPCartProductAdapter(get
|
139
|
+
setListAdapter(new IPCartProductAdapter(getContext(), R.layout.list_item_cart, itemList));
|
140
140
|
|
141
141
|
}
|
142
142
|
|
@@ -170,6 +170,10 @@
|
|
170
170
|
|
171
171
|
|
172
172
|
|
173
|
+
// ArrayAdapter クラスは Context を 要求しているので Activity を渡す必要もないです
|
174
|
+
|
175
|
+
/*
|
176
|
+
|
173
177
|
public IPCartProductAdapter(Activity activity, int textViewResourceId, List<IPCartProductData> objects) {
|
174
178
|
|
175
179
|
super(activity, textViewResourceId, objects);
|
@@ -178,6 +182,22 @@
|
|
178
182
|
|
179
183
|
// productData = objects;
|
180
184
|
|
185
|
+
mLayoutInflater = LayoutInflater.from(context);
|
186
|
+
|
187
|
+
}
|
188
|
+
|
189
|
+
*/
|
190
|
+
|
191
|
+
// こちらが適切でしょう
|
192
|
+
|
193
|
+
public IPCartProductAdapter(Context context, int textViewResourceId, List<IPCartProductData> objects) {
|
194
|
+
|
195
|
+
super(context, textViewResourceId, objects);
|
196
|
+
|
197
|
+
|
198
|
+
|
199
|
+
// productData = objects;
|
200
|
+
|
181
201
|
mLayoutInflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
|
182
202
|
|
183
203
|
}
|
1
コメントアウトアウトではなく書き直した点があったので,コメントアウトにして差異をわかりやすくした.
test
CHANGED
@@ -28,8 +28,64 @@
|
|
28
28
|
|
29
29
|
|
30
30
|
|
31
|
+
// 明確な理由がなければ onCreate() で setAdapter() してよいでしょう
|
32
|
+
|
33
|
+
/*
|
34
|
+
|
31
35
|
@Override
|
32
36
|
|
37
|
+
public void onViewCreated(View view, Bundle savedInstanceState) {
|
38
|
+
|
39
|
+
super.onViewCreated(view, savedInstanceState);
|
40
|
+
|
41
|
+
|
42
|
+
|
43
|
+
ListView cartListView = (ListView)view.findViewById(android.R.id.list); {
|
44
|
+
|
45
|
+
List<IPCartProductData> itemList = new ArrayList<>();
|
46
|
+
|
47
|
+
|
48
|
+
|
49
|
+
IPCartProductData customCartData1 = new IPCartProductData();
|
50
|
+
|
51
|
+
customCartData1.setProductImage(BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher));
|
52
|
+
|
53
|
+
customCartData1.setProductName("ハンバーガー");
|
54
|
+
|
55
|
+
customCartData1.setProductPrice(120);
|
56
|
+
|
57
|
+
|
58
|
+
|
59
|
+
IPCartProductData customCartData2 = new IPCartProductData();
|
60
|
+
|
61
|
+
customCartData2.setProductImage(BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher));
|
62
|
+
|
63
|
+
customCartData2.setProductName("コーラ");
|
64
|
+
|
65
|
+
customCartData2.setProductPrice(80);
|
66
|
+
|
67
|
+
|
68
|
+
|
69
|
+
itemList.add(customCartData1);
|
70
|
+
|
71
|
+
itemList.add(customCartData2);
|
72
|
+
|
73
|
+
|
74
|
+
|
75
|
+
adapter = new IPCartProductAdapter(getActivity(), android.R.layout.simple_expandable_list_item_1, itemList);
|
76
|
+
|
77
|
+
|
78
|
+
|
79
|
+
cartListView.setAdapter(adapter);
|
80
|
+
|
81
|
+
}
|
82
|
+
|
83
|
+
*/
|
84
|
+
|
85
|
+
|
86
|
+
|
87
|
+
@Override
|
88
|
+
|
33
89
|
public void onCreate(Bundle savedInstanceState) {
|
34
90
|
|
35
91
|
super.onCreate(savedInstanceState);
|