Comportement suspect de getView
Bonjour à tous,
je me tourne vers vous car j'ai un comportement suspect de mon getView().
En effet j'ai crée mon propre adapter pour alimenter une de mes listView présente dans l'application. Voici le code de mon adapter:
Code:
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
|
package com.restomaniak.alpha;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.content.Context;
import android.graphics.Typeface;
import android.location.Location;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.RatingBar;
import android.widget.TextView;
public class AdapterPersoLivraison extends BaseAdapter {
private JSONArray maListe = null;
private JSONArray maListe2 = null;
private Context mContext;
private LayoutInflater mInflater;
Typeface robotoCond;
Typeface robotoBold;
private float longFloat;
private float latFloat;
private float moyenneCuisine;
private float moyenneService;
private float moyenneQP;
private int moyenneTotal;
private float resultat;
private String resultatString;
private int id_category_resto;
private int id_category_note;
Location locationRestaurant = new Location("Restaurant");
Location locationDetecter = new Location("Detecter");
Float longitude;
Float latitude;
private int km = 0;
private int retro = 0;
private boolean used = true;
JSONObject json_data=null;
JSONObject json_data2=null;
private int indice = 0;
public AdapterPersoLivraison (Context context, JSONArray jArray, JSONArray jArray2)
{
mContext = context;
maListe = jArray;
maListe2 = jArray2;
mInflater = LayoutInflater.from(mContext);
robotoCond = Typeface.createFromAsset(mContext.getAssets(), "Roboto_Condensed.ttf");
robotoBold = Typeface.createFromAsset(mContext.getAssets(), "Roboto_Bold.ttf");
}
public int getCount() {
return maListe.length();
// TODO Auto-generated method stub
}
public Object getItem(int position) {
// TODO Auto-generated method stub
try {
return maListe.get(position);
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return position;
}
public long getItemId(int position) {
// TODO Auto-generated method stub
return (position);
}
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
LinearLayout layoutItem;
//(1) : Réutilisation des layouts
if (convertView == null) {
//Initialisation de notre item à partir du layout XML "personne_layout.xml"
layoutItem = (LinearLayout) mInflater.inflate(R.layout.affichageitembis, parent, false);
} else {
layoutItem = (LinearLayout) convertView;
}
//(2) : Récupération des TextView de notre layout
TextView tv_Nom = (TextView)layoutItem.findViewById(R.id.nom);
//TextView tv_Adresse = (TextView)layoutItem.findViewById(R.id.adresse);
TextView tv_PrixMoyen = (TextView)layoutItem.findViewById(R.id.prixMoyen);
TextView tv_NombreAvis = (TextView)layoutItem.findViewById(R.id.avis);
TextView tv_prixMoyenBDD = (TextView)layoutItem.findViewById(R.id.infoprixMoyen);
TextView tv_signeEuro = (TextView)layoutItem.findViewById(R.id.prixMoyeneuro);
TextView tv_Type = (TextView)layoutItem.findViewById(R.id.type);
RatingBar moyenneEtoile = (RatingBar)layoutItem.findViewById(R.id.etoileListeRestoMoyenne);
//(3) : Renseignement des valeurs
try {
json_data = maListe.getJSONObject(position);
tv_Nom.setText(json_data.getString("name"));
tv_Type.setText(json_data.getString("type"));
tv_PrixMoyen.setText(json_data.getString("average_price"));
System.out.println(position); //pour voir l'evolution de la variable
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
tv_Nom.setTypeface(robotoBold);
tv_NombreAvis.setTypeface(robotoBold);
tv_PrixMoyen.setTypeface(robotoCond);
tv_prixMoyenBDD.setTypeface(robotoCond);
tv_signeEuro.setTypeface(robotoBold);
tv_Type.setTypeface(robotoCond);
/*//(4) Changement de la couleur du fond de notre item
if (mListP.get(position).genre == Personne.MASCULIN) {
layoutItem.setBackgroundColor(Color.BLUE);
} else {
layoutItem.setBackgroundColor(Color.MAGENTA);
}*/
//On retourne l'item créé.
return layoutItem;
}
} |
Comme vous pouvez le voir j'ai ajouté cette ligne de code:
Code:
1 2
|
System.out.println(position); //pour voir l'evolution de la variable |
A savoir que maListe à une taille de 5.
Voilà ce que je récupère dans le logcat:
http://www.restomaniak.com/media/getView.PNG
Ce n'est quand même pas normal si ? Normalement je devrais avoir une série de 0 1 2 3 et 4 mais qu'une seul fois. Je ne comprend pas d'où celà sort.
Je vous remercie.
Italia57