我现在已经搜索了几个星期,但我无法找到答案。我试图在ListView
内创建widget
。我设法填充了ListView
。我的问题是当我点击ListView
中的某个项目时,我想打开Activity
并将该项目的Id
传递给我调用的Activity
。但是,当我转到Id
时,我似乎无法正确地通过null
传递Activity
。
这是我的代码
WidgetProvier
public class WidgetProvider extends AppWidgetProvider {
public void onUpdate(Context context, AppWidgetManager appWidgetManager,
int[] appWidgetIds) {
....
for (int i = 0; i < N; ++i) {
RemoteViews remoteViews = updateWidgetListView(context,
appWidgetIds[i]);
appWidgetManager.notifyAppWidgetViewDataChanged(appWidgetIds[i], R.id.listViewWidget);
appWidgetManager.updateAppWidget(appWidgetIds[i], remoteViews);
}
super.onUpdate(context, appWidgetManager, appWidgetIds);
}
private RemoteViews updateWidgetListView(Context context, int appWidgetId) {
//For ListView
Intent svcIntent = new Intent(context, WidgetService.class);
svcIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
svcIntent.setData(Uri.parse(svcIntent.toUri(Intent.URI_INTENT_SCHEME)));
remoteViews.setRemoteAdapter(appWidgetId, R.id.listViewWidget, svcIntent);
//When item is clicked on ListView
Intent startActivityIntent = new Intent(context, ActivityOne.class);
PendingIntent startActivityPendingIntent = PendingIntent.getActivity(context, 0, startActivityIntent, PendingIntent.FLAG_UPDATE_CURRENT);
remoteViews.setPendingIntentTemplate(R.id.listViewWidget, startActivityPendingIntent);
return remoteViews;
}
WidgetService
public class WidgetService extends RemoteViewsService {
@Override
public RemoteViewsFactory onGetViewFactory(Intent intent) {
return (new ListProvider(this.getApplicationContext(), intent));
}
}
ListProvider
@Override
public RemoteViews getViewAt(int position) {
final RemoteViews widgetRow = new RemoteViews(
context.getPackageName(), R.layout.list_row);
ListItem listItem = listItemList.get(position);
widgetRow.setTextViewText(R.id.txtID, listItem.id);
//Pass the ID to ActivityOne
Intent fillInIntent = new Intent();
fillInIntent.putExtra("EXTRA_ID", listItem.id);
widgetRow.setOnClickFillInIntent(R.id.llRow, fillInIntent);
return widgetRow;
}
ActivityOne
public class ActivityOne extends Activity {
String mID;
@Override
protected void onCreate(Bundle savedState) {
....
mID = intent.getStringExtra("EXTRA_ID");
....
}
我使用ListView
内的Web Service
填充ListProvider
并在onDataSetChanged
上调用List
。在调试它时,在填充EXTRA_ID
的同时,它还会将所有数据放在Activity
中,也许这就是为什么在打开textField.clearButtonMode = UITextFieldViewMode.whileEditing;
时会混淆并返回null的原因,我不知道为什么要解决这个问题。请指导我如何解决这个问题。提前感谢您的帮助。