I have a widget that is using a collection. What I need is to pass an id, from the widget, to an activity in my app when one of the rows of the widget ListView
is selected.
For some reason, when I debug activity, the wrong id is being sent. I've narrowed it down to something with the position. If I pass the position to my activity when I debug, it is the wrong position It may just be my misunderstanding of how a widget with a collection works, since it's my first one.
Widget Service
public class WidgetService extends RemoteViewsService {
@Override
public RemoteViewsFactory onGetViewFactory(Intent intent) {
return new StackRemoteViewsFactory(this.getApplicationContext(), intent);
}
}
class StackRemoteViewsFactory implements RemoteViewsService.RemoteViewsFactory {
private List<Integer> mIds = new ArrayList<Integer>();
private Context mContext;
private int mAppWidgetId;
public StackRemoteViewsFactory(Context context, Intent intent) {
mContext = context;
mAppWidgetId = intent.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, AppWidgetManager.INVALID_APPWIDGET_ID);
}
public void onCreate() {
}
public int getCount() {
// TODO Auto-generated method stub
return mIds.size();
}
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
public RemoteViews getLoadingView() {
// TODO Auto-generated method stub
return null;
}
public RemoteViews getViewAt(int position) {
RemoteViews rv = new RemoteViews(mContext.getPackageName(), R.layout.widget_item);
final Intent mi = new Intent(mContext, MyActivity.class);
final Bundle bun = new Bundle();
bun.putInt("id", mIds.get(position));
mi.putExtras(bun);
final PendingIntent piMain = PendingIntent.getActivity(mContext, mAppWidgetId, mi, PendingIntent.FLAG_UPDATE_CURRENT);
rv.setOnClickPendingIntent(R.id.widget_text, piMain);
rv.setTextViewText(R.id.widget_text, mIds.get(position));
return rv;
}
public int getViewTypeCount() {
// TODO Auto-generated method stub
return 1;
}
public boolean hasStableIds() {
// TODO Auto-generated method stub
return true;
}
public void onDataSetChanged() {
mIds = Utilities.GetIds();
}
public void onDestroy() {
// TODO Auto-generated method stub
}
}
My Activity
public class Activity extends SherlockFragmentActivity {
@Override
public void onCreate(final Bundle icicle)
{
super.onCreate(icicle);
setContentView(R.layout.browser_pager);
if (getIntent().getExtras() != null)
{
final Bundle extras = getIntent().getExtras();
int id = extras.getInt("id"); //this id is wrong
}
}
}