如何在app widget android中显示listview

时间:2014-04-04 05:51:12

标签: android android-listview android-widget widget

  

我正在尝试为锁屏创建一个app小部件,显示listview。   但我在appwidget中显示listview的问题。

2 个答案:

答案 0 :(得分:1)

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

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

答案 1 :(得分:0)

@saugat man ligal - 你可以从上面的给出链接找到Listview演示:

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

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

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工作......希望这会有所帮助