Handling on item click in Widget ListView [Android]
Asked Answered
F

2

6

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.

Floria answered 21/9, 2016 at 8:40 Comment(5)
You haven't added code from your ActivityOne where you are getting null instead of an Id.Peppel
@Peppel I've added my ActivityOneFloria
Try and add EXTRA_ID in Intent from updateWidgetListView().Peppel
@Peppel Like this startActivityIntent.putExtra("EXTRA_ID2", intent.getStringExtra("EXTRA_ID"); ); ? I'm wondering on how can I get the Id from the ListProviderFloria
Why don't you first add a hard coded value to start with and see if you get that in your Activity and not null. Worry about details later.Peppel
F
1

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.

Floria answered 22/9, 2016 at 2:55 Comment(0)
D
0

For newer api's when you need to add flag mutable or immutable, use PendingIntent.FLAG_MUTABLE otherwise the extras will be null.

Dixon answered 3/8 at 11:54 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.