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 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301
|
package com.android.webtrack;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashMap;
import SynchronizationTypes.ByteListReader;
import SynchronizationTypes.ByteListWriter;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;
public class ListeVehiculesActivity extends Activity {
private ListView lv1;
private int[] tab;
int tailletot;
Socket leSocket;
DataOutputStream outToServer;
DataInputStream inFromServer;
@SuppressWarnings("rawtypes")
ArrayList <Comparable> ListeDesVehicules = new ArrayList<Comparable>();
ArrayList <Integer> IdsVehicules = new ArrayList <Integer> ();
int taille = 0;
int year;
int month;
int day;
String ip;
String port;
Calendar c;
byte[] b = new byte[100000];
int e;
ByteListWriter BLW = new ByteListWriter();
ArrayList <Byte> array = new ArrayList <Byte>();
int numVehicule;
String chaine1;
ArrayList<HashMap<String, String>> listItem = new ArrayList<HashMap<String, String>>();
HashMap<String, String> map;
int in;
String s;
protected ProgressDialog myProgressDialog;
final Handler uiThreadCallback = new Handler();
@Override
public void onCreate(Bundle icicle)
{
/*
* A la création de la fenêtre
*/
super.onCreate(icicle);
myProgressDialog = ProgressDialog.show(ListeVehiculesActivity.this,
"", "Chargement", true);
final Runnable runInUIThread = new Runnable() {
public void run() {
afficher_list();
}
};
new Thread() {
@Override public void run() {
myProgressDialog.dismiss();
uiThreadCallback.post(runInUIThread);
}
}.start();
}
public void afficher_list(){
setContentView(R.layout.main_vehicules);
/****************************
* Récupération des paramètres
*****************************/
Bundle parametres = getIntent().getExtras();
year = parametres.getInt("YEAR");
month = parametres.getInt("MONTH");
day = parametres.getInt("DAY");
ip = parametres.getString("IP");
port = parametres.getString("PORT");
c = Calendar.getInstance();
/*
* Par défaut la date du jour
*/
if(year == 0 && month == 0 && day == 0){
year = Calendar.getInstance().get(Calendar.YEAR);
month = Calendar.getInstance().get(Calendar.MONTH) + 1; // Parce que Janvier est en position 0
day = Calendar.getInstance().get(Calendar.DATE);
c.set(year,month,day);
}
/*
* Sinon on prend en compte les paramètres de l'application
*/
else {
// On crée une date avec les paramètres
c.set(year,month,day);
}
try {
e = Integer.valueOf(port);
leSocket = new Socket(ip, e);
outToServer = new DataOutputStream(leSocket.getOutputStream());
inFromServer = new DataInputStream(leSocket.getInputStream());
BLW.WriteByte((byte) 0);
BLW.WriteByte((byte) 0x07);
BLW.WriteInt16((byte) 991);
BLW.WriteString("synoptis");
BLW.WriteString("exeo");
BLW.WriteByte((byte) 0x31);
BLW.WriteDate(c);
for (int i=0;i<BLW.Count();i++){
b[i] = BLW._bytes.get(i);
}
outToServer.write(b,0,BLW.Count());
@SuppressWarnings("unused")
byte ACK = inFromServer.readByte();
tailletot = inFromServer.read() + inFromServer.read()* 256 + inFromServer.read() * 256 * 256 + inFromServer.read() * (256*256*256) ;
@SuppressWarnings("unused")
int TaillePaquet = inFromServer.read() + inFromServer.read()* 256 + inFromServer.read() * 256 * 256 + inFromServer.read() * (256*256*256) ;
for (int i=0;i<tailletot;i++){
int valeur;
valeur = inFromServer.read();
array.add((byte)valeur);
}
ByteListReader BLR = new ByteListReader(array);
while (BLR.CanRead() == true) {
numVehicule = BLR.ReadInt16();
chaine1 = BLR.ReadString();
ListeDesVehicules.add(numVehicule); // Ajout de l'équipe dans la liste
IdsVehicules.add(numVehicule);
ListeDesVehicules.add(chaine1);
taille = ListeDesVehicules.size();
}
tab = new int[IdsVehicules.size()];
for (int idv=0;idv<IdsVehicules.size();idv++){
tab[idv] = (Integer) IdsVehicules.get(idv);
}
leSocket.close();
} // try
catch (UnknownHostException ex)
{
System.err.println("Machine inconnue : "+ex);
ex.printStackTrace();
}
catch (IOException ex)
{
System.err.println("Erreur : "+ex);
ex.printStackTrace();
}
lv1 = (ListView) findViewById(R.id.ListView01);
/**********************************************************************************************************************************
* Preparation de la liste à afficher dans la vue Android
**********************************************************************************************************************************/
for(int i=0;i<taille;i=i+2){
in = (Integer) ListeDesVehicules.get(i);
s = (String) ListeDesVehicules.get(i+1);
//Création d'une HashMap pour insérer les informations du premier item de notre listView
map = new HashMap<String, String>();
//on insère un élément titre que l'on récupérera dans le textView titre créé dans le fichier affichageitem.xml
map.put("id", String.valueOf(in));
//on insère un élément description que l'on récupérera dans le textView description créé dans le fichier affichageitem.xml
map.put("nom", s);
//on insère la référence à l'image (convertit en String car normalement c'est un int) que l'on récupérera dans l'imageView créé dans le fichier affichageitem.xml
map.put("img", String.valueOf(R.drawable.cam));
//enfin on ajoute cette hashMap dans la arrayList
listItem.add(map);
}
if (tailletot != 0){
map = new HashMap<String, String>();
map.put("id", "TOUS");
map.put("nom", "Tous les véhicules");
map.put("img", String.valueOf(R.drawable.cams));
listItem.add(map);
}
else {
// Sinon la liste est vide, tout simplement
Toast.makeText(ListeVehiculesActivity.this, "Il n'existe pas de camion(s) ayant circulé à cete date.", Toast.LENGTH_LONG).show();
}
//Création d'un SimpleAdapter qui se chargera de mettre les items présent dans notre list (listItem) dans la vue affichageitem
SimpleAdapter mSchedule = new SimpleAdapter (this.getBaseContext(), listItem, R.layout.liste_vehicules,
new String[] {"img", "id", "nom"}, new int[] {R.id.img, R.id.id, R.id.nom});
//On attribut à notre listView l'adapter que l'on vient de créer
lv1.setAdapter(mSchedule);
//Enfin on met un écouteur d'évènement sur notre listView
lv1.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> a, View v, int position, long id) {
//on récupère la HashMap contenant les infos de notre item (titre, description, img)
@SuppressWarnings("unchecked")
HashMap<String, String> map = (HashMap<String, String>) lv1.getItemAtPosition(position);
//on créer une boite de dialogue
AlertDialog.Builder adb = new AlertDialog.Builder(ListeVehiculesActivity.this);
//on attribut un titre à notre boite de dialogue
adb.setTitle("Sélection Item");
//on insère un message à notre boite de dialogue, et ici on affiche le titre de l'item cliqué
adb.setMessage("Vous avez sélectionné le véhicule : "+map.get("id"));
//on indique que l'on veut le bouton ok à notre boite de dialogue
adb.setPositiveButton("Ok", null);
//on affiche la boite de dialogue
adb.show();
Intent intent= getIntent();
intent.putExtra("IDVEHICULE", map.get("id"));
intent.putExtra("NOMVEHICULE", map.get("nom"));
intent.putExtra("TAB",tab);
setResult(RESULT_OK, intent);
finish();
}
});
}
protected HashMap<String, String> getItemAtPosition(int position) {
// TODO Auto-generated method stub
return null;
}
} |
Partager