我有一个使用集合的小部件。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.
出于某种原因,当我调试活动时,发送了错误的 ID。我已将其范围缩小到与该职位有关的内容。如果我在调试时将位置传递给我的活动,那是错误的位置这可能只是我对带有集合的小部件如何工作的误解,因为它是我的第一个。
小部件服务
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
}
}
我的活动
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
}
}
}