主屏幕小部件文本视图不更新

时间:2013-05-15 06:38:15

标签: android android-layout android-intent android-widget broadcastreceiver

这是我程序的代码 -

package com.nadeem.todos;

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.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.widget.RemoteViews;

public class todo_widget extends AppWidgetProvider {
private SQLiteDatabase db;
private TodoSQLiteHelper dbHelper;
String[] widget_title = { "", "", "", "", "", "" };
String[] widget_content = { "", "", "", "", "", "" };
public static String ACTION_WIDGET_REFRESH = "ActionReceiverRefresh";
public static String ACTION_WIDGET_OPEN = "ActionReceiverOpen";

@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager,
        int[] appWidgetIds) {

    Intent intent = new Intent(context, MainActivity.class);
    PendingIntent pendingIntent = PendingIntent.getActivity(context, 0,
            intent, PendingIntent.FLAG_UPDATE_CURRENT);

    RemoteViews views = new RemoteViews(context.getPackageName(),
            R.layout.widget_layout);
    views.setOnClickPendingIntent(R.id.contentw2, pendingIntent);
    views.setOnClickPendingIntent(R.id.contentw3, pendingIntent);
    Intent i1 = new Intent(context, todo_widget.class);
    i1.setAction(ACTION_WIDGET_REFRESH);

    PendingIntent pi = PendingIntent.getBroadcast(context, 0, i1,
            PendingIntent.FLAG_UPDATE_CURRENT);
    views.setOnClickPendingIntent(R.id.flip, pi);
    views.setOnClickPendingIntent(R.id.contentw1, pi);
    appWidgetManager.updateAppWidget(appWidgetIds, views);
}

@Override
public void onReceive(Context context, Intent intent) {
    AppWidgetManager appWidgetManager = AppWidgetManager
            .getInstance(context);

    int[] ids = AppWidgetManager.getInstance(context).getAppWidgetIds(
            new ComponentName(context, todo_widget.class));
    RemoteViews views = new RemoteViews(context.getPackageName(),
            R.layout.widget_layout);
    if (intent.getAction().equals(ACTION_WIDGET_REFRESH)) {
        views.setTextViewText(R.id.titlew1, widget_title[3]);
        views.setTextViewText(R.id.contentw1, widget_content[3]);
        appWidgetManager.updateAppWidget(ids, views);

    } else if (intent.getAction().equals(
            AppWidgetManager.ACTION_APPWIDGET_UPDATE)) {
        dbHelper = new TodoSQLiteHelper(context);
        db = dbHelper.getWritableDatabase();
        String[] tableColumns = new String[] { "_id", "title", "todo" };

        int j = 0;

        Cursor cursor = db.query("todos", tableColumns, null, null, null,
                null, null);
        cursor.moveToFirst();
        while (!cursor.isAfterLast()) {

            if (j < 6) {
                widget_title[j] = cursor.getString(1);
                widget_content[j] = cursor.getString(2);
            }
            j++;

            cursor.moveToNext();
        }
        db.close();
        final int N = ids.length;
        for (int i = 0; i < N; i++) {
            int appWidgetId = ids[i];
            views.setTextViewText(R.id.titlew1, widget_title[0]);
            views.setTextViewText(R.id.titlew2, widget_title[1]);
            views.setTextViewText(R.id.titlew3, widget_title[2]);
            views.setTextViewText(R.id.contentw1, widget_content[0]);
            views.setTextViewText(R.id.contentw2, widget_content[1]);
            views.setTextViewText(R.id.contentw3, widget_content[2]);
            appWidgetManager.updateAppWidget(appWidgetId, views);

        }
    }
    super.onReceive(context, intent);
}
    }

我的问题是当Action_widget_refresh为true时,onRecieve方法中的文本视图不会更新。我在文本视图上设置了待定意图,以便在单击它时文本视图(R.id.content1)的文本发生更改。但是当我点击文本视图时,文本视图会消失。我不知道为什么会这样。除此之外,代码工作正常。

2 个答案:

答案 0 :(得分:0)

我认为你应该这样做:

if (intent.getAction().equals(ACTION_WIDGET_REFRESH)) {
    views.setTextViewText(R.id.titlew1, widget_title[3]);
    views.setTextViewText(R.id.contentw1, widget_content[3]);
    final int N = ids.length;
    for (int i = 0; i < N; i++) {
        int appWidgetId = ids[i];
        appWidgetManager.updateAppWidget(appWidgetId, views);
    }
}

答案 1 :(得分:0)

每次调用appWidgetManager.updateAppWidget()时,传入的RemoveView都需要“包含窗口小部件的完整表示”。这意味着您需要每次都为TextView设置文本,而不仅仅是文本应该更改时。通过仅设置更改的文本,您将丢失要保持不变的文本。