###前提・実現したいこと
ウィジェットにListViewを配置し、1行毎に名前とボタンを表示するサンプルアプリを作っています。(イメージ参照)
ListViewの表示まで出来たのですが、rowのボタンをタップしてもイベントが取得できません。
イメージのBUTTON1とBUTTON2のタップイベントは取得できました。
SuzukiとSatoの横にあるボタンのイベント取得方法を教えて下さい
(将来的にはSuzukiのボタンをタップしたらSuzukiさんとtextviewを変えたい)
###該当のソースコード
AndroidManifest.xml(抜粋) <receiver android:name=".NewAppWidget"> <intent-filter> <action android:name="android.appwidget.action.APPWIDGET_UPDATE"/> <action android:name="com.android.BUTTON1_CLICKED" /> <action android:name="com.android.BUTTON2_CLICKED" /> <action android:name="com.android.BUTTON3_CLICKED" /> </intent-filter> <meta-data android:name="android.appwidget.provider" android:resource="@xml/new_app_widget_info"/> </receiver> <service android:name=".SampleWidgetService" android:permission="android.permission.BIND_REMOTEVIEWS"> </service> NewAppWidget.class public class NewAppWidget extends AppWidgetProvider { private static final String TAG = "NewAppWidget"; public static final String btn1Filter = "com.android.BUTTON1_CLICKED"; public static final String btn2Filter = "com.android.BUTTON2_CLICKED"; public static final String btn3Filter = "com.android.BUTTON3_CLICKED"; @Override public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) { super.onUpdate(context, appWidgetManager, appWidgetIds); Log.e(TAG, "[onUpdate]"); for (int appWidgetId : appWidgetIds) { Intent remoteViewsFactoryIntent = new Intent(context, SampleWidgetService.class); RemoteViews rv = new RemoteViews(context.getPackageName(), R.layout.new_app_widget); rv.setRemoteAdapter(R.id.listView, remoteViewsFactoryIntent); // BUTTON1 このイベントは取得できました Intent btn1Intent = new Intent(NewAppWidget.btn1Filter); PendingIntent btn1Pending = PendingIntent.getBroadcast(context, 0, btn1Intent, 0); rv.setOnClickPendingIntent(R.id.btn1_id, btn1Pending); // BTUUON2 このイベントは取得できました Intent btn2Intent = new Intent(NewAppWidget.btn2Filter); PendingIntent btn2Pending = PendingIntent.getBroadcast(context, 0, btn2Intent, 0); rv.setOnClickPendingIntent(R.id.btn2_id, btn2Pending); appWidgetManager.updateAppWidget(appWidgetId, rv); } } @Override public void onReceive(Context ctx, Intent intent) { super.onReceive(ctx, intent); Log.e(TAG,"[action]" + intent.getAction()); } @Override public void onEnabled(Context context) { super.onEnabled(context); Log.e(TAG, "[onEnabled]"); } @Override public void onDisabled(Context context) { super.onDisabled(context); Log.e(TAG, "[onDisabled]"); } } Person.class public class Person { public String getName() { return name; } public void setName(String name) { this.name = name; } private String name; } SampleWidgetService.class public class SampleWidgetService extends RemoteViewsService { private static final String TAG = "SampleViewFactory"; private List<Person> persons = new ArrayList<Person>(); @Override public RemoteViewsFactory onGetViewFactory(Intent intent) { Log.e(TAG,"[onGetViewFactory]"); return new SampleWidgetFactory(); } private class SampleWidgetFactory implements RemoteViewsFactory { public void onCreate() { Log.e(TAG, "[onCreate]"); if (persons.size() == 0) { Person p1 = new Person(); p1.setName("Suzuki"); persons.add(p1); Person p2 = new Person(); p2.setName("Sato"); persons.add(p2); } } public void onDataSetChanged() { Log.e(TAG, "[onDataSetChanged]"); } public void onDestroy() { Log.e(TAG, "[onDestroy]"); } public RemoteViews getViewAt(int position) { Log.e(TAG, "[getViewAt]: " + position); RemoteViews rv = null; Person p = persons.get(position); rv = new RemoteViews(getPackageName(), R.layout.listview_row); rv.setTextViewText(R.id.nameText, p.getName()); // LISTBUTTONS // R.id.myButtonのイベントを取りたい。setOnClickPendingIntentしてもイベントがとれない Intent btn3Intent = new Intent(NewAppWidget.btn3Filter); PendingIntent btn3Pending = PendingIntent.getBroadcast(getApplicationContext(), 0, btn3Intent, 0); rv.setOnClickPendingIntent(R.id.myButton, btn3Pending); return rv; } public long getItemId(int position) { Log.e(TAG, "[getItemId]: " + position); return position; } public int getCount() { Log.e(TAG, "[getCount]"); return persons.size(); } public RemoteViews getLoadingView() { Log.e(TAG, "[getLoadingView]"); return null; } public int getViewTypeCount() { Log.e(TAG, "[getViewTypeCount]"); return 1; } public boolean hasStableIds() { Log.e(TAG, "[hasStableIds]"); return true; } } } new_app_widget.xml <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:padding="@dimen/widget_margin"> <Button android:id="@+id/btn1_id" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="button1" /> <Button android:id="@+id/btn2_id" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="button2" /> <ListView android:id="@+id/listView" android:layout_width="fill_parent" android:layout_height="match_parent"/> </LinearLayout> listview_row.xml <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:id="@+id/nameText" android:layout_weight="1" android:textSize="24sp" android:padding="16dp" android:text="名前" android:layout_width="0dp" android:layout_height="wrap_content"/> <Button android:id="@+id/myButton" android:layout_weight="1" android:textSize="24sp" android:text="ボタン" android:layout_width="0dp" android:layout_height="wrap_content"/> </LinearLayout>
###試したこと
以下のページを参考に作成しました
Androidでの簡易なWidget作り方 - Qiita
Y.A.M の 雑記帳: Android AppWidget
AppWidgetのクリックイベントを取得する - Androidプログラマへの道 ~ Moonlight 明日香 ~
###補足情報(言語/FW/ツール等のバージョンなど)
AndroidStudio2.1
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2016/05/24 03:17