0
我正在從AppWidgetProvider類的onUpdate()部分開始,正在由服務更新的App Widget。問題在於,無論何時在小部件服務中使用遊標,視圖都不會被添加到小部件中。如果我簡單地替換字符串的sql查詢/結果,它就可以工作。Android應用程序小部件與SQlite查詢
不工作實施例(使用的ContentProvider/SQL):
public class TestWidgetService extends Service {
@Override
public void onStart(Intent intent, int startId) {
AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(getApplicationContext());
ComponentName thisWidget = new ComponentName(getApplicationContext(), TestWidget.class);
int[] allWidgetIds = appWidgetManager.getAppWidgetIds(thisWidget);
for (int widgetId : allWidgetIds) {
RemoteViews remoteViews = new RemoteViews(
getApplicationContext().getPackageName(),
R.layout.test_widget
);
// get items
Uri items = Uri.parse(TestWidgetContent.CONTENT_URI + "/news/1");
Cursor iCursor = getContentResolver().query(items, null, null, null, "ITM_TIMESTAMP DESC");
if (iCursor == null) {
continue;
}
else if (iCursor.moveToFirst()) {
while (iCursor.isAfterLast() == false) {
Log.w("got", iCursor.getString(iCursor.getColumnIndex("FED_TITLE")));
RemoteViews item = new RemoteViews(
getApplicationContext().getPackageName(),
R.layout.test_widget_item
);
item.setTextViewText(
R.id.test_widget_item_feed,
iCursor.getString(iCursor.getColumnIndex("FED_TITLE"))
);
item.setTextViewText(
R.id.test_widget_item_title,
iCursor.getString(iCursor.getColumnIndex("ITM_TITLE"))
);
remoteViews.addView(R.id.test_widget_items, item);
iCursor.moveToNext();
}
}
iCursor.close();
//
appWidgetManager.updateAppWidget(widgetId, remoteViews);
}
stopSelf();
super.onStart(intent, startId);
}
@Override
public IBinder onBind(Intent intent) {
return null;
}
}
該示例僅示出了佈局「test_widget」沒有增加的觀點,即使SQL返回結果和視圖添加。
工作示例(字符串只):
public class TestWidgetService extends Service {
@Override
public void onStart(Intent intent, int startId) {
AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(getApplicationContext());
ComponentName thisWidget = new ComponentName(getApplicationContext(), TestWidget.class);
int[] allWidgetIds = appWidgetManager.getAppWidgetIds(thisWidget);
for (int widgetId : allWidgetIds) {
RemoteViews remoteViews = new RemoteViews(
getApplicationContext().getPackageName(),
R.layout.test_widget
);
// get items
RemoteViews item = new RemoteViews(
getApplicationContext().getPackageName(),
R.layout.test_widget_item
);
item.setTextViewText(
R.id.test_widget_item_feed,
"feed"
);
item.setTextViewText(
R.id.test_widget_item_title,
"title"
);
remoteViews.addView(R.id.test_widget_items, item);
//
appWidgetManager.updateAppWidget(widgetId, remoteViews);
}
stopSelf();
super.onStart(intent, startId);
}
@Override
public IBinder onBind(Intent intent) {
return null;
}
}
本工作實施例顯示了佈局「test_widget」與
我花了上週試圖將的LinearLayout ID「test_widget_items」中插入的意見得到這個小部件使用SQL結果,任何人有任何輕微的想法可能是錯誤的?
我只是試着TestWidgetService.this.openOrCreateDatabase(),而不是實例化一個完整的DB助手類,但是我遇到了同樣的問題,即使結果正確讀取(並顯示在LogCat中),添加的視圖也不會顯示在窗口小部件中, – Andre