Bonjour,
Je développe actuellement un widget qui comporte une listview. Dans chaque item de la liste, ontrouve des textview avec des valeurs :
- titre
- description
- url
Je souhaiterai envoyer de ma classe StackWidgetService ces informations vers la classe StackWidgetProvider.
Le but est de pouvoir recevoir les informations lors du clic sur un item dans la classe StackWidgetProvider afin de lancer une nouvelle activity qui afiche les informations de l'item sélectionné.
Aujourd'hui, seul l'Id de l'item sélectionné est affiché lors du clic sur un item de la liste.
Voici le code des méthodes onUpdate() et onReceive de la classe StackWigdetProvider :
Et celui de la méthode getViewAt() de la classe StackWidgetService :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59 @Override public void onReceive(Context context, Intent intent) { AppWidgetManager mgr = AppWidgetManager.getInstance(context); if (intent.getAction().equals(OPEN_DETAILS)) { //We receive the Id of item which has been clicked int appWidgetId = intent.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, AppWidgetManager.INVALID_APPWIDGET_ID); int viewIndex = intent.getIntExtra(EXTRA_ITEM, 0); Toast.makeText(context, "Touched view " + viewIndex, Toast.LENGTH_SHORT).show(); // Now, we want receive all information of the item : titre, url, description, ... // ???? // ???? //And after, we want to start an activity with all information of the item which has been clicked and print them in the activity Intent DetailDeal = new Intent(Intent.ACTION_VIEW); DetailDeal.setClassName("com.example.android.stackwidget", "com.example.android.stackwidget.AddDealActivity"); DetailDeal.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); context.startActivity(DetailDeal); } super.onReceive(context, intent); } @Override public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) { // update each of the widgets with the remote adapter for (int i = 0; i < appWidgetIds.length; ++i) { // Here we setup the intent which points to the StackViewService which will // provide the views for this collection. Intent intent = new Intent(context, StackWidgetService.class); intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetIds[i]); // When intents are compared, the extras are ignored, so we need to embed the extras // into the data so that the extras will not be ignored. intent.setData(Uri.parse(intent.toUri(Intent.URI_INTENT_SCHEME))); RemoteViews rv = new RemoteViews(context.getPackageName(), R.layout.widget_layout_white_theme); rv.setRemoteAdapter(appWidgetIds[i], R.id.stack_view, intent); // The empty view is displayed when the collection has no items. It should be a sibling // of the collection view. rv.setEmptyView(R.id.stack_view, R.id.empty_view); // Here we setup the a pending intent template. Individuals items of a collection // cannot setup their own pending intents, instead, the collection as a whole can // setup a pending intent template, and the individual items can set a fillInIntent // to create unique before on an item to item basis. Intent toastIntent = new Intent(context, StackWidgetProvider.class); toastIntent.setAction(StackWidgetProvider.OPEN_DETAILS); toastIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetIds[i]); intent.setData(Uri.parse(intent.toUri(Intent.URI_INTENT_SCHEME))); PendingIntent toastPendingIntent = PendingIntent.getBroadcast(context, 0, toastIntent, PendingIntent.FLAG_UPDATE_CURRENT); rv.setPendingIntentTemplate(R.id.stack_view, toastPendingIntent); appWidgetManager.updateAppWidget(appWidgetIds[i], rv); } super.onUpdate(context, appWidgetManager, appWidgetIds); }
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55 public RemoteViews getViewAt(int position) { // position will always range from 0 to getCount() - 1. // We construct a remote views item based on our widget item xml file, and set the // text based on the position. WidgetItem item = mWidgetItems.get(position); // We construct a remote views item based on our widget item xml file, and set the // text based on the position. RemoteViews rv = new RemoteViews(mContext.getPackageName(), R.layout.widget_item_white_theme); rv.setTextViewText(R.id.titre, item.titre); rv.setTextViewText(R.id.description, item.description); rv.setTextViewText(R.id.url, "@"+item.site.toString()); rv.setTextViewText(R.id.ancienprix, item.ancienprix); rv.setTextViewText(R.id.nouveauprix, item.nouveauPrix); //On barre l'ancien prix rv.setInt(R.id.ancienprix, "setPaintFlags", Paint.STRIKE_THRU_TEXT_FLAG | Paint.ANTI_ALIAS_FLAG); //On positionne la bande à gauche de l'item. Verte si le deal est encore actif, rouge sinon rv.setInt(R.id.dealtermine, "setBackgroundColor", item.dealTermine ? android.graphics.Color.rgb(204, 0, 0) : android.graphics.Color.rgb(102, 153, 0)); //On positionne l'image de l'item suivant le degré du deal if (item.qualite == "chaud") { //C'est un très bon deal rv.setImageViewResource(R.id.img, R.drawable.ic_action_lab_black); } else if (item.qualite == "moyen") { //c'est un deal moyen rv.setImageViewResource(R.id.img, R.drawable.ic_action_lab_red); } else if (item.qualite == "froid") { //c'est un deal bof rv.setImageViewResource(R.id.img, R.drawable.ic_action_lab_green); } // Next, we set a fill-intent which will be used to fill-in the pending intent template // which is set on the collection view in StackWidgetProvider. Bundle extras = new Bundle(); extras.putInt(StackWidgetProvider.EXTRA_ITEM, position); Intent fillInIntent = new Intent(); fillInIntent.putExtras(extras); rv.setOnClickFillInIntent(R.id.group_layout, fillInIntent); // You can do heaving lifting in here, synchronously. For example, if you need to // process an image, fetch something from the network, etc., it is ok to do it here, // synchronously. A loading view will show up in lieu of the actual contents in the // interim. try { System.out.println("Loading view " + position); Thread.sleep(500); } catch (InterruptedException e) { e.printStackTrace(); } // Return the remote views object. return rv; }
Partager