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
|
package com.restomaniak.alpha;
import java.text.NumberFormat;
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 AdapterPersoSurPlace extends BaseAdapter {
private JSONArray maListe = new JSONArray();
private JSONArray maListe2 = new JSONArray();
private Context mContext = null;
private LayoutInflater mInflater = null;
private Typeface robotoCond = null;
private Typeface robotoBold = null;
private float longFloat = 0;
private float latFloat = 0;
private float resultat =0;
private String resultatString = new String();
private Location locationRestaurant = new Location("Restaurant");
private Location locationDetecter = new Location("Detecter");
private int km = 0;
private float moyenneCuisine = 0;
private float moyenneService = 0;
private float moyenneQP = 0;
private float moyenneTotal = 0;
private JSONObject json_data = new JSONObject();
private JSONObject json_data2 = new JSONObject();
private boolean used = true;
private int indice = 0;
private int id_category_resto = 0;
private int id_category_note = 0;
public AdapterPersoSurPlace (Context context, JSONArray jArray, JSONArray jArray2, Location detecte, Float lg, Float lt)
{
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");
locationDetecter = detecte;
longFloat = lg;
latFloat = lt;
}
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_Distance = (TextView)layoutItem.findViewById(R.id.distance);
TextView tv_Type = (TextView)layoutItem.findViewById(R.id.type);
RatingBar moyenneEtoile = (RatingBar)layoutItem.findViewById(R.id.etoileListeRestoMoyenne);
//(3) : Renseignement des valeurs
try {
if(position == 0) {
used = true;
indice = 0;
}
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"));
id_category_resto = Integer.parseInt(json_data.getString("id_category"));
if (indice < maListe2.length() && used)
{
json_data2 = maListe2.getJSONObject(indice);
indice++;
id_category_note = Integer.parseInt(json_data2.getString("prout"));
used = false;
}
if (id_category_resto == id_category_note)
{
moyenneCuisine = Float.parseFloat(json_data2.getString("moyennecuisine"));
moyenneService = Float.parseFloat(json_data2.getString("moyenneservice"));
moyenneQP = Float.parseFloat(json_data2.getString("moyenneqp"));
moyenneTotal = (moyenneCuisine + moyenneService + moyenneQP)/3;
moyenneEtoile.setRating(moyenneTotal);
tv_NombreAvis.setText("("+json_data2.getString("nombreCommentaire")+" avis)");
used = true;
} else {
moyenneEtoile.setRating(0);
tv_NombreAvis.setText("(0 avis)");
}
//longFloat = Float.parseFloat(json_data.getString("villedelivered"));
//latFloat = Float.parseFloat(json_data.getString("cpdelivered"));
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//longFloat = (float) (longFloat/Math.pow(10,6));
//latFloat = (float) (latFloat/Math.pow(10,6));
locationRestaurant.setLongitude(longFloat);
locationRestaurant.setLatitude(latFloat);
resultat = locationDetecter.distanceTo(locationRestaurant);
resultat = Math.round(resultat);
if (resultat >= 1000)
{
resultat = (float) (resultat/Math.pow(10,3));
km = 1;
}
NumberFormat Myformat = NumberFormat.getInstance();
Myformat.setMinimumFractionDigits(1); //Nb de Digit mini
Myformat.setMaximumFractionDigits(1); //Nb de Digit Maxi
resultatString = Myformat.format(resultat);
if (km == 1)
{
resultatString = resultatString + " km";
}
else {
resultatString = resultatString + " m";
}
tv_Distance.setText(resultatString);
tv_Nom.setTypeface(robotoBold);
tv_NombreAvis.setTypeface(robotoBold);
tv_PrixMoyen.setTypeface(robotoCond);
tv_prixMoyenBDD.setTypeface(robotoCond);
tv_signeEuro.setTypeface(robotoBold);
tv_Type.setTypeface(robotoCond);
tv_Distance.setTypeface(robotoBold);
//tv_Adresse.setTypeface(arial);
/*//(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;
}
} |
Partager