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
| ArrayList<HashMap<String, String>> mylist = new ArrayList<HashMap<String, String>>();
JSONObject json = JSONfunctions.getJSONfromURL("http://192.168.1.66/android_connect/get_all_products.php");
try{
JSONArray products = json.getJSONArray("products");
for(int i=0;i<products.length();i++){
HashMap<String, String> map = new HashMap<String, String>();
JSONObject e = products.getJSONObject(i);
map.put("id", String.valueOf(i));
map.put("des", "Designation :" + e.getString("des"));
map.put("pri_uni", "Prix Unitaire: " + e.getString("pri_uni"));
map.put("pid", "PID: " + e.getString("pid"));
mylist.add(map);
}
}catch(JSONException e) {
Log.e("log_tag", "Error parsing data "+e.toString());
}
ListAdapter adapter = new SimpleAdapter(this, mylist , R.layout.main,
new String[] { "des", "pri_uni" },
new int[] { R.id.item_title, R.id.item_subtitle });
setListAdapter(adapter);
final ListView lv = getListView();
lv.setTextFilterEnabled(true);
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
@SuppressWarnings("unchecked")
HashMap<String, String> o = (HashMap<String, String>) lv.getItemAtPosition(position);
Toast.makeText(Main.this, "ID '" + o.get("pid") + "' was clicked.", Toast.LENGTH_SHORT).show();
}
}); |
Partager