2017-09-01 7 views
0

J'ai vu des questions liées à ce problème, mais je suis incapable de comprendre pourquoi cela ne fonctionne pas pour moi.Gestion de la liste des widgets Android cliquez sur

Je m'attends à ce que la méthode onReceive() dans la classe Widget Provider soit appelée lorsque l'on clique sur l'élément de liste, mais rien ne se passe.

Certains commentaires sont inclus, et ils montrent ce que j'ai essayé aussi.

Code pertinent:

mise en page d'application principale a ListView défini comme ceci:

<ListView 
     android:id="@+id/events_list" 
     style="@android:style/TextAppearance.Small" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" /> 

Row est défini avec RelativeLayout avec deux configurations linéaires imbriquées, pas sûr si cela est important de partager.

getViewAt() est mis en œuvre par l'usine de vue et est ici la fonction:

@Override 
    public RemoteViews getViewAt(int position) { 

     if (events_ack == null) { 
      Log.e("LIST", "getViewAt (null) events_ack"); 
      return null; 
     } 

     RemoteViews row = new RemoteViews(context.getPackageName(), R.layout.row); 

     row.setTextViewText(R.id.event_name, events_ack[position].name); 
     row.setTextViewText(R.id.description, events_ack[position].description); 
     row.setTextViewText(R.id.publicTV, (events_ack[position].is_public == 0) ? "Public: False" : "Public: True"); 
     row.setTextViewText(R.id.timeTV, events_ack[position].time); 
     row.setViewVisibility(R.id.price, View.GONE); 

     // download and set image BONUS QUESTION- does not work through AsyncTask using future insted (images are downloaded but not shown). 

     row.setImageViewBitmap(R.id.image, pic); 

     Bundle extras = new Bundle(); 
     extras.putInt(CasusWidgetProvider.EXTRA_WORD, events_ack[position].id); 
     Intent fillInIntent = new Intent(); 
     fillInIntent.putExtras(extras); 
     // Make it possible to distinguish the individual on-click 
     // action of a given item 
     row.setOnClickFillInIntent(R.id.events_list, fillInIntent); 

     //extras.putInt(CasusWidgetProvider.EXTRA_WORD, events_ack[position].id); 
     //i.putExtras(extras); 
     //i.setAction(CasusWidgetProvider.ITEM_CLICK); 
     //row.setOnClickFillInIntent(R.layout.casus_widget, i); //event_name? 

     // ?? 

     AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context); 
     RemoteViews rv = new RemoteViews(context.getPackageName(), R.layout.casus_widget); 
     appWidgetManager.updateAppWidget(appWidgetId, rv); 
     //appWidgetManager.updateAppWidget(appWidgetId, row); 

     return row; 
    } 

classe Provider Widget a méthode onUpdate():

@Override 
    public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) { 

     Log.w("CASUS", "UPDATE function called"); 


     for (int i = 0; i < appWidgetIds.length; i++) { 

      Intent intent = new Intent(context, WidgetService.class); 
      intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetIds[i]); 
      intent.setData(Uri.parse(intent.toUri(Intent.URI_INTENT_SCHEME))); 
      RemoteViews rv = new RemoteViews(context.getPackageName(), R.layout.casus_widget); 
      rv.setRemoteAdapter(appWidgetIds[i], R.id.events_list, intent); 

      Intent clickIntent=new Intent(context, WidgetService.class); 
      clickIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetIds[i]); 
      clickIntent.setData(Uri.parse(intent.toUri(Intent.URI_INTENT_SCHEME))); 
      clickIntent.setAction(ITEM_CLICK); 

      PendingIntent clickPI = PendingIntent.getActivity(context, 0, clickIntent, PendingIntent.FLAG_UPDATE_CURRENT); 
      rv.setPendingIntentTemplate(R.id.events_list, clickPI); 
      appWidgetManager.notifyAppWidgetViewDataChanged(appWidgetIds[i], R.id.events_list); //? 

      clickIntent = new Intent(context, CasusWidgetProvider.class); 
      clickIntent.setAction(UPDATE_LIST); 
      clickIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, appWidgetIds); 


      PendingIntent pendingIntentRefresh = PendingIntent.getBroadcast(context, 0, clickIntent, 0); 
      rv.setOnClickPendingIntent(R.id.update, pendingIntentRefresh); 

      appWidgetManager.updateAppWidget(appWidgetIds[i], rv); 
     } 

     fetchData(context, appWidgetIds); 

     super.onUpdate(context, appWidgetManager, appWidgetIds); 

    } 

Répondre

0

Je l'ai compris. fonction onUpdate() dans la classe de fournisseur widget a été modifié pour ressembler à ceci:

@Override 
    public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) { 
     for (int i = 0; i < appWidgetIds.length; i++) { 
      Intent intent = new Intent(context, WidgetService.class); 
      intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetIds[i]); 
      intent.setData(Uri.parse(intent.toUri(Intent.URI_INTENT_SCHEME))); 
      RemoteViews rv = new RemoteViews(context.getPackageName(), R.layout.casus_widget); 
      rv.setRemoteAdapter(appWidgetIds[i], R.id.events_list, intent); 

      appWidgetManager.updateAppWidget(appWidgetIds[i], rv); 
     } 

     fetchData(context, appWidgetIds); 

     super.onUpdate(context, appWidgetManager, appWidgetIds); 

    } 

En fonction getViewAt() sur l'usine widget cette ligne:

row.setOnClickFillInIntent(R.id.events_list, fillInIntent); 

a été modifié pour correspondre ID correct:

row.setOnClickFillInIntent(R.id.row_layout, fillInIntent); 

le modèle de gestionnaire de clic est défini dans la classe qui fait la requête HTTP, essentiellement:

for (int widgetId : allWidgetIds) { 
      RemoteViews remoteViews = new RemoteViews(this.getApplicationContext().getPackageName(), R.layout.casus_widget); 


      Intent svcIntent = new Intent(this.getApplicationContext(), WidgetService.class); 
      svcIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, widgetId); 
      svcIntent.putExtra("events", events); 
      svcIntent.setData(Uri.parse(svcIntent.toUri(Intent.URI_INTENT_SCHEME))); 
      remoteViews.setRemoteAdapter(widgetId, R.id.events_list, svcIntent); 


      Intent clickIntent = new Intent(this.getApplicationContext(), CasusWidgetProvider.class); 
      clickIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, allWidgetIds); 
      clickIntent.setData(Uri.parse(intent.toUri(Intent.URI_INTENT_SCHEME))); 
      clickIntent.setAction(CasusWidgetProvider.ITEM_CLICK); 


      PendingIntent toastPendingIntent = PendingIntent.getBroadcast(this.getApplicationContext(), 0, clickIntent, PendingIntent.FLAG_UPDATE_CURRENT); 
      remoteViews.setPendingIntentTemplate(R.id.events_list, toastPendingIntent); 

      appWidgetManager.updateAppWidget(widgetId, remoteViews); 

     } 

Espérons que cela pourrait aider ..