处理 Widget ListView 中的项目点击 [Android]
Handling on item click in Widget ListView [Android]
我已经搜索了数周,但找不到答案。我正在尝试在 widget
中创建一个 ListView
。我设法填充 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;
}
小部件服务
public class WidgetService extends RemoteViewsService {
@Override
public RemoteViewsFactory onGetViewFactory(Intent intent) {
return (new ListProvider(this.getApplicationContext(), intent));
}
}
列表提供者
@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");
....
}
我正在使用我的 ListProvider
中的 Web Service
填充我的 ListView
,并在 onDataSetChanged
上调用它。调试它时,在填充 List
时它还会将所有数据放入 EXTRA_ID
,这可能是打开 Activity
时变得混乱和 [=46= 的原因] null 相反,我不确定为什么要解决这个问题。请指导我如何解决这个问题。预先感谢您的帮助。
我发现一个很棒的example I tried following it and changes how the data is set and then on the onReceive
in my AppWidgetProvider
I call there my ActivityOne
and passed the data from what I get in the Bundle
. This link也帮我启动了一个Activity
里面的onReceive
里面的AppWidgetProvider
。我只是在删除主屏幕上的小部件时遇到应用程序崩溃。当我发现如何解决它时会更新这个答案。
编辑
我必须解决我忘记添加 onDelete
然后添加 notifyAppWidgetViewDataChanged
以及 ListView
现在工作正常的崩溃部分。
我已经搜索了数周,但找不到答案。我正在尝试在 widget
中创建一个 ListView
。我设法填充 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;
}
小部件服务
public class WidgetService extends RemoteViewsService {
@Override
public RemoteViewsFactory onGetViewFactory(Intent intent) {
return (new ListProvider(this.getApplicationContext(), intent));
}
}
列表提供者
@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");
....
}
我正在使用我的 ListProvider
中的 Web Service
填充我的 ListView
,并在 onDataSetChanged
上调用它。调试它时,在填充 List
时它还会将所有数据放入 EXTRA_ID
,这可能是打开 Activity
时变得混乱和 [=46= 的原因] null 相反,我不确定为什么要解决这个问题。请指导我如何解决这个问题。预先感谢您的帮助。
我发现一个很棒的example I tried following it and changes how the data is set and then on the onReceive
in my AppWidgetProvider
I call there my ActivityOne
and passed the data from what I get in the Bundle
. This link也帮我启动了一个Activity
里面的onReceive
里面的AppWidgetProvider
。我只是在删除主屏幕上的小部件时遇到应用程序崩溃。当我发现如何解决它时会更新这个答案。
编辑
我必须解决我忘记添加 onDelete
然后添加 notifyAppWidgetViewDataChanged
以及 ListView
现在工作正常的崩溃部分。