質問編集履歴

2

ListAdapter のgetViewメソッド内でリスナーが抜けていたので追加しました。

2018/03/19 00:07

投稿

lereve
lereve

スコア12

test CHANGED
File without changes
test CHANGED
@@ -54,6 +54,14 @@
54
54
 
55
55
 
56
56
 
57
+ ListAdapter のgetViewメソッド内にリスナーが抜けておりましたので追加しましたところ、
58
+
59
+ Button okButton =(Button) convertView.findViewById(R.id.ok_Button);
60
+
61
+ の部分でぬるぽが発生します。
62
+
63
+
64
+
57
65
  ### 画面写真
58
66
 
59
67
  ![イメージ説明](d340a8f957e02af34c77a9a4c840daa5.png)
@@ -234,28 +242,6 @@
234
242
 
235
243
  ```Java
236
244
 
237
- import android.content.Context;
238
-
239
- import android.view.LayoutInflater;
240
-
241
- import android.view.View;
242
-
243
- import android.view.ViewGroup;
244
-
245
- import android.widget.ArrayAdapter;
246
-
247
- import android.widget.Button;
248
-
249
- import android.widget.TextView;
250
-
251
-
252
-
253
- import java.util.List;
254
-
255
-
256
-
257
-
258
-
259
245
  public class ListAdapter extends ArrayAdapter<ListItem> {
260
246
 
261
247
 
@@ -314,6 +300,30 @@
314
300
 
315
301
 
316
302
 
303
+ Button okButton =(Button) convertView.findViewById(R.id.ok_Button);
304
+
305
+ okButton.setOnClickListener(new OK_Listener());
306
+
307
+
308
+
309
+ Button ngButton =(Button) convertView.findViewById(R.id.ng_Button);
310
+
311
+ ngButton.setOnClickListener(new NG_Listener());
312
+
313
+
314
+
315
+ Button noneButton =(Button) convertView.findViewById(R.id.none_Button);
316
+
317
+ noneButton.setOnClickListener(new NONE_Listener());
318
+
319
+
320
+
321
+ Button commentButton =(Button) convertView.findViewById(R.id.comment_Button);
322
+
323
+ commentButton.setOnClickListener(new Comment_Listener());
324
+
325
+
326
+
317
327
  // QuestionNumberに番号を設定
318
328
 
319
329
  TextView questionNumber = (TextView)view.findViewById(R.id.questionNumber);
@@ -334,7 +344,59 @@
334
344
 
335
345
  }
336
346
 
337
-
347
+ //okボタンのリスナー
348
+
349
+ class OK_Listener implements View.OnClickListener {
350
+
351
+ public void onClick(View v){
352
+
353
+ Log.v("**********", "OK_Button");
354
+
355
+ }
356
+
357
+ }
358
+
359
+
360
+
361
+ //ngボタンのリスナー
362
+
363
+ class NG_Listener implements View.OnClickListener {
364
+
365
+ public void onClick(View v){
366
+
367
+ Log.v("**********", "NG_Button");
368
+
369
+ }
370
+
371
+ }
372
+
373
+
374
+
375
+ //noneボタンのリスナー
376
+
377
+ class NONE_Listener implements View.OnClickListener {
378
+
379
+ public void onClick(View v){
380
+
381
+ Log.v("**********", "NONEK_Button");
382
+
383
+ }
384
+
385
+ }
386
+
387
+
388
+
389
+ //commentボタンのリスナー
390
+
391
+ class Comment_Listener implements View.OnClickListener {
392
+
393
+ public void onClick(View v){
394
+
395
+ Log.v("**********", "Comment_Button");
396
+
397
+ }
398
+
399
+ }
338
400
 
339
401
  }
340
402
 

1

MainActivityのコードがListViewのコードになってしまっていたのを修正しました。

2018/03/19 00:07

投稿

lereve
lereve

スコア12

test CHANGED
File without changes
test CHANGED
@@ -42,6 +42,18 @@
42
42
 
43
43
  よろしくお願いします。
44
44
 
45
+
46
+
47
+ ### 追記・修正
48
+
49
+ MainActivityのコードがListViewのコードになってしまっていました。
50
+
51
+ 訂正しました。
52
+
53
+ 申し訳ありませんでした。
54
+
55
+
56
+
45
57
  ### 画面写真
46
58
 
47
59
  ![イメージ説明](d340a8f957e02af34c77a9a4c840daa5.png)
@@ -52,6 +64,176 @@
52
64
 
53
65
  ```Java
