【问题标题】:How to show listiview in app widget android如何在应用小部件android中显示列表视图
【发布时间】:2014-04-04 05:51:11
【问题描述】:

我正在尝试为显示列表视图的锁屏制作应用小部件。 但我在 appwidget 中显示列表视图时遇到问题。

【问题讨论】:

  • @Apoorva Faldu 这没有帮助。实际上您提供的代码仅有助于更新小部件。实际上我想更新应用小部件的所有列表。

标签: android android-listview android-widget widget


【解决方案1】:

CommonsGuy 在 GitHub 中有一个在主屏幕小部件中使用 ListView 的示例:

https://github.com/commonsguy/cw-advandroid/tree/master/AppWidget/LoremWidget

【讨论】:

  • 哦,谢谢。我还想在 xml 布局中为小部件创建一个按钮,并希望刷新应用小部件的整个列表视图。我该怎么做?
【解决方案2】:

@saugat man ligal - 您可以从上面的链接中找到 Listview 演示:

https://github.com/commonsguy/cw-advandroid/tree/master/AppWidget/LoremWidget

您可以使用 Button 来同步或刷新您的列表视图,如下所示:

package com.automatic.widget;

import android.app.PendingIntent;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.widget.RemoteViews;

public class Widget extends AppWidgetProvider {

private static final String SYNC_CLICKED    = "automaticWidgetSyncButtonClick";

@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
    RemoteViews remoteViews;
    ComponentName watchWidget;

    remoteViews = new RemoteViews(context.getPackageName(), R.layout.widget_layout);
    watchWidget = new ComponentName(context, Widget.class);

    remoteViews.setOnClickPendingIntent(R.id.sync_button, getPendingSelfIntent(context, SYNC_CLICKED));
    appWidgetManager.updateAppWidget(watchWidget, remoteViews);
}

@Override
public void onReceive(Context context, Intent intent) {
    // TODO Auto-generated method stub
    super.onReceive(context, intent);

    if (SYNC_CLICKED.equals(intent.getAction())) {

        AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);

        RemoteViews remoteViews;
        ComponentName watchWidget;

        remoteViews = new RemoteViews(context.getPackageName(), R.layout.widget_layout);
        watchWidget = new ComponentName(context, Widget.class);

        remoteViews.setTextViewText(R.id.sync_button, "TESTING");

        appWidgetManager.updateAppWidget(watchWidget, remoteViews);

    }
}

protected PendingIntent getPendingSelfIntent(Context context, String action) {
    Intent intent = new Intent(context, getClass());
    intent.setAction(action);
    return PendingIntent.getBroadcast(context, 0, intent, 0);
}
}

所以现在你可以集成它并使 Refresh ListView 工作......希望这会有所帮助

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-01-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-09-15
    • 1970-01-01
    相关资源
    最近更新 更多