質問編集履歴

2

 

2016/05/19 07:41

投稿

gorira1321
gorira1321

スコア27

test CHANGED
File without changes
test CHANGED
@@ -2,15 +2,15 @@
2
2
 
3
3
  ウィジェットにListViewを配置し、1行毎に名前とボタンを表示するサンプルアプリを作っています。(イメージ参照)
4
4
 
5
- ListViewの表示まで出来たのですが、ボタンをタップしてもイベントが取得できません。
5
+ ListViewの表示まで出来たのですが、rowのボタンをタップしてもイベントが取得できません。
6
6
 
7
7
 
8
8
 
9
9
  イメージのBUTTON1とBUTTON2のタップイベントは取得できました。
10
10
 
11
- SuzukiとSatoの横にあるボタンのイベント方法を教えて下さい
11
+ SuzukiとSatoの横にあるボタンのイベント取得方法を教えて下さい
12
-
12
+
13
- 将来的にはSuzukiのボタンをタップしたらSuzukiさんとtextviewを変えたい
13
+ 将来的にはSuzukiのボタンをタップしたらSuzukiさんとtextviewを変えたい
14
14
 
15
15
  ![イメージ説明](b0c3dda4914cc58e35d3219646275cff.png)
16
16
 

1

説明の追加

2016/05/19 07:41

投稿

gorira1321
gorira1321

スコア27

test CHANGED
File without changes
test CHANGED
@@ -16,478 +16,468 @@
16
16
 
17
17
 
18
18
 
