列表视图和按钮单击窗口小部件[Android]

时间:2016-08-31 00:15:04

标签: android listview widget

我创建的Widget有一个Button,而下面有一个ListView。单击按钮时,我想打开Activity,但似乎无效,我不知道为什么。

以下是我的AppWidgetProvider

中的代码
 @Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager,
                     int[] appWidgetIds) {
    final int N = appWidgetIds.length;

    for (int i = 0; i < N; ++i) {
        RemoteViews remoteViews = updateWidgetListView(context,
                appWidgetIds[i]);
        appWidgetManager.updateAppWidget(appWidgetIds[i], remoteViews);


    Intent configIntent = new Intent(context, NewActivity.class);
    PendingIntent configPendingIntent = PendingIntent.getActivity(context, 0, configIntent, 0);
    remoteViews.setOnClickPendingIntent(R.id.btn_activity, configPendingIntent);
    }



    super.onUpdate(context, appWidgetManager, appWidgetIds);

} 

private RemoteViews updateWidgetListView(Context context, int appWidgetId) {

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

    Intent svcIntent = new Intent(context, WidgetService.class);
    svcIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
    svcIntent.setData(Uri.parse(svcIntent.toUri(Intent.URI_INTENT_SCHEME)));

    remoteViews.setRemoteAdapter(appWidgetId, R.id.listViewWidget,
            svcIntent);

    remoteViews.setEmptyView(R.id.listViewWidget, R.id.empty_view);

    return remoteViews;
}

我认为这是因为我的updateWidgetListView,我尝试将Intent放在updateWidgetListView内,但单击按钮时仍然无效。

提前谢谢你。

1 个答案:

答案 0 :(得分:0)

在我的onUpdate我在widget_layout

之前初始化我的iteration
public void onUpdate(Context context, AppWidgetManager appWidgetManager,
                     int[] appWidgetIds) {

    // initializing widget layout
    RemoteViews rvBtn = new RemoteViews(context.getPackageName(),
            R.layout.home_widget);

    // register the button event
    rvBtn.setOnClickPendingIntent(R.id. btn_activity,
            buildButtonPendingIntent(context));

    final int N = appWidgetIds.length;

    for (int i = 0; i < N; ++i) {
     //initialize another RemoteViews for ListView
        RemoteViews remoteViews = updateWidgetListView(context,
                appWidgetIds[i]);
        appWidgetManager.updateAppWidget(appWidgetIds[i], remoteViews);
    }


    super.onUpdate(context, appWidgetManager, appWidgetIds);

}

然后创建BroadcastReceiver,我将调用Intent并在receiver上添加AndroidManifest