54
66
 
67
+ public class MainActivity extends AppCompatActivity {
68
+
69
+
70
+
71
+ @Override
72
+
73
+ protected void onCreate(Bundle savedInstanceState) {
74
+
75
+ super.onCreate(savedInstanceState);
76
+
77
+ setContentView(R.layout.activity_main);
78
+
79
+
80
+
81
+ // レイアウトからリストビューを取得
82
+
83
+ ListView listView = (ListView)findViewById(R.id.listview);
84
+
85
+
86
+
87
+ // リストビューに表示する要素を設定
88
+
89
+ ArrayList<ListItem> listItems = new ArrayList<>();
90
+
91
+ for (int i = 0; i < 30; i++) {
92
+
93
+ ListItem item = new ListItem(i + 1, "sample text", 0);
94
+
95
+ listItems.add(item);
96
+
97
+ }
98
+
99
+
100
+
101
+ // 出力結果をリストビューに表示
102
+
103
+ ListAdapter adapter = new ListAdapter(this, R.layout.list_item, listItems);
104
+
105
+ listView.setAdapter(adapter);
106
+
107
+
108
+
109
+ // タップ時のイベントを追加
110
+
111
+ listView.setOnItemClickListener(onItemClickListener);
112
+
113
+
114
+
115
+ }
116
+
117
+
118
+
119
+ private AdapterView.OnItemClickListener onItemClickListener = new AdapterView.OnItemClickListener() {
120
+
121
+ @Override
122
+
123
+ public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
124
+
125
+ Toast.makeText(MainActivity.this, "テスト", Toast.LENGTH_LONG).show();
126
+
127
+
128
+
129
+ // タップしたアイテムの取得
130
+
131
+ ListView listView = (ListView)parent;
132
+
133
+ ListItem item = (ListItem)listView.getItemAtPosition(position); // ListItemにキャスト
134
+
135
+
136
+
137
+ AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
138
+
139
+ builder.setTitle("Tap No. " + String.valueOf(position));
140
+
141
+ builder.setMessage(item.getQuestionContent());
142
+
143
+ builder.show();
144
+
145
+ }
146
+
147
+ };
148
+
149
+
150
+
151
+ }
152
+
153
+ ```
154
+
155
+
156
+
157
+ ListItem
158
+
159
+ ```Java
160
+
161
+ public class ListItem {
162
+
163
+ private int mQuestionNumber = 0;
164
+
165
+ private String mQuestionContent = "";
166
+
167
+ private int mStatus = 0;
168
+
169
+
170
+
171
+ public ListItem(int questionNumber, String questionContent, int status){
172
+
173
+ mQuestionNumber = questionNumber;
174
+
175
+ mQuestionContent = questionContent;
176
+
177
+ mStatus = status;
178
+
179
+ }
180
+
181
+
182
+
183
+ public void setQuestionNumber(int questionNumber){
184
+
185
+ mQuestionNumber = questionNumber;
186
+
187
+ }
188
+
189
+
190
+
191
+ public void setQuestionContext(String questionContext){
192
+
193
+ mQuestionContent = questionContext;
194
+
195
+ }
196
+
197
+
198
+
199
+ public void setStatus(int status){
200
+
201
+ mStatus = status;
202
+
203
+ }
204
+
205
+ public int getQuestionNumber(){
206
+
207
+ return mQuestionNumber;
208
+
209
+ }
210
+
211
+ public String getQuestionContent(){
212
+
213
+ return mQuestionContent;
214
+
215
+ }
216
+
217
+ public int getStatus(){
218
+
219
+ return mStatus;
220
+
221
+ }
222
+
223
+
224
+
225
+ }
226
+
227
+
228
+
229
+ ```
230
+
231
+
232
+
233
+ ListAdapter
234
+
235
+ ```Java
236
+
55
237
  import android.content.Context;
