IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Android Discussion :

Transfert de paramètre dans un widget avec setOnClickFillIntent


Sujet :

Android

  1. #1
    Membre du Club
    Inscrit en
    Décembre 2008
    Messages
    108
    Détails du profil
    Informations forums :
    Inscription : Décembre 2008
    Messages : 108
    Points : 40
    Points
    40
    Par défaut Transfert de paramètre dans un widget avec setOnClickFillIntent
    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 :

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

  2. #2
    Expert éminent

    Homme Profil pro
    Ingénieur systèmes et réseaux
    Inscrit en
    Février 2007
    Messages
    4 253
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Rhône (Rhône Alpes)

    Informations professionnelles :
    Activité : Ingénieur systèmes et réseaux
    Secteur : High Tech - Multimédia et Internet

    Informations forums :
    Inscription : Février 2007
    Messages : 4 253
    Points : 7 618
    Points
    7 618
    Billets dans le blog
    3
    Par défaut
    Pourquoi ne pas directement dire à la DirectView quel Intent final doit être utilisé ?
    En gros, je ne comprends pas trop l'interêt de passer par un Intent intermédiaire.
    N'oubliez pas de cliquer sur mais aussi sur si un commentaire vous a été utile !
    Et surtout

Discussions similaires

  1. Réponses: 0
    Dernier message: 18/02/2014, 16h37
  2. Nommer les paramètres dans le wsdl avec EJB3
    Par djaih dans le forum Services Web
    Réponses: 1
    Dernier message: 01/03/2010, 12h57
  3. placer des paramètres dans une URL avec querystring
    Par DAGDD dans le forum SharePoint
    Réponses: 19
    Dernier message: 10/07/2009, 08h35
  4. Pb Update dans une PS avec nom de colonne comme paramètre
    Par blowlagoon dans le forum MS SQL Server
    Réponses: 6
    Dernier message: 07/06/2006, 10h20
  5. [Débutant] Pb avec les paramètres dans lien dynamique
    Par hackwell69 dans le forum Struts 1
    Réponses: 2
    Dernier message: 21/02/2005, 11h33

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo