Dudes,
my Android widget displays only one record from my database. I'm sure that more than one item is retrieved from the database.
WidgetProvider's onUpdate method:
public void onUpdate(Context context, AppWidgetManager appWidgetManager,
int[] appWidgetIds) {
super.onUpdate(context, appWidgetManager, appWidgetIds);
if (items == null) {
items = new ItemsData(new DbHelper(context));
}
final int N = appWidgetIds.length;
Item[] iii = items.getItemsArray();
Log.d(TAG, "got " + iii.length + " items from db");
ComponentName thisWidget = new ComponentName(context, WidgetProvider.class);
for (int i = 0; i < N; i++) {
int appWidgetId = appWidgetIds[i];
Log.d(TAG, "updating widget " + appWidgetId);
RemoteViews rv = new RemoteViews(context.getPackageName(),
R.layout.widgetlayout);
for (Item item : iii) {
Log.d(TAG, "updating item " + item.id + ", " + item.title);
RemoteViews views = new RemoteViews(context.getPackageName(),
R.layout.row);
views.setTextViewText(R.id.textViewShortName, item.title);
views.setTextViewText(R.id.textViewDesc, item.description);
views.setTextViewText(R.id.textViewDue, new StringBuilder()
.append(item.dueDate).toString());
rv.addView(R.id.llWidget, views);
}
appWidgetManager.updateAppWidget(thisWidget, rv);
}
}
And my layout is just:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/llWidget"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#ff99ff">
<include
android:id="@+id/row"
layout="@layout/row" />
</LinearLayout>
with 'row' being three TextView items, arranged to my needs.
What happens is this: When I add items to the db and widget updates, only the last item can be seen. It is placed on the top.
I've searched android widget guide, stack overflow, some other websites, tutorial from a book, but even though some have presented similar examples, I can't get it to work... Google Android widget guide shows how to use collections, but it's only for 3.0+. Learning Android book describes a widget with elements added as rows, so I followed it - but couldn't make any progress... People asked about getting data from db from within widget (eg. Android: get Widget Data from database ) and, to my knowledge, had not came across such problem. And their solutions don't work for me.
Right now I don't even know how to look for an error... Strange thing is, in LogCat I can see that "updating item X" lines.
Any ideas?
UPDATE 1. Updated method (relevant portion) after Sergey Benner's info:
Item[] iii = items.getItemsArray();
for (int i = 0; i < N; i++) {
int appWidgetId = appWidgetIds[i];
RemoteViews rv = new RemoteViews(context.getPackageName(),
R.layout.widgetlayout);
for (Item item : iii) {
RemoteViews views = new RemoteViews(context.getPackageName(),
R.layout.row);
views.setTextViewText(R.id.textViewShortName, item.title);
views.setTextViewText(R.id.textViewDesc, item.description);
views.setTextViewText(R.id.textViewDue, new StringBuilder()
.append(item.dueDate).toString());
rv.addView(R.id.llWidget, views);
}
appWidgetManager.updateAppWidget(appWidgetId, rv);
}
UPDATE 2.
AndroidManifest file:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.whatnot.todoex"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="8" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:debuggable="true"
android:name=".ToDoExApp" >
<activity
android:label="@string/app_name"
android:name=".ListItemsActivity" >
<intent-filter >
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:label="@string/titleAdd" android:name=".AddItemActivity" />
<activity android:label="@string/titleRemindments" android:name=".RemindmentListActivity" />
<activity android:label="@string/titleAddRemindment" android:name=".AddRemindmentActivity" />
<activity android:label="@string/titleSetDate" android:name=".ChooseDateActivity" />
<receiver android:name=".WidgetProvider" >
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
</intent-filter>
<meta-data android:name="android.appwidget.provider"
android:resource="@xml/widgetprovider" />
</receiver>
</application>
</manifest>
widgetprovider.xml:
<?xml version="1.0" encoding="utf-8"?>
<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
android:minWidth="294dp"
android:minHeight="160dp"
android:updatePeriodMillis="3000"
android:initialLayout="@layout/widgetlayout" >
</appwidget-provider>
row.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/textViewShortName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium" />
<LinearLayout
android:id="@+id/linearLayout1"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView
android:id="@+id/textViewDesc"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_weight="0.80"
android:textAppearance="?android:attr/textAppearanceSmall" />
<TextView
android:id="@+id/textViewDue"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="right"
android:textAppearance="?android:attr/textAppearanceSmall" />
</LinearLayout>
</LinearLayout>
and updated onUpdate method at the beginning of this post. There is not much else in the WidgetProvider: an empty constructor, onEnabled with just super.onEnabled.. Also: I've put whole widget layout and row xmls.
UPDATE 3. Now, each time an item is changed i run this method:
private void updateWidget(Item item) {
Log.d(TAG, "updateWidget(). Sending broadcast to widget.");
Intent intent = new Intent(WidgetProvider.UPDATE);
intent.putExtra(WidgetProvider.UPDATE_ITEM, item.id);
sendBroadcast(intent);
}
My widget's onReceived looks like this now:
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(UPDATE)) {
Log.d(TAG, "onReceived detected new update");
AppWidgetManager appWidgetManager = AppWidgetManager
.getInstance(context); // 13
this.onUpdate(context, appWidgetManager, appWidgetManager
.getAppWidgetIds(new ComponentName(context,
WidgetProvider.class)),
intent.getExtras().getLong(WidgetProvider.UPDATE_ITEM));
} else {
super.onReceive(context, intent);
}
}
And a special, modified version of onUpdate is like this:
public void onUpdate(Context context, AppWidgetManager appWidgetManager,
int[] appWidgetIds, long itemId) {
super.onUpdate(context, appWidgetManager, appWidgetIds);
if (items == null) {
items = ((ToDoExApp) ((ContextWrapper) context).getBaseContext()).itemsData;
}
Item item = items.getItem2(itemId);
if (item == null) {
Log.d(TAG, "Item with id = " + itemId + " not found.");
return;
}
final int N = appWidgetIds.length;
for (int i = 0; i < N; i++) {
int appWidgetId = appWidgetIds[i];
Log.d(TAG, "updating widget " + appWidgetId);
RemoteViews views = new RemoteViews(context.getPackageName(),
R.layout.row);
views.setTextViewText(R.id.textViewShortName, item.title);
views.setTextViewText(R.id.textViewDesc, item.description);
views.setTextViewText(R.id.textViewDue,
new StringBuilder().append(item.dueDate).toString());
appWidgetManager.updateAppWidget(appWidgetId, views);
}
}
Now, whenever I change, add an item to the database, information about this is broadcasted to the widget. Then it is received and processed. However, only this item is displayed on the widget. Yamba application (from Learning Android) has something very similar - each time an update is found, it is being added to the widget. The only serious difference that I see is that Yamba uses content resolver and I use database...