19
+
20
+
19
- ###発生している問題・エラメッセ
21
+ ###該当のソスコ
22
+
20
-
23
+ ```ここに言語を入力
24
+
25
+
26
+
21
-
27
+ AndroidManifest.xml(抜粋)
28
+
29
+ <receiver android:name=".NewAppWidget">
30
+
31
+ <intent-filter>
32
+
33
+ <action android:name="android.appwidget.action.APPWIDGET_UPDATE"/>
34
+
35
+ <action android:name="com.android.BUTTON1_CLICKED" />
36
+
37
+ <action android:name="com.android.BUTTON2_CLICKED" />
38
+
39
+ <action android:name="com.android.BUTTON3_CLICKED" />
40
+
41
+ </intent-filter>
42
+
43
+
44
+
45
+ <meta-data
46
+
47
+ android:name="android.appwidget.provider"
48
+
49
+ android:resource="@xml/new_app_widget_info"/>
50
+
51
+ </receiver>
52
+
53
+
54
+
55
+ <service android:name=".SampleWidgetService"
56
+
57
+ android:permission="android.permission.BIND_REMOTEVIEWS">
58
+
59
+ </service>
60
+
61
+
62
+
63
+ NewAppWidget.class
64
+
65
+ public class NewAppWidget extends AppWidgetProvider {
66
+
67
+
68
+
69
+ private static final String TAG = "NewAppWidget";
70
+
71
+
72
+
73
+ public static final String btn1Filter = "com.android.BUTTON1_CLICKED";
74
+
75
+ public static final String btn2Filter = "com.android.BUTTON2_CLICKED";
76
+
77
+ public static final String btn3Filter = "com.android.BUTTON3_CLICKED";
78
+
79
+
80
+
81
+ @Override
82
+
83
+ public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
84
+
85
+ super.onUpdate(context, appWidgetManager, appWidgetIds);
86
+
87
+
88
+
89
+ Log.e(TAG, "[onUpdate]");
90
+
91
+
92
+
93
+ for (int appWidgetId : appWidgetIds) {
94
+
95
+
96
+
97
+ Intent remoteViewsFactoryIntent = new Intent(context, SampleWidgetService.class);
98
+
99
+ RemoteViews rv = new RemoteViews(context.getPackageName(), R.layout.new_app_widget);
100
+
101
+ rv.setRemoteAdapter(R.id.listView, remoteViewsFactoryIntent);
102
+
103
+
104
+
105
+ // BUTTON1 このイベントは取得できました
106
+
107
+ Intent btn1Intent = new Intent(NewAppWidget.btn1Filter);
108
+
109
+ PendingIntent btn1Pending = PendingIntent.getBroadcast(context, 0, btn1Intent, 0);
110
+
111
+ rv.setOnClickPendingIntent(R.id.btn1_id, btn1Pending);
112
+
113
+
114
+
115
+ // BTUUON2 このイベントは取得できました
116
+
117
+ Intent btn2Intent = new Intent(NewAppWidget.btn2Filter);
118
+
119
+ PendingIntent btn2Pending = PendingIntent.getBroadcast(context, 0, btn2Intent, 0);
120
+
121
+ rv.setOnClickPendingIntent(R.id.btn2_id, btn2Pending);
122
+
123
+
124
+
125
+
126
+
127
+
128
+
129
+ appWidgetManager.updateAppWidget(appWidgetId, rv);
130
+
131
+ }
132
+
133
+ }
134
+
135
+
136
+
137
+ @Override
138
+
139
+ public void onReceive(Context ctx, Intent intent) {
140
+
141
+ super.onReceive(ctx, intent);
142
+
143
+ Log.e(TAG,"[action]" + intent.getAction());
144
+
145
+ }
146
+
147
+
148
+
149
+ @Override
150
+
151
+ public void onEnabled(Context context) {
152
+
153
+ super.onEnabled(context);
154
+
155
+ Log.e(TAG, "[onEnabled]");
156
+
157
+ }
158
+
159
+
160
+
161
+ @Override
162
+
163
+ public void onDisabled(Context context) {
164
+
165
+ super.onDisabled(context);
166
+
167
+ Log.e(TAG, "[onDisabled]");
168
+
169
+ }
170
+
171
+ }
172
+
173
+
174
+
175
+ Person.class
176
+
177
+ public class Person {
178
+
179
+
180
+
181
+ public String getName() {
182
+
183
+ return name;
184
+
185
+ }
186
+
187
+
188
+
189
+ public void setName(String name) {
190
+
191
+ this.name = name;
192
+
193
+ }
194
+
195
+
196
+
197
+ private String name;
198
+
199
+ }
200
+
201
+
202
+
203
+ SampleWidgetService.class
204
+
205
+ public class SampleWidgetService extends RemoteViewsService {
206
+
207
+
208
+
209
+ private static final String TAG = "SampleViewFactory";
210
+
211
+
212
+
213
+ private List<Person> persons = new ArrayList<Person>();
214
+
215
+
216
+
217
+ @Override
218
+
219
+ public RemoteViewsFactory onGetViewFactory(Intent intent) {
220
+
221
+ Log.e(TAG,"[onGetViewFactory]");
222
+
223
+ return new SampleWidgetFactory();
224
+
225
+ }
226
+
227
+
228
+
229
+ private class SampleWidgetFactory implements RemoteViewsFactory {
230
+
231
+
232
+
233
+ public void onCreate() {
234
+
235
+ Log.e(TAG, "[onCreate]");
236
+
237
+
238
+
239
+ if (persons.size() == 0) {
240
+
241
+ Person p1 = new Person();
242
+
243
+ p1.setName("Suzuki");
244
+
245
+ persons.add(p1);
246
+
247
+
248
+
249
+ Person p2 = new Person();
250
+
251
+ p2.setName("Sato");
252
+
253
+ persons.add(p2);
254
+
255
+ }
256
+
257
+ }
258
+
259
+
260
+
261
+ public void onDataSetChanged() {
262
+
263
+ Log.e(TAG, "[onDataSetChanged]");
264
+
265
+ }
266
+
267
+
268
+
269
+ public void onDestroy() {
270
+
271
+ Log.e(TAG, "[onDestroy]");
272
+
273
+ }
274
+
275
+
276
+
277
+ public RemoteViews getViewAt(int position) {
278
+
279
+ Log.e(TAG, "[getViewAt]: " + position);
280
+
281
+
282
+
283
+ RemoteViews rv = null;
284
+
285
+
286
+
287
+ Person p = persons.get(position);
288
+
289
+
290
+
291
+ rv = new RemoteViews(getPackageName(), R.layout.listview_row);
292
+
293
+ rv.setTextViewText(R.id.nameText, p.getName());
294
+
295
+
296
+
297
+ // LISTBUTTONS
298
+
299
+ // R.id.myButtonのイベントを取りたい。setOnClickPendingIntentしてもイベントがとれない
300
+
301
+ Intent btn3Intent = new Intent(NewAppWidget.btn3Filter);
302
+
303
+ PendingIntent btn3Pending = PendingIntent.getBroadcast(getApplicationContext(), 0, btn3Intent, 0);
304
+
305
+ rv.setOnClickPendingIntent(R.id.myButton, btn3Pending);
306
+
307
+
308
+
309
+ return rv;
310
+
311
+ }
312
+
313
+
314
+
315
+ public long getItemId(int position) {
316
+
317
+ Log.e(TAG, "[getItemId]: " + position);
318
+
319
+
320
+
321
+ return position;
322
+
323
+ }
324
+
325
+
326
+
327
+ public int getCount() {
328
+
329
+ Log.e(TAG, "[getCount]");
330
+
331
+
332
+
333
+ return persons.size();
334
+
335
+ }
336
+
337
+
338
+
339
+ public RemoteViews getLoadingView() {
340
+
341
+ Log.e(TAG, "[getLoadingView]");
342
+
343
+
344
+
345
+ return null;
346
+
347
+ }
348
+
349
+
350
+
351
+ public int getViewTypeCount() {
352
+
353
+ Log.e(TAG, "[getViewTypeCount]");
354
+
355
+
356
+
357
+ return 1;
358
+
359
+ }
360
+
361
+
362
+
363
+ public boolean hasStableIds() {
364
+
365
+ Log.e(TAG, "[hasStableIds]");
366
+
367
+
368
+
369
+ return true;
370
+
371
+ }
372
+
373
+ }
374
+
375
+ }
376
+
377
+
378
+
379
+ new_app_widget.xml
380
+
381
+ <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
382
+
383
+ android:layout_width="match_parent"
384
+
385
+ android:layout_height="match_parent"
386
+
387
+ android:orientation="vertical"
388
+
389
+ android:padding="@dimen/widget_margin">
390
+
391
+
392
+
393
+ <Button android:id="@+id/btn1_id"
394
+
395
+ android:layout_width="wrap_content"
396
+
397
+ android:layout_height="wrap_content"
398
+
399
+ android:text="button1" />
400
+
401
+
402
+
403
+ <Button android:id="@+id/btn2_id"
404
+
405
+ android:layout_width="wrap_content"
406
+
407
+ android:layout_height="wrap_content"
408
+
409
+ android:text="button2" />
410
+
411
+
412
+
413
+ <ListView
414
+
415
+ android:id="@+id/listView"
416
+
417
+ android:layout_width="fill_parent"
418
+
419
+ android:layout_height="match_parent"/>
420
+
421
+
422
+
423
+ </LinearLayout>
424
+
425
+
426
+
427
+
428
+
429
+ listview_row.xml
430
+
431
+ <?xml version="1.0" encoding="utf-8"?>
432
+
433
+ <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
434
+
435
+ android:orientation="horizontal"
436
+
437
+ android:layout_width="match_parent"
438
+
439
+ android:layout_height="match_parent">
440
+
441
+
442
+
443
+ <TextView
444
+
445
+ android:id="@+id/nameText"
446
+
447
+ android:layout_weight="1"
448
+
449
+ android:textSize="24sp"
450
+
451
+ android:padding="16dp"
452
+
453
+ android:text="名前"
454
+
455
+ android:layout_width="0dp"
456
+
457
+ android:layout_height="wrap_content"/>
458
+
459
+
460
+
461
+ <Button
462
+
463
+ android:id="@+id/myButton"
464
+
465
+ android:layout_weight="1"
466
+
467
+ android:textSize="24sp"
468
+
469
+ android:text="ボタン"
470
+
471
+ android:layout_width="0dp"
472
+
473
+ android:layout_height="wrap_content"/>
474
+
475
+
476
+
477
+ </LinearLayout>
22
478
 
23
479
  ```
