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
| package com.example.test;
public class ListeProduits extends Activity {
ListView list;
....................
LazyAdapter adapter2;
ArrayList<HashMap<String, String>> listp = new ArrayList<HashMap<String, String>>();
//URL to get JSON Array
private static String url = "http://xxxx.free.fr/xxxxx.php";
//JSON Node Names
public static final String TAG_PRODUIT = "Produit";
public static final String TAG_PRIX = "Prix";
public static final String TAG_ENSTOCK = "Quantite";
public static final String TAG_DESCRIPTION = "Description";
public static final String TAG_RESTANT = "Restant";
public static final String TAG_URLIMAGE = "URLimage";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.liste_produits);
new JSONParse().execute();
LPProduit = (TextView)findViewById(R.id.LPProduit);
.....................
listp = new ArrayList<HashMap<String, String>>();
}
public class JSONParse extends AsyncTask<String, String, JSONArray> {
private ProgressDialog pDialog;
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(ListeProduits.this);
pDialog.setMessage("Chargement des données ...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(true);
pDialog.show();
}
@Override
protected JSONArray doInBackground(String... args) {
JSONParser jParser = new JSONParser();
// Getting JSON from URL
JSONArray json = jParser.getJSONFromUrl(url);
return json;
}
@Override
protected void onPostExecute(JSONArray json) {
pDialog.dismiss();
try {
// On récupère le tableau JSON de l'URL
for(int i = 0; i < json.length(); i++){
JSONObject c = json.getJSONObject(i);
// On stocke les valeurs JSON dans une variable
produit = c.getString(TAG_PRODUIT);
prix = c.getString(TAG_PRIX);
dispo = c.getString(TAG_ENSTOCK);
description = c.getString(TAG_DESCRIPTION);
URLimage = c.getString(TAG_URLIMAGE);
restant = Integer.parseInt(dispo);
// Adding value HashMap key => value
HashMap<String, String> map = new HashMap<String, String>();
map.put(TAG_PRODUIT, produit);
map.put(TAG_PRIX, prix +" euros");
if(restant == 0) {
map.put(TAG_ENSTOCK, "Rupture de stock");
}
else {
map.put(TAG_ENSTOCK, "En Stock");
}
map.put(TAG_DESCRIPTION, description);
if(restant != 0 && restant < 5) {
Restant = "Il ne reste plus que "+restant+" exemplaires";
map.put(TAG_RESTANT, Restant);
}
else {
map.put(TAG_RESTANT, "null");
}
map.put(TAG_URLIMAGE, URLimage);
listp.add(map);
list=(ListView)findViewById(R.id.list);
adapter2 = new LazyAdapter(ListeProduits.this, listp);
list.setAdapter(adapter);
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
...................
}
});
}
} catch (JSONException e) {
e.printStackTrace();
}
}
} |
Partager