Laser
Laser

Reputation: 6960

ListView Item ID homescreen widget

I have homescreen widget with ListView

How can I obtain number of current Item which was clicked: I've tried to do it by following way: Insite custom class which extends AppWidgetProvider

 public void onUpdate(Context ctxt, AppWidgetManager mgr,
                         int[] appWidgetIds) {
        for (int i=0; i<appWidgetIds.length; i++) {
            Intent svcIntent=new Intent(ctxt, WidgetService.class);

            svcIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetIds[i]);
            svcIntent.setData(Uri.parse(svcIntent.toUri(Intent.URI_INTENT_SCHEME)));

            RemoteViews widget=new RemoteViews(ctxt.getPackageName(),
                    R.layout.widget);

            widget.setRemoteAdapter(appWidgetIds[i], R.id.contacts,
                    svcIntent);

            Intent clickIntent=new Intent(ctxt, AppWidget.class);


            clickIntent.setAction(ACTION_WIDGET_REFRESH);
            clickIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, appWidgetIds[i]);

            PendingIntent pi=PendingIntent.getBroadcast(ctxt, 0 , clickIntent,  PendingIntent.FLAG_UPDATE_CURRENT);
            widget.setPendingIntentTemplate(R.id.contacts, pi);
            mgr.updateAppWidget(appWidgetIds[i], widget);
        }
        super.onUpdate(ctxt, mgr, appWidgetIds);
    } 

@Override
        public void onReceive(Context context, Intent intent) {
        Log.i("gotcha","receive");


        if (intent.getAction().equals(ACTION_WIDGET_REFRESH)) {

            Intent callIntent = new Intent(Intent.ACTION_CALL);
            Bundle extras = intent.getExtras();
            int extrass= -1;
            if(extras!=null) {
                 extrass = extras.getInt(AppWidgetManager.EXTRA_APPWIDGET_IDS);
            }

            Log.i("receive", Integer.toString(extrass) ) ; // Here is I've expected to see Item ID

        }

But It's not helped. I've just see different numbers (not Items ID)

How can I obtain them?

Upvotes: 0

Views: 676

Answers (2)

ParikshitSinghTomar
ParikshitSinghTomar

Reputation: 417

Read it once I think you get what should we do

When using collections (eg. ListView, StackView etc.) in widgets, it is very costly to set PendingIntents on the individual items, and is hence not permitted. Instead this method should be used to set a single PendingIntent template on the collection, and individual items can differentiate their on-click behavior using setOnClickFillInIntent(int, Intent).

Upvotes: 0

Isaiah
Isaiah

Reputation: 442

the problem is

  PendingIntent pi=PendingIntent.getBroadcast(ctxt, 0 , clickIntent,  PendingIntent.FLAG_UPDATE_CURRENT);

Because if you always give 0 as requestCode, and the flag is FLAG_UPDATE_CURRENT, all the list items refer to the same PendingIntent which is update by the lasted.

So just give different requestCode to the PendingIntent will solve this problem, please use

PendingIntent pi=PendingIntent.getBroadcast(ctxt,  appWidgetIds[i] , clickIntent,  PendingIntent.FLAG_UPDATE_CURRENT)`

instead. And also set the OnClickFillIntent to the RemoteView in the adapter.getViewAt()

    RemoteViews rv = new RemoteViews(pkg, R.layout.item);
    Intent i = new Intent().putExtra("position", position);
    rv.setOnClickFillInIntent(R.id.item, i);

Then you can retrieve the appwidget id , as well as the list item position.

Upvotes: 3

Related Questions