UPDATE: I discovered a problem, please run it if you have the same problem. http://code.google.com/p/android/issues/detail?id=28016
I have an appwidget with a gridview on it.
When I start adding elements to my widget, almost always I get the first elements shown twice (instead of displaying the first element and second element).
If I have the intention of updating the widget, the problem is fixed and never returns (it is assumed that I already have two elements in my gridview).
But this always happens when the first two elements are added.
Any ideas what this could be?
UPDATE: I just noticed that this always happens when a new item is added to the GridView. If I update the widget without adding a new element, then it works fine.
Another thing I saw is that the getViewAt method is always called twice for the first element (zero position). Maybe this is connected?
I watched the sample closely: http://developer.android.com/resources/samples/WeatherListWidget/src/com/example/android/weatherlistwidget/WeatherWidgetService.html
Here is my RemoteViewsService, I think this is an important part, but I'm not really sure. What else could affect this?
package com.manor.TestApp;
public class TestAppRemoteViewsService extends RemoteViewsService {
@Override
public RemoteViewsFactory onGetViewFactory (intent intent) {
return a new TestAppViewsFactory (this.getApplicationContext (), intent); }
}
class TestAppViewsFactory implements RemoteViewsService.RemoteViewsFactory {
Closed Context mContext;
// private int mAppWidgetId;
private TestDb mDb = null;
private int mCount = 0;
private String [] mData;
public TestAppViewsFactory (context context, intent intention) {mContext = context;
}
@Override public void onCreate () {
mDb = new TestDb(mContext); mDb.open(false);
}
@Override public void onDestroy () {
if (mDb != null) mDb.close();
}
@Override public int getCount () {
Log.d("TestApp", "getCount: " + Integer.toString(mCount)); return mCount;
}
@Override public RemoteViews getViewAt (int position) {
Log.d("TestApp", "pos: " + Integer.toString(position)); if (position >= mData.length) return null; Log.d("TestApp", "p: " + mData[position]); /*if (position > 0) { Log.d("TestApp", "here"); }*/ SharedPreferences sharedPreferences = >mContext.getSharedPreferences(TestAppPreferenceActivity.SHARED_PREFS_NAME, 0); RemoteViews rv = new RemoteViews(mContext.getPackageName(), >R.layout.widget_item); // --- set text and image to remoteviews --- return rv;
}
@Override public RemoteViews getLoadingView () {return null; }
@Override public void onDataSetChanged () {SharedPreferences sharedPreferences =
mContext.getSharedPreferences (TestAppPreferenceActivity.SHARED_PREFS_NAME, 0);
String[] strs = mDb.getData(); if (strs == null) { mCount = 0; return; }
}
@Override public int getViewTypeCount () {return 1; }
@Override public long getItemId (int pos) {return pos; }
@Override public boolean hasStableIds () {return false; }
}