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
| public class ListeProduits extends Activity {
ListView list;
TextView Produit;
TextView Prix;
TextView EnStock;
TextView LPProduit,LPPrix,LPDescription;
ImageView image,ImageProduit;
Button Btngetdata,Ajouter;
ImageButton VoirMonPanier;
ArrayList<HashMap<String, String>> listp = new ArrayList<HashMap<String, String>>();
//URL to get JSON Array
private static String url = "http://xxxxx.free.fr/xxxx.php";
//JSON Node Names
//private static final String TAG_OS = "android";
private static final String TAG_PRODUIT = "Produit";
private static final String TAG_PRIX = "Prix";
private static final String TAG_ENSTOCK = "Quantite";
private static final String TAG_DESCRIPTION = "Description";
JSONArray android = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.liste_produits);
listp = new ArrayList<HashMap<String, String>>();
VoirMonPanier = (ImageButton)findViewById(R.id.LPPanier);
Btngetdata = (Button)findViewById(R.id.getdata);
Btngetdata.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
new JSONParse().execute();
}
});
}
private class JSONParse extends AsyncTask<String, String, JSONArray> {
private ProgressDialog pDialog;
@Override
protected void onPreExecute() {
super.onPreExecute();
Produit = (TextView)findViewById(R.id.Produit);
Prix = (TextView)findViewById(R.id.Prix);
EnStock = (TextView)findViewById(R.id.Enstock);
Ajouter = (Button)findViewById(R.id.LPAjouterPanier);
LPProduit = (TextView)findViewById(R.id.LPProduit);
LPPrix = (TextView)findViewById(R.id.LPPrix);
LPDescription = (TextView)findViewById(R.id.LPDescription);
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
final String produit = c.getString(TAG_PRODUIT);
final String prix = c.getString(TAG_PRIX);
final String dispo = c.getString(TAG_ENSTOCK);
final String description = c.getString(TAG_DESCRIPTION);
// 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(dispo != "null") {
//EnStock.setTextColor(getResources().getColor(R.color.green));
map.put(TAG_ENSTOCK, "En Stock");
}
else {
//EnStock.setTextColor(getResources().getColor(R.color.red));
map.put(TAG_ENSTOCK, "Rupture de stock");
}
listp.add(map);
list=(ListView)findViewById(R.id.list);
ListAdapter adapter = new SimpleAdapter(ListeProduits.this, listp,
R.layout.list_v,
new String[] { TAG_PRODUIT,TAG_PRIX,TAG_ENSTOCK}, new int[] {
R.id.Produit,R.id.Prix,R.id.Enstock});
list.setAdapter(adapter);
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
//Toast.makeText(ListeProduits.this, "You Clicked at "+oslist.get(+position).get("name"), Toast.LENGTH_SHORT).show();
Ajouter.setVisibility(View.VISIBLE);
LPProduit.setText(produit);
LPPrix.setText("Prix () : "+prix);
LPDescription.setText(description);
}
});
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}
} |
Partager