android widget listview可点击

时间:2014-10-26 19:17:19

标签: android listview android-listview widget

我有一个包含listview的小部件。我试图将事件绑定到listview中的listitem。我已经通过几个代码示例,它们都不起作用!

// widget provider

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);
}

我不知道但只是点击一下就是Android上的头疼!我能够绑定数据但不能绑定click事件。

1 个答案:

答案 0 :(得分:1)

尝试更改此内容:

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

类似于:

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