Passage d'un SimpleAdapter à un ArrayAdapter
Salut à tous j'ai trouvé 2 Tuto qui peuvent m'aider à afficher des données d'une base MySql dans un ListView.
Le 1er utilise un SimpleAdapter et récupére les données d'une base distante avec JSON
Code :
Code:
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();
}
}); |
Le 2éme utilise un ArrayAdapter et récupére les données à partir d'un simple tableau
Code :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
| planets = (Planet[]) getLastNonConfigurationInstance() ;
if ( planets == null ) {
planets = new Planet[] {
new Planet("Mercury"), new Planet("Venus"), new Planet("Earth"),
new Planet("Mars"), new Planet("Jupiter"), new Planet("Saturn"),
new Planet("Uranus"), new Planet("Neptune"), new Planet("Ceres"),
new Planet("Pluto"), new Planet("Haumea"), new Planet("Makemake"),
new Planet("Eris")
};
}
ArrayList<Planet> planetList = new ArrayList<Planet>();
planetList.addAll( Arrays.asList(planets) );
// Set our custom array adapter as the ListView's adapter.
listAdapter = new PlanetArrayAdapter(this, planetList);
mainListView.setAdapter( listAdapter ); |
Alors que mon problème se pose dans la récupération des données à partir de la base MySql distante dans un ArrayList .. quelqu'un peut m'aider ??