I have a widget that uses a collection. I need to pass id from widget to action in my application when one of the widget rows is selected ListView.
For some reason, when I debug activity, an invalid identifier is sent. I narrowed it to a position. If I pass the position of my activity during debugging, this is the wrong position. Maybe this is just a misunderstanding of how the widget works with the collection, as this is my first.
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() {
return mIds.size();
}
public long getItemId(int position) {
return position;
}
public RemoteViews getLoadingView() {
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() {
return 1;
}
public boolean hasStableIds() {
return true;
}
public void onDataSetChanged() {
mIds = Utilities.GetIds();
}
public void onDestroy() {
}
}
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");
}
}
}
source
share