androidlistviewwidget

Handling on item click in Widget ListView [Android]


I've been searching this for weeks now but I can't find an answer. I'm trying to create a ListView inside a widget. I managed to populate the ListView. My problem is when I click an item inside the ListView I wanted to open an Activity and passing the Id of that item to the Activity I called. But it seems I can't get the passed Id correctly it is always null when going to the Activity I called.

Here is my code

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");
....
}

I'm populating my ListView using a Web Service that is inside my ListProvider and calling it on onDataSetChanged. When debugging it, while the List is being populated it also puts all the data in EXTRA_ID, maybe it was the reason why when the Activity is opened is gets confuse and returns null instead, I'm not sure why how to fix this. Please guide me how to solve this. Thank you in advance for the help.


Solution

  • I found a great 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 also help me to start an Activity inside the onReceive inside the AppWidgetProvider. I'm just encountering app crashes when removing the widget on home screen. Will update this answer when I found out how to solve it.

    Edit

    I got to solve the crashing part I forgot to add the onDelete and then add notifyAppWidgetViewDataChanged as well for the ListView its working fine now.