2014-10-26 45 views
1

我有一個有一個列表視圖的小部件。我正在嘗試將事件綁定到listview中的listitem。我已經通過了幾個代碼示例,它們都不起作用!安卓部件列表查看可點擊

//控件提供者

public class Widget extends AppWidgetProvider 
{ 

    public static String YOUR_AWESOME_ACTION = "com.my.assignmentmanager.YourAwesomeAction"; 

    @Override 
    public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) 
    { 
     for (int i = 0; i < appWidgetIds.length; i++) 
     { 
      Intent svcIntent = new Intent(context, WidgetService.class); 

      svcIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetIds[i]); 
      svcIntent.setData(Uri.parse(svcIntent.toUri(Intent.URI_INTENT_SCHEME))); 

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

      widget.setRemoteAdapter(appWidgetIds[i], R.id.widgetListView, svcIntent); 




      Intent toastIntent = new Intent(context, Widget.class); 
      // Set the action for the intent. 
      // When the user touches a particular view, it will have the effect of 
      // broadcasting TOAST_ACTION. 
      toastIntent.setAction(Widget.YOUR_AWESOME_ACTION);; 
      toastIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetIds[i]); 


      svcIntent.setData(Uri.parse(svcIntent.toUri(Intent.URI_INTENT_SCHEME))); 

      PendingIntent toastPendingIntent = PendingIntent.getBroadcast(context, 0, toastIntent,PendingIntent.FLAG_UPDATE_CURRENT);    

      widget.setPendingIntentTemplate(R.id.widgetListView, toastPendingIntent); 



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

     super.onUpdate(context, appWidgetManager, appWidgetIds); 
    } 

上接收方法!應該可以扔登錄

@Override 
    public void onReceive(Context context, Intent intent) 
    { 
     Log.d("debug", "onReceive"); 
     super.onReceive(context, intent); 

     Log.d("debug", "onReceive"); 

    }  

} 

小部件佈局XML

<?xml version="1.0" encoding="utf-8"?> 
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

    <ListView 
     android:id="@+id/widgetListView" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_weight="1" > 
    </ListView> 

    <TextView 
     android:id="@+id/empty_view" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:gravity="center" 
     android:text="wqeqweqwew" 
     android:textColor="#ffffff" 
     android:textSize="20sp" 
     android:visibility="gone" /> 

</FrameLayout> 

的Widget工廠的getViewAt方法

  @Override 
public RemoteViews getViewAt(int rowNumber) 
{ 

    RemoteViews remoteView = new RemoteViews(mContext.getPackageName(), R.layout.widget_listitem); 
    remoteView.setTextViewText(R.id.widgetAssignmentTitle, mWidgetItems.get(0).assignmentTitle); 
    remoteView.setTextViewText(R.id.widgetDaysLeft, mWidgetItems.get(0).assignmentTitle); 

    Log.d("debug", "wwwwwwwwwwwwwwwwww"); 
    Bundle extras = new Bundle(); 
    extras.putInt("KEY", rowNumber); 
    Intent fillIntent = new Intent(); 
    fillIntent.putExtra("Water", rowNumber); 

    remoteView.setOnClickFillInIntent(R.layout.widget_listitem, fillIntent); 

    return (remoteView); 
} 

我不知道,但結合只是一個點擊就是這樣一個頭痛的多媒體遊戲我能夠綁定數據,但無法綁定點擊事件。

+0

有你能解決這個問題?我有同樣的問題 – natsumiyu 2016-09-20 08:57:35

回答

1

嘗試修改此:

remoteView.setOnClickFillInIntent(R.layout.widget_listitem, fillIntent); 

喜歡的東西:

remoteView.setOnClickFillInIntent(R.id.listitemview, fillIntent); 
+0

嗨我有同樣的問題,你能幫我http://stackoverflow.com/questions/39587137/how-to-get-the-clicked-item-from-listview-of- a-widget-android謝謝 – natsumiyu 2016-09-21 08:00:46