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 :

Parsing JSON disparition des ImageView lors du scrolling


Sujet :

Composants graphiques Android

  1. #1
    Membre régulier
    Profil pro
    Inscrit en
    Mars 2007
    Messages
    541
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Mars 2007
    Messages : 541
    Points : 124
    Points
    124
    Par défaut Parsing JSON disparition des ImageView lors du scrolling
    Bonjour,

    Je parse un fichier .json et j'affiche des images qui sont dans le dossier drawable-hdpi.

    Le problème est que lorsque j'effectue du scrolling, des images supplémentaires apparaissent dans chaque ligne de la liste voir disparaissent.

    voici mon adapter:


    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
     
    public class MonAdapter extends ArrayAdapter<MaClasse>{
     
    	public MonAdapter(Context context) {
    		super(context, R.layout.listrow);
     
    	}
     
    	@Override
    	public View getView(int position, View convertView, ViewGroup parent) {
    		View v = convertView;
    		if (v == null) {
    	LayoutInflater vi = (LayoutInflater) getContext().getSystemService(
    					Context.LAYOUT_INFLATER_SERVICE);
    			v = vi.inflate(R.layout.listrow, null);
    		}
     
    		MaClasse o = getItem(position);
    		if (o != null) {
    			TextView tt = (TextView) v.findViewById(R.id.libelle);
    			if (tt != null) {
    				tt.setText(o.getArret().getLibelle());
    			}
     
     
    			LinearLayout layout = (LinearLayout) v
    			.findViewById(R.id.mylot2);		
    			LinearLayout layout3 = (LinearLayout) v
    			.findViewById(R.id.mylot3);	
    			int count=0;
     
    			image = new ImageView(getContext());
    int resid = getContext().getResources().getIdentifier("ligne_"+o.getLigne().numLigne.toLowerCase(), "drawable","fr.monpackage");
    					image.setImageResource(resid);
     
    	layout.addView(image,new LinearLayout.LayoutParams(50, 50));
     
    		}
    		return v;
    	}
     
    }
    D'où cela pourrait venir?

    Merci.

  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
    Forcément... tu rajotues des ImageView à la main...
    Comme les views sont réutilisées (pour éviter de gaspiller du temps de traitement et de la mémoire), tu peux recevoir une view (convertView) dans laquelle tu as déjà créé l'imageview => et paf, ca en fait deux (ou trois, quatre, .... ).

    Il vaut mieux avoir l'image-view dans le layout initial et de la cacher (View.GONE) ou de la montre (View.VISIBLE) au besoin...
    Voir changer la ressource associéee par setDrawableResource (ou une fonction du genre).
    N'oubliez pas de cliquer sur mais aussi sur si un commentaire vous a été utile !
    Et surtout

  3. #3
    Membre régulier
    Profil pro
    Inscrit en
    Mars 2007
    Messages
    541
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Mars 2007
    Messages : 541
    Points : 124
    Points
    124
    Par défaut
    J'ai effectuer l'autre manipulation e déclarant les imageviews dans le layout même constat

  4. #4
    Membre extrêmement actif
    Avatar de Ryu2000
    Homme Profil pro
    Étudiant
    Inscrit en
    Décembre 2008
    Messages
    9 595
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 36
    Localisation : France, Hérault (Languedoc Roussillon)

    Informations professionnelles :
    Activité : Étudiant

    Informations forums :
    Inscription : Décembre 2008
    Messages : 9 595
    Points : 18 500
    Points
    18 500
    Par défaut
    Tant que t’utilise addView ça te le fera.

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    LinearLayout layoutItem;
    if (convertView == null) {
    	layoutItem = (LinearLayout) mInflater.inflate(R.layout.layout, parent, false);
    } else {
    	layoutItem = (LinearLayout) convertView;
    }
    ImageView iv = layoutItem.findViewById(R.id.imageView);
    Si tu fais comme ça, ça devrait fonctionner.
    Keith Flint 1969 - 2019

  5. #5
    Membre régulier
    Profil pro
    Inscrit en
    Mars 2007
    Messages
    541
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Mars 2007
    Messages : 541
    Points : 124
    Points
    124
    Par défaut
    je n'utilise pas d'addview, je récupère l'imageview via les ressources Id

  6. #6
    Membre extrêmement actif
    Avatar de Ryu2000
    Homme Profil pro
    Étudiant
    Inscrit en
    Décembre 2008
    Messages
    9 595
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 36
    Localisation : France, Hérault (Languedoc Roussillon)

    Informations professionnelles :
    Activité : Étudiant

    Informations forums :
    Inscription : Décembre 2008
    Messages : 9 595
    Points : 18 500
    Points
    18 500
    Par défaut
    Dans ton code il y a ça :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    layout.addView(image,new LinearLayout.LayoutParams(50, 50));
    Ah moi que tu ais changé ton code depuis.

    En attendant normalement dans ton R.layout.listrow il devrait y avoir une ImageView.
    Tu récupère comme ça :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    ImageView iv = layoutItem.findViewById(R.id.imageView);
    Tu fais un test, si tu n'a pas d'image à afficher :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    iv.setVisibility(View.GONE);
    Sinon :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    iv.setVisibility(View.VISIBLE);
    Et là tu affectes une images.
    Keith Flint 1969 - 2019

  7. #7
    Membre régulier
    Profil pro
    Inscrit en
    Mars 2007
    Messages
    541
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Mars 2007
    Messages : 541
    Points : 124
    Points
    124
    Par défaut
    voici ma classe:


    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
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
     
    public class ListeArretAdapter extends ArrayAdapter<ListeArret> {
     
     
     
    	// private String mPage = "";
     
    	public ListeArretAdapter(Context context) {
     
    		super(context, R.layout.listrow);
    	}
     
    	@Override
     
    	public View getView(int position, View convertView, ViewGroup parent) {
    		View v = convertView;
    		if (v == null) {
    			LayoutInflater vi = (LayoutInflater) getContext().getSystemService(
    					Context.LAYOUT_INFLATER_SERVICE);
    			v = vi.inflate(R.layout.listrow, null);
    		}
    		ListeArret o = getItem(position);
    		if (o != null) {
    			TextView tt = (TextView) v.findViewById(R.id.station);
    			if (tt != null) {
    				tt.setText("Station:" + o.getLibelle());
    			}
    			TextView bb = (TextView) v.findViewById(R.id.distance);
    			if (bb != null) {
    				Log.v("", "getDistance:" + o.getDistance());
    				bb.setText("Distance: " + o.getDistance());
    			}
    			ImageView image1 = (ImageView) v.findViewById(R.id.img1);
    			ImageView image2 = (ImageView) v.findViewById(R.id.img2);
    			ImageView image3 = (ImageView) v.findViewById(R.id.img3);
    			ImageView image4 = (ImageView) v.findViewById(R.id.img4);
    			ImageView image5 = (ImageView) v.findViewById(R.id.img5);
    			ImageView image6 = (ImageView) v.findViewById(R.id.img6);
    			ImageView image7 = (ImageView) v.findViewById(R.id.img7);
    			ImageView image8 = (ImageView) v.findViewById(R.id.img8);
    			ImageView image9 = (ImageView) v.findViewById(R.id.img9);
    			ImageView image10 = (ImageView) v.findViewById(R.id.img10);
    			ImageView image11 = (ImageView) v.findViewById(R.id.img11);
    			ImageView image12 = (ImageView) v.findViewById(R.id.img12);
     
    			int count = 0;
    			for (int i = 0; i < o.getLignes().size(); i++) {
    				Log.v("Adpater", i + "ListeArretAdapter:"
    						+ o.getLignes().get(i).getNumLigne());
    				int resid = getContext().getResources().getIdentifier(
    						"img_"+ o.getLignes().get(i).getNumLigne()
    										.toLowerCase(), "drawable",
    						"fr.monpackage");
    			if (count >= 3 && count < 6) {
    			if (count == 3) {
    					image4.setImageResource(resid);
    				} else if (count == 4) {
    						image5.setImageResource(resid);
    					} else if (count == 5) {
    						image6.setImageResource(resid);
    					}
    					count++;
    				} else if (count >= 6 && count < 9) {
    					if (count == 6) {
    					image7.setImageResource(resid);
    				} else if (count == 7) {
    						image8.setImageResource(resid);
    					} else if (count == 8) {
    						image9.setImageResource(resid);
    					}
     
    			count++;
    			} else if (count >= 9 && count < 12) {
    				if (count == 9) {
    						image10.setImageResource(resid);
    					} else if (count == 10) {
    						image11.setImageResource(resid);
    					} else if (count == 11) {
    						image12.setImageResource(resid);
    					}
    					count++;
    				} else {
    			if (count == 0) {
    						image1.setImageResource(resid);
    					} else if (count == 1) {
    						image2.setImageResource(resid);
    					} else if (count == 2) {
    						image3.setImageResource(resid);
    					}
    					count++;
    				}
    			}
    		}
    	return v;
     
    	}
     
     
     
    }

    mon xml:


    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
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222
    223
    224
    225
    226
    227
    228
    229
    230
    231
    232
    233
    234
    235
    236
    237
    238
    239
    240
    241
    242
    243
    244
    245
    246
    247
    248
    249
    250
    251
    252
    253
    254
    255
    256
    257
    258
    259
    260
    261
    262
    263
    264
    265
    266
    267
    268
    269
    270
    271
    272
    273
    274
    275
    276
    277
    278
    279
     
    <?xml version="1.0" encoding="utf-8"?>
     
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
     
        android:id="@+id/monlayout"
     
        android:layout_width="fill_parent"
     
        android:layout_height="wrap_content"
     
        android:background="@drawable/menu_boutton_list"
     
        android:orientation="horizontal"
     
        android:padding="3dp" >
     
     
     
        <LinearLayout
     
            android:id="@+id/mylot"
     
            android:layout_width="wrap_content"
     
            android:layout_height="fill_parent"
     
            android:layout_weight="1"
     
            android:orientation="vertical" >
     
     
     
            <TextView
     
                android:layout_width="wrap_content"
     
                android:layout_height="wrap_content" />
     
     
     
            <TextView
     
                android:layout_width="wrap_content"
     
                android:layout_height="wrap_content" />
     
     
     
            <TextView
     
                android:id="@+id/station"
     
                android:layout_width="wrap_content"
     
                android:layout_height="wrap_content"
     
                android:text="AZERTY" />
     
     
     
            <TextView
     
                android:id="@+id/distance"
     
                android:layout_width="wrap_content"
     
                android:layout_height="wrap_content"
     
                android:text="OP" />
     
     
     
            <TextView
     
                android:layout_width="wrap_content"
     
                android:layout_height="wrap_content" />
     
     
     
     
     
        </LinearLayout>
     
     
     
        <LinearLayout
     
            android:layout_width="wrap_content"
     
            android:layout_height="fill_parent"
     
            android:layout_gravity="right"
     
            android:orientation="horizontal" >
     
     
     
            <LinearLayout
     
                android:id="@+id/mylot2"
     
                android:layout_width="wrap_content"
     
                android:layout_height="fill_parent"
     
                android:orientation="vertical" >
     
     
     
                <ImageView
     
                    android:id="@+id/img1"
     
                    android:layout_width="25dp"
     
                    android:layout_height="25dp" />
     
     
     
                <ImageView
     
                    android:id="@+id/img2"
     
                    android:layout_width="25dp"
     
                    android:layout_height="25dp" />
     
     
     
                <ImageView
     
                    android:id="@+id/img3"
     
                    android:layout_width="25dp"
     
                    android:layout_height="25dp" />
     
            </LinearLayout>
     
     
     
            <LinearLayout
     
                android:id="@+id/mylot3"
     
                android:layout_width="wrap_content"
     
                android:layout_height="wrap_content"
     
                android:orientation="vertical" >
     
     
     
                <ImageView
     
                    android:id="@+id/img4"
     
                    android:layout_width="25dp"
     
                    android:layout_height="25dp" />
     
     
     
                <ImageView
     
                    android:id="@+id/img5"
     
                    android:layout_width="25dp"
     
                    android:layout_height="25dp" />
     
     
     
                <ImageView
     
                    android:id="@+id/img6"
     
                    android:layout_width="25dp"
     
                    android:layout_height="25dp" />
     
            </LinearLayout>
     
     
     
            <LinearLayout
     
                android:id="@+id/mylot4"
     
                android:layout_width="wrap_content"
     
                android:layout_height="wrap_content"
     
                android:orientation="vertical" >
     
     
     
                <ImageView
     
                    android:id="@+id/img7"
     
                    android:layout_width="25dp"
     
                    android:layout_height="25dp" />
     
     
     
                <ImageView
     
                    android:id="@+id/img8"
     
                    android:layout_width="25dp"
     
                    android:layout_height="25dp" />
     
     
     
                <ImageView
     
                    android:id="@+id/img9"
     
                    android:layout_width="25dp"
     
                    android:layout_height="25dp" />
     
            </LinearLayout>
     
     
     
            <LinearLayout
     
                android:id="@+id/mylot5"
     
                android:layout_width="wrap_content"
     
                android:layout_height="wrap_content"
     
                android:orientation="vertical" >
     
     
     
                <ImageView
     
                    android:id="@+id/img10"
     
                    android:layout_width="25dp"
     
                    android:layout_height="25dp" />
     
     
     
                <ImageView
     
                    android:id="@+id/img11"
     
                    android:layout_width="25dp"
     
                    android:layout_height="25dp" />
     
     
     
                <ImageView
     
                    android:id="@+id/img12"
     
                    android:layout_width="25dp"
     
                    android:layout_height="25dp" />
     
            </LinearLayout>
     
        </LinearLayout>
     
     
     
     
    </LinearLayout>

  8. #8
    Membre extrêmement actif
    Avatar de Ryu2000
    Homme Profil pro
    Étudiant
    Inscrit en
    Décembre 2008
    Messages
    9 595
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 36
    Localisation : France, Hérault (Languedoc Roussillon)

    Informations professionnelles :
    Activité : Étudiant

    Informations forums :
    Inscription : Décembre 2008
    Messages : 9 595
    Points : 18 500
    Points
    18 500
    Par défaut
    Et ton problème c'est qu'il y a toujours 12 images ?

    Si parfois tu n'as pas à toutes les afficher, il suffit de changer la Visibility.
    On ne comprend pas grand chose à ton code, tout est trop espacé.
    Keith Flint 1969 - 2019

  9. #9
    Membre régulier
    Profil pro
    Inscrit en
    Mars 2007
    Messages
    541
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Mars 2007
    Messages : 541
    Points : 124
    Points
    124
    Par défaut
    cela ne posera pas de problème ?

  10. #10
    Membre extrêmement actif
    Avatar de Ryu2000
    Homme Profil pro
    Étudiant
    Inscrit en
    Décembre 2008
    Messages
    9 595
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 36
    Localisation : France, Hérault (Languedoc Roussillon)

    Informations professionnelles :
    Activité : Étudiant

    Informations forums :
    Inscription : Décembre 2008
    Messages : 9 595
    Points : 18 500
    Points
    18 500
    Par défaut
    Non la Visibility GONE est faite pour ça.
    Il existe aussi INVISIBLE.

    Par contre tu devras penser à mettre VISIBLE : imageView.setVisibility(View.VISIBLE); quand tu veux afficher l'image.
    Vu que tu recycle ta View, si ça se trouve une fois t'as fais imageView.setVisibility(View.GONE); et si tu changes la source de l'image dans l'ImageView l'ImageView ne sera pas là !
    Keith Flint 1969 - 2019

  11. #11
    Membre régulier
    Profil pro
    Inscrit en
    Mars 2007
    Messages
    541
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Mars 2007
    Messages : 541
    Points : 124
    Points
    124
    Par défaut
    J'ai toujours ce problème.

Discussions similaires

  1. Réponses: 0
    Dernier message: 08/11/2013, 17h19
  2. Fixer une imageview lors du scroll
    Par dsjulien dans le forum Composants graphiques
    Réponses: 3
    Dernier message: 12/10/2013, 16h41
  3. Réponses: 0
    Dernier message: 11/09/2012, 11h29
  4. Réponses: 6
    Dernier message: 15/06/2011, 11h52
  5. Réponses: 1
    Dernier message: 28/04/2009, 13h09

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