2014-10-26 46 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