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

Composants graphiques Android Discussion :

Modifier les attributs d'une custom view dynamiquement


Sujet :

Composants graphiques Android

Vue hybride

Message précédent Message précédent   Message suivant Message suivant
  1. #1
    Membre chevronné
    Profil pro
    Inscrit en
    Juillet 2012
    Messages
    476
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Juillet 2012
    Messages : 476
    Par défaut Modifier les attributs d'une custom view dynamiquement
    Bonjour à tous,

    Dans mon projet, j'ai créé une custom view pour représenter un élément que je peux appliquer un peu partout. Voici son code
    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
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    public class Plan extends LinearLayout {
     
        @SuppressWarnings("WeakerAccess")
        String price = "";
        @SuppressWarnings("WeakerAccess")
        String period = "";
        @SuppressWarnings("WeakerAccess")
        String title = "";
        @SuppressWarnings("WeakerAccess")
        String titleInfo = "";
     
        public Plan(Context context) {
            super(context);
            LayoutInflater.from(context).inflate(R.layout.plan, this);
     
        }
     
        @SuppressLint("NewApi")
        public Plan(Context context, AttributeSet attrs) {
            super(context, attrs);
            TypedArray a = context.getTheme().obtainStyledAttributes(attrs,
                    R.styleable.Plan, 0, 0);
     
            try {
                // get the text and drawables specified using the names in attrs.xml
                price = a.getString(R.styleable.Plan_price);
                period = a.getString(R.styleable.Plan_period);
                title = a.getString(R.styleable.Plan_plan_title);
                titleInfo = a.getString(R.styleable.Plan_title_info);
     
            } finally {
                a.recycle();
            }
     
            LayoutInflater.from(context).inflate(R.layout.plan, this);
            TextView priceView = (TextView) this.findViewById(R.id.price);
            priceView.setText(price);
            TextView periodView = (TextView) this.findViewById(R.id.period);
            periodView.setText(period);
            TextView titleView = (TextView) this.findViewById(R.id.title);
            titleView.setText(title);
            TextView infoView = (TextView) this.findViewById(R.id.title_info);
            infoView.setText(titleInfo);
     
            this.setClickable(true);
            this.setFocusable(true);
            this.setFocusableInTouchMode(false);
        }
     
        public String getPrice() {
            return price;
        }
     
        public void setPrice(String price) {
            this.price = price;
        }
     
        public String getPeriod() {
            return period;
        }
     
        public void setPeriod(String period) {
            this.period = period;
        }
     
        public void setTitle(String title) {
            this.title = title;
        }
     
        public void setTitleInfo(String titleInfo) {
            this.titleInfo = titleInfo;
        }
    }
    et un example de layout dans lequel je le mets
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
     <com.....Plan
                android:id="@+id/plan_hour"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:onClick="selectPrice"
                custom:period="@string/plan_period_hour"
                custom:plan_title="@string/plan_title_hour"
                custom:price="7 €"
                custom:title_info="@string/plan_titleinfo_hour" />
    Pour le projet, j'aurais besoin de modifier et/ou créer cette custom view dynamiquement. Mais je n'y arrive pas. Dans mon activité, j'ai beau mettre
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
          Plan planHour = new Plan(this);
                    planHour.setPrice(rental.getPriceHour());
                    planHour.setPeriod(getString(R.string.plan_period_hour));
                    planHour.setTitle(getString(R.string.plan_title_hour));
                    planHour.setTitle(getString(R.string.plan_titleinfo_hour));
                    layout.addView(planHour);
    ou via du xml
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    Plan planHour = (Plan) findViewById(R.id.plan_hour);
                    planHour.setVisibility(View.VISIBLE);
                    planHour.setPrice(rental.getPriceHour());
                    planHour.setPeriod(getString(R.string.plan_period_hour));
                    planHour.setTitle(getString(R.string.plan_title_hour));
                    planHour.setTitle(getString(R.string.plan_titleinfo_hour));
                    planHour.invalidate();
    rien n'y fait. La custom view est bien dessiné (je vois le background), mais les données ne sont pas remplies.
    Comment faire ?
    Merci

  2. #2
    Membre chevronné
    Profil pro
    Inscrit en
    Juillet 2012
    Messages
    476
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Juillet 2012
    Messages : 476
    Par défaut
    Personnepour m'aier?

  3. #3
    Expert confirmé

    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
    Billets dans le blog
    3
    Par défaut
    Je ne vois pas quand tu fais les "view.setText" ... après les "setTitle" et autres...

  4. #4
    Membre chevronné
    Profil pro
    Inscrit en
    Juillet 2012
    Messages
    476
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Juillet 2012
    Messages : 476
    Par défaut
    Comprends pas.
    Quand je fais setTitle ou setPeriod, je donne bien aux variable title et period de Plan les valeurs souhaitées, getString(R.string.plan_title_hour) par exemple ? J'ai essayé
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    planHour.setTitle(getString(R.string.plan_title_hour)).setText("fff");
    mais j'ai une erreur. Pourquoi la valeur de la variable ne s'affiche pas ?

Discussions similaires

  1. Réponses: 0
    Dernier message: 18/03/2016, 14h08
  2. [WD14] Modifier les attributs du titre d'une colonne de table
    Par mogwai162 dans le forum WinDev
    Réponses: 2
    Dernier message: 12/03/2010, 10h12
  3. Réponses: 1
    Dernier message: 14/08/2009, 08h50
  4. Modifier en javascript les attributs d'une classe CSS
    Par troumad dans le forum Général JavaScript
    Réponses: 2
    Dernier message: 05/09/2007, 10h45
  5. [VBA-E]modifier les attributs d'un commentaire dans une cellule
    Par Olivier vb dans le forum Macros et VBA Excel
    Réponses: 2
    Dernier message: 15/03/2004, 10h26

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