2

初めてのアプリウィジェットを作成しようとしていますが、アイテムがクリックされても動作しないようにしたいと考えています。アプリウィジェット(remoteviews)のリストビュー項目をクリックしてアクティビティを起動しない

Intent toastIntent = new Intent(context, TareaModificar.class); 
toastIntent.putExtra("id", id); 
PendingIntent toastPendingIntent = PendingIntent.getBroadcast(context, 0, toastIntent, 
PendingIntent.FLAG_UPDATE_CURRENT); 
    remoteViews.setPendingIntentTemplate(R.id.widget_tareas_listview, toastPendingIntent); 

と私getViewAtからのコード():

Bundle extras = new Bundle(); 
    extras.putInt(WidgetTareas.id, cursor.getInt(0)); 
    Intent fillInIntent = new Intent(); 
    fillInIntent.putExtras(extras); 
    remoteView.setOnClickFillInIntent(R.id.widget_tareas, fillInIntent); 

idはウィジェットプロバイダーのグローバル文字列変数です は、ここに私にonUpdate(のコード)です。

EDIT私はすべてのコードを与えているは、多分この方法は、それを助けるために簡単です:

AppWidgetProviderコード:

public class WidgetTareas extends AppWidgetProvider { 
private String TAG = getClass().getSimpleName(); 
private DbAdapter dbAdapter; 
public static final String id = "1"; 

public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) { 
    for (int i = 0; i < appWidgetIds.length; ++i) { 
     Log.d(TAG,"Se actualiza el widget: " +i); 
     RemoteViews remoteViews = updateWidgetListView(context, 
       appWidgetIds[i]); 
     appWidgetManager.updateAppWidget(appWidgetIds[i], 
       remoteViews); 
    } 
    super.onUpdate(context, appWidgetManager, appWidgetIds); 
} 

private RemoteViews updateWidgetListView(Context context, 
             int appWidgetId) { 
    dbAdapter = new DbAdapter(context); 
    dbAdapter.actualizaQueda(0); 
    //which layout to show on widget 
    RemoteViews remoteViews = new RemoteViews(context.getPackageName(),R.layout.widget_tareas); 

    //RemoteViews Service needed to provide adapter for ListView 
    Intent svcIntent = new Intent(context, WidgetTareasService.class); 
    //passing app widget id to that RemoteViews Service 
    svcIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId); 
    //setting a unique Uri to the intent 
    //don't know its purpose to me right now 
    svcIntent.setData(Uri.parse(
      svcIntent.toUri(Intent.URI_INTENT_SCHEME))); 
    //setting adapter to listview of the widget 
    remoteViews.setRemoteAdapter(appWidgetId, R.id.widget_tareas_listview, 
      svcIntent); 

    Intent toastIntent = new Intent(context, TareaModificar.class); 
    toastIntent.putExtra("id", id); 
    toastIntent.setAction("ACTION_SHOW_TOAST"); 
    PendingIntent toastPendingIntent = PendingIntent.getBroadcast(context, 0, toastIntent, 
      PendingIntent.FLAG_UPDATE_CURRENT); 
    remoteViews.setPendingIntentTemplate(R.id.widget_tareas_listview, toastPendingIntent); 
    return remoteViews; 
} 

@Override 
public void onReceive(@NonNull Context context, @NonNull Intent intent) { 
    super.onReceive(context, intent); 
    Toast.makeText(context, "Intent received", Toast.LENGTH_SHORT).show(); 
    switch (intent.getAction()) { 
     case "ACTION_SHOW_TOAST": 
      //change your activity name 
      int id = intent.getIntExtra("id", -1); 
      context.startActivity(new Intent(context, 
        TareaModificar.class) 
        .setFlags(
          Intent.FLAG_ACTIVITY_NEW_TASK)); 
      break; 
    } 
} 

}

RemoteViewsFactoryコード:

@Override 
public RemoteViews getViewAt(int position) { 
    if (position == 0) 
     cursor = dbAdapter.getTareasOrdenadoHacer(0); 

    RemoteViews remoteView = new RemoteViews(
      context.getPackageName(), R.layout.a); 
    Log.d(TAG,"Se carga la factoría"); 
    cursor.moveToPosition(position); 
    String a = cursor.getString(cursor.getColumnIndex(dbAdapter.COL_A)); 
    remoteView.setTextViewText(R.id.tarea_widget_aa, dbAdapter.getAA(a)); 
    remoteView.setTextViewText(R.id.tarea_widget_tt, cursor.getString(cursor.getColumnIndex(dbAdapter.COL_T))); 
    String queda = context.getString(R.string.en) +" "+ cursor.getString(cursor.getColumnIndex(dbAdapter.COL_Q)) +" "+context.getString(R.string.dias); 
    remoteView.setTextViewText(R.id.tarea_widget_q, queda); 

    remoteView.setTextViewText(R.id.tarea_widget_pos, position); 


    // Next, we set a fill-intent which will be used to fill-in the pending intent template 
    // which is set on the collection view in StackWidgetProvider. 
    Bundle extras = new Bundle(); 
    extras.putInt(WidgetTareas.id, cursor.getInt(0)); 
    Intent fillInIntent = new Intent(); 
    fillInIntent.putExtras(extras); 
    remoteView.setOnClickFillInIntent(R.id.widget_tareas, fillInIntent); 
    return remoteView; 
} 

ウィジェットのレイアウトコード:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:orientation="vertical" android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:id="@+id/widget_tareas" 
android:background="@color/common_action_bar_splitter"> 
<TextView 
    android:padding="10dp" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:textColor="@color/black" 
    android:textSize="18sp" 
    android:background="#E6E6E6" 
    android:text="@string/tareas"/> 
<ListView 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:id="@+id/widget_tareas_listview"/> 

レイアウトコード:

<LinearLayout 
xmlns:android="http://schemas.android.com/apk/res/android" 
android:orientation="horizontal" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:padding="10dp" 
android:id="@+id/widget_tareas_item"> 
<LinearLayout 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:orientation="vertical"> 
    <TextView 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:textColor="@color/black" 
     android:text="99" 
     android:gravity="center_horizontal" 
     android:id="@+id/tarea_widget_q"/> 
    <TextView 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:textColor="@color/black" 
     android:gravity="center_horizontal" 
     android:text="pos" 
     android:id="@+id/tarea_widget_pos"/> 
</LinearLayout> 

<LinearLayout 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:paddingLeft="10dp" 
    android:orientation="vertical"> 
    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="New Text" 
     android:textColor="@color/black" 
     android:id="@+id/tarea_widget_aa" /> 
    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:textColor="@color/black" 
     android:text="sad" 
     android:id="@+id/tarea_widget_tt" /> 
</LinearLayout> 

+0

現在は間違っていますか? –

+0

@ JhamanDasウィジェットからアイテムをクリックしても何も起きません。アクティビティを開始します。TareaModificar – androidjunior

答えて

0

あなたのウィジェットプロバイダのonReceiveでアクションを管理する必要があります。

がgetViewAt方法であなたの意図を準備します

Intent toastIntent = new Intent(context, TareaModificar.class); 
toastIntent.putExtra("id", id); 
toastIntent.setAction("ACTION_SHOW_TOAST"); 
PendingIntent toastPendingIntent = PendingIntent.getBroadcast(context, 0, toastIntent, 
PendingIntent.FLAG_UPDATE_CURRENT); 
    remoteViews.setPendingIntentTemplate(R.id.widget_tareas_listview, toastPendingIntent); 

そして、あなたのonReceiveで、それを管理:

@Override 
    public void onReceive(@NonNull Context context, @NonNull Intent intent) { 
     super.onReceive(context, intent); 
     switch (intent.getAction()) { 

      case "ACTION_SHOW_TOAST": 
        //change your activity name 
        int id = intent.getIntExtra("id", -1); 
        context.startActivity(new Intent(context, 
        TareaModificar.class) 
        .setFlags(
        Intent.FLAG_ACTIVITY_NEW_TASK) 

       break; 
     } 
    } 
+0

1.コードの最初の部分がgetViewAt()に移動していますか?このメソッドは、プロバイダではなくFactoryにあるため、onUpdate()メソッドで言うとしたらどうでしょうか? 2。AndroidスタジオはConstants(SyncStateContract)のクラスをインポートするように求めていますが、インポートするとACTION_SHOW_GCM_NEWSが見つかりません。何が間違っているのですか? – androidjunior

+0

@androidjunior私の悪い、私のコードの一部、それを削除することを忘れました。 –

+0

修正されたコード。それを試してみてください。必要に応じて、ウィジェットプロバイダにlistviewを提供できます。コードはonUpdateにある可能性があります。 –

1

は、だから私は最終的にそれを動作させることができました。 )メソッドにonUpdate(中

public static final String TAREA_MODIFICAR = "TAREAMODIFICAR"; 
public static final String EXTRA_ITEM = "1"; 

を::

WidgetTareasクラスでは、追加:何私が行っていることはhereからコードを取得し、最小の変更、そうです

Intent toastIntent = new Intent(context, WidgetTareas.class); 
     toastIntent.setAction(WidgetTareas.TAREA_MODIFICAR); 
     toastIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetIds[i]); 
     PendingIntent toastPendingIntent = PendingIntent.getBroadcast(context, 0, toastIntent, 
       PendingIntent.FLAG_UPDATE_CURRENT); 
     remoteViews.setPendingIntentTemplate(R.id.widget_tareas_listview, toastPendingIntent); 
     appWidgetManager.updateAppWidget(appWidgetIds[i], remoteViews); 

方法onReceive:

public void onReceive(Context context, Intent intent) { 
    AppWidgetManager mgr = AppWidgetManager.getInstance(context); 
    if (intent.getAction().equals(TAREA_MODIFICAR)) { 
     int appWidgetId = intent.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, 
       AppWidgetManager.INVALID_APPWIDGET_ID); 
     int viewIndex = intent.getIntExtra(EXTRA_ITEM, 0); 
     Intent i = new Intent(context, TareaModificar.class); 
     i.putExtra("id",String.valueOf(viewIndex)); 
     i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
     context.startActivity(i); 
     Toast.makeText(context, "Vista pulsada es: " + viewIndex, Toast.LENGTH_SHORT).show(); 
    } 
    super.onReceive(context, intent); 
} 

最後に、WidgetTareasServiceクラス:

Bundle extras = new Bundle(); 
    extras.putInt(WidgetTareas.EXTRA_ITEM, cursor.getInt(0)); 
    Intent fillInIntent = new Intent(); 
    fillInIntent.putExtras(extras); 
    remoteView.setOnClickFillInIntent(R.id.widget_tareas_item, fillInIntent); 
関連する問題