这是我的程序的代码 -
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 为真时,onRecieve 方法中的文本视图不会更新。我在文本视图上设置了待处理的意图,以便在单击它时文本视图(R.id.content1)的文本会发生变化。但是当我点击文本视图时,文本视图消失了。我不知道为什么会这样。除此之外,代码工作正常。