56
238
 
57
239
  import android.view.LayoutInflater;
@@ -157,191 +339,3 @@
157
339
  }
158
340
 
159
341
  ```
160
-
161
-
162
-
163
- ListItem
164
-
165
- ```Java
166
-
167
- public class ListItem {
168
-
169
- private int mQuestionNumber = 0;
170
-
171
- private String mQuestionContent = "";
172
-
173
- private int mStatus = 0;
174
-
175
-
176
-
177
- public ListItem(int questionNumber, String questionContent, int status){
178
-
179
- mQuestionNumber = questionNumber;
180
-
181
- mQuestionContent = questionContent;
182
-
183
- mStatus = status;
184
-
185
- }
186
-
187
-
188
-
189
- public void setQuestionNumber(int questionNumber){
190
-
191
- mQuestionNumber = questionNumber;
192
-
193
- }
194
-
195
-
196
-
197
- public void setQuestionContext(String questionContext){
198
-
199
- mQuestionContent = questionContext;
200
-
201
- }
202
-
203
-
204
-
205
- public void setStatus(int status){
206
-
207
- mStatus = status;
208
-
209
- }
210
-
211
- public int getQuestionNumber(){
212
-
213
- return mQuestionNumber;
214
-
215
- }
216
-
217
- public String getQuestionContent(){
218
-
219
- return mQuestionContent;
220
-
221
- }
222
-
223
- public int getStatus(){
224
-
225
- return mStatus;
226
-
227
- }
228
-
229
-
230
-
231
- }
232
-
233
-
234
-
235
- ```
236
-
237
-
238
-
239
- ListAdapter
240
-
241
- ```Java
242
-
243
- import android.content.Context;
244
-
245
- import android.view.LayoutInflater;
246
-
247
- import android.view.View;
248
-
249
- import android.view.ViewGroup;
250
-
251
- import android.widget.ArrayAdapter;
252
-
253
- import android.widget.Button;
254
-
255
- import android.widget.TextView;
256
-
257
-
258
-
259
- import java.util.List;
260
-
261
-
262
-
263
-
264
-
265
- public class ListAdapter extends ArrayAdapter<ListItem> {
266
-
267
-
268
-
269
- private int mResource;
270
-
271
- private List<ListItem> mItems;
272
-
273
- private LayoutInflater mInflater;
274
-
275
-
276
-
277
- public ListAdapter(Context context, int resource, List<ListItem> items){
278
-
279
- super(context, resource, items);
280
-
281
-
282
-
283
- mResource = resource;
284
-
285
- mItems = items;
286
-
287
- mInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
288
-
289
-
290
-
291
- }
292
-
293
-
294
-
295
- @Override
296
-
297
- public View getView(int position, View convertView, ViewGroup parent) {
298
-
299
- View view;
300
-
301
-
302
-
303
- if (convertView != null) {
304
-
305
- view = convertView;
306
-
307
- }
308
-
309
- else {
310
-
311
- view = mInflater.inflate(mResource, null);
312
-
313
- }
314
-
315
-
316
-
317
- // リストビューに表示する要素を取得
318
-
319
- ListItem item = mItems.get(position);
320
-
321
-
322
-
323
- // QuestionNumberに番号を設定
324
-
325
- TextView questionNumber = (TextView)view.findViewById(R.id.questionNumber);
326
-
327
- questionNumber.setText(Integer.toString(item.getQuestionNumber()));
328
-
329
-
330
-
331
- // QuestionContextに番号を設定
332
-
333
- TextView questionContext = (TextView)view.findViewById(R.id.questionContext);
334
-
335
- questionContext.setText(item.getQuestionContent());
336
-
337
-
338
-
339
- return view;
340
-
341
- }
342
-
343
-
344
-
345
- }
346
-
347
- ```