0

I'm trying to add multiple Views to a ViewFlipper, which is in an AppWidget (a Homescreen Widget). Here is my code:

for (Item item : list) {
    RemoteViews rv = new RemoteViews(getPackageName(),
            R.layout.widget_item);

    rv.setTextViewText(R.id.txtTitle, item.getTitle());

    Intent launchIntent = new Intent(this, ActItemViewer.class);
    launchIntent.putExtra("id", item.getId());

    PendingIntent pendingIntent = PendingIntent.getActivity(
            this, id, launchIntent, 0);
    rv.setOnClickPendingIntent(R.id.txtTitle, pendingIntent);

    views.addView(R.id.viewFlipper, rv);
}

This code works fine, all items are shown in my widget. But when I click on an item in the ViewFlipper, ActItemViewer is always opened with the same "id". So I think that only the first PendingIntent is set.

Is there any way to have different onClick-Events for every Child in the ViewFlipper? I have seen some Widgets, that have this functionality, for example the "Spiegel Online"-Widget (german), or the Market-Widget.

kroegerama
  • 769
  • 9
  • 27

2 Answers2

3

An easier solution not requiring random-ness is that you can embed all the extras into the data-string like this to make the intent unique:

intent.setData(Uri.parse(intent.toUri(Intent.URI_INTENT_SCHEME)));
arne.jans
  • 3,798
  • 2
  • 22
  • 27
-1

I found the solution here. This works perfektly.

I only had to use launchIntent.setAction("" + Math.random()) to make each Intent unique to the system. (The extras don't make an Intent unique, as Android doesn't check if they are different).

Community
  • 1
  • 1
kroegerama
  • 769
  • 9
  • 27