24
480
 
25
- エラーメッセージ
26
-
27
- ```
28
-
29
-
30
-
31
- ###該当のソースコード
32
-
33
- ```ここに言語を入力
34
-
35
-
36
-
37
- AndroidManifest.xml(抜粋)
38
-
39
- <receiver android:name=".NewAppWidget">
40
-
41
- <intent-filter>
42
-
43
- <action android:name="android.appwidget.action.APPWIDGET_UPDATE"/>
44
-
45
- <action android:name="com.android.BUTTON1_CLICKED" />
46
-
47
- <action android:name="com.android.BUTTON2_CLICKED" />
48
-
49
- <action android:name="com.android.BUTTON3_CLICKED" />
50
-
51
- </intent-filter>
52
-
53
-
54
-
55
- <meta-data
56
-
57
- android:name="android.appwidget.provider"
58
-
59
- android:resource="@xml/new_app_widget_info"/>
60
-
61
- </receiver>
62
-
63
-
64
-
65
- <service android:name=".SampleWidgetService"
66
-
67
- android:permission="android.permission.BIND_REMOTEVIEWS">
68
-
69
- </service>
70
-
71
-
72
-
73
- NewAppWidget.class
74
-
75
- public class NewAppWidget extends AppWidgetProvider {
76
-
77
-
78
-
79
- private static final String TAG = "NewAppWidget";
80
-
81
-
82
-
83
- public static final String btn1Filter = "com.android.BUTTON1_CLICKED";
84
-
85
- public static final String btn2Filter = "com.android.BUTTON2_CLICKED";
86
-
87
- public static final String btn3Filter = "com.android.BUTTON3_CLICKED";
88
-
89
-
90
-
91
- @Override
92
-
93
- public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
94
-
95
- super.onUpdate(context, appWidgetManager, appWidgetIds);
96
-
97
-
98
-
99
- Log.e(TAG, "[onUpdate]");
100
-
101
-
102
-
103
- for (int appWidgetId : appWidgetIds) {
104
-
105
-
106
-
107
- Intent remoteViewsFactoryIntent = new Intent(context, SampleWidgetService.class);
108
-
109
- RemoteViews rv = new RemoteViews(context.getPackageName(), R.layout.new_app_widget);
110
-
111
- rv.setRemoteAdapter(R.id.listView, remoteViewsFactoryIntent);
112
-
113
-
114
-
115
- // BUTTON1
116
-
117
- Intent btn1Intent = new Intent(NewAppWidget.btn1Filter);
118
-
119
- PendingIntent btn1Pending = PendingIntent.getBroadcast(context, 0, btn1Intent, 0);
120
-
121
- rv.setOnClickPendingIntent(R.id.btn1_id, btn1Pending);
122
-
123
-
124
-
125
- // BTUUON2
126
-
127
- Intent btn2Intent = new Intent(NewAppWidget.btn2Filter);
128
-
129
- PendingIntent btn2Pending = PendingIntent.getBroadcast(context, 0, btn2Intent, 0);
130
-
131
- rv.setOnClickPendingIntent(R.id.btn2_id, btn2Pending);
132
-
133
-
134
-
135
-
136
-
137
-
138
-
139
- appWidgetManager.updateAppWidget(appWidgetId, rv);
140
-
141
- }
142
-
143
- }
144
-
145
-
146
-
147
- @Override
148
-
149
- public void onReceive(Context ctx, Intent intent) {
150
-
151
- super.onReceive(ctx, intent);
152
-
153
- Log.e(TAG,"[action]" + intent.getAction());
154
-
155
- }
156
-
157
-
158
-
159
- @Override
160
-
161
- public void onEnabled(Context context) {
162
-
163
- super.onEnabled(context);
164
-
165
- Log.e(TAG, "[onEnabled]");
166
-
167
- }
168
-
169
-
170
-
171
- @Override
172
-
173
- public void onDisabled(Context context) {
174
-
175
- super.onDisabled(context);
176
-
177
- Log.e(TAG, "[onDisabled]");
178
-
179
- }
180
-
181
- }
182
-
183
-
184
-
185
- Person.class
186
-
187
- public class Person {
188
-
189
-
190
-
191
- public String getName() {
192
-
193
- return name;
194
-
195
- }
196
-
197
-
198
-
199
- public void setName(String name) {
200
-
201
- this.name = name;
202
-
203
- }
204
-
205
-
206
-
207
- private String name;
208
-
209
- }
210
-
211
-
212
-
213
- SampleWidgetService.class
214
-
215
- public class SampleWidgetService extends RemoteViewsService {
216
-
217
-
218
-
219
- private static final String TAG = "SampleViewFactory";
220
-
221
-
222
-
223
- private List<Person> persons = new ArrayList<Person>();
224
-
225
-
226
-
227
- @Override
228
-
229
- public RemoteViewsFactory onGetViewFactory(Intent intent) {
230
-
231
- Log.e(TAG,"[onGetViewFactory]");
232
-
233
- return new SampleWidgetFactory();
234
-
235
- }
236
-
237
-
238
-
239
- private class SampleWidgetFactory implements RemoteViewsFactory {
240
-
241
-
242
-
243
- public void onCreate() {
244
-
245
- Log.e(TAG, "[onCreate]");
246
-
247
-
248
-
249
- if (persons.size() == 0) {
250
-
251
- Person p1 = new Person();
252
-
253
- p1.setName("Suzuki");
254
-
255
- persons.add(p1);
256
-
257
-
258
-
259
- Person p2 = new Person();
260
-
261
- p2.setName("Sato");
262
-
263
- persons.add(p2);
264
-
265
- }
266
-
267
- }
268
-
269
-
270
-
271
- public void onDataSetChanged() {
272
-
273
- Log.e(TAG, "[onDataSetChanged]");
274
-
275
- }
276
-
277
-
278
-
279
- public void onDestroy() {
280
-
281
- Log.e(TAG, "[onDestroy]");
282
-
283
- }
284
-
285
-
286
-
287
- public RemoteViews getViewAt(int position) {
288
-
289
- Log.e(TAG, "[getViewAt]: " + position);
290
-
291
-
292
-
293
- RemoteViews rv = null;
294
-
295
-
296
-
297
- Person p = persons.get(position);
298
-
299
-
300
-
301
- rv = new RemoteViews(getPackageName(), R.layout.listview_row);
302
-
303
- rv.setTextViewText(R.id.nameText, p.getName());
304
-
305
-
306
-
307
- // LISTBUTTONS
308
-
309
- // R.id.myButtonのイベントを取りたい
310
-
311
- Intent btn3Intent = new Intent(NewAppWidget.btn3Filter);
312
-
313
- PendingIntent btn3Pending = PendingIntent.getBroadcast(getApplicationContext(), 0, btn3Intent, 0);
314
-
315
- rv.setOnClickPendingIntent(R.id.myButton, btn3Pending);
316
-
317
-
318
-
319
- return rv;
320
-
321
- }
322
-
323
-
324
-
325
- public long getItemId(int position) {
326
-
327
- Log.e(TAG, "[getItemId]: " + position);
328
-
329
-
330
-
331
- return position;
332
-
333
- }
334
-
335
-
336
-
337
- public int getCount() {
338
-
339
- Log.e(TAG, "[getCount]");
340
-
341
-
342
-
343
- return persons.size();
344
-
345
- }
346
-
347
-
348
-
349
- public RemoteViews getLoadingView() {
350
-
351
- Log.e(TAG, "[getLoadingView]");
352
-
353
-
354
-
355
- return null;
356
-
357
- }
358
-
359
-
360
-
361
- public int getViewTypeCount() {
362
-
363
- Log.e(TAG, "[getViewTypeCount]");
364
-
365
-
366
-
367
- return 1;
368
-
369
- }
370
-
371
-
372
-
373
- public boolean hasStableIds() {
374
-
375
- Log.e(TAG, "[hasStableIds]");
376
-
377
-
378
-
379
- return true;
380
-
381
- }
382
-
383
- }
384
-
385
- }
386
-
387
-
388
-
389
- new_app_widget.xml
390
-
391
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
392
-
393
- android:layout_width="match_parent"
394
-
395
- android:layout_height="match_parent"
396
-
397
- android:orientation="vertical"
398
-
399
- android:padding="@dimen/widget_margin">
400
-
401
-
402
-
403
- <Button android:id="@+id/btn1_id"
404
-
405
- android:layout_width="wrap_content"
406
-
407
- android:layout_height="wrap_content"
408
-
409
- android:text="button1" />
410
-
411
-
412
-
413
- <Button android:id="@+id/btn2_id"
414
-
415
- android:layout_width="wrap_content"
416
-
417
- android:layout_height="wrap_content"
418
-
419
- android:text="button2" />
420
-
421
-
422
-
423
- <ListView
424
-
425
- android:id="@+id/listView"
426
-
427
- android:layout_width="fill_parent"
428
-
429
- android:layout_height="match_parent"/>
430
-
431
-
432
-
433
- </LinearLayout>
434
-
435
-
436
-
437
-
438
-
439
- listview_row.xml
440
-
441
- <?xml version="1.0" encoding="utf-8"?>
442
-
443
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
444
-
445
- android:orientation="horizontal"
446
-
447
- android:layout_width="match_parent"
448
-
449
- android:layout_height="match_parent">
450
-
451
-
452
-
453
- <TextView
454
-
455
- android:id="@+id/nameText"
456
-
457
- android:layout_weight="1"
458
-
459
- android:textSize="24sp"
460
-
461
- android:padding="16dp"
462
-
463
- android:text="名前"
464
-
465
- android:layout_width="0dp"
466
-
467
- android:layout_height="wrap_content"/>
468
-
469
-
470
-
471
- <Button
472
-
473
- android:id="@+id/myButton"
474
-
475
- android:layout_weight="1"
476
-
477
- android:textSize="24sp"
478
-
479
- android:text="ボタン"
480
-
481
- android:layout_width="0dp"
482
-
483
- android:layout_height="wrap_content"/>
484
-
485
-
486
-
487
- </LinearLayout>
488
-
489
- ```
490
-
491
481
 
492
482
 
493
483
  ###試したこと