Android : Read item in ListView Android

on Thursday, March 26, 2015


I want take the information of a list view made with a hasmap and for this i have made this code:



public class MainActivity extends Activity {

ListView rdv;
HashMap<String, String> map;
ArrayList<HashMap<String, String>> rdvous = new ArrayList<>();
String URL;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);


URL = "http://172.16.32.101/?mot=listerrdv&id=5";

rdv = (ListView)findViewById(R.id.listRDV);
new HttpAsyncTask().execute(URL);

rdv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
@SuppressWarnings("unchecked")
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(getBaseContext(),map.get("titre"),Toast.LENGTH_LONG).show();
}
});
}


. . .



private class HttpAsyncTask extends AsyncTask<String, Void, String> {
@Override
protected String doInBackground(String... urls) {

return GET(urls[0]);
}
// onPostExecute displays the results of the AsyncTask.
@Override
protected void onPostExecute(String result) {
try {
JSONArray json = new JSONArray(result);
String str = "";

for(int i = 0 ; i<json.length() ; i++) {
map = new HashMap<String, String>();
str ="";
str += "RDV " + (i+1) + "\n";
str += "jour: " + json.getJSONObject(i).getString("jour") + "\n";
str += "heure: " + json.getJSONObject(i).getString("heure") + "\n";
str += "perso: " + json.getJSONObject(i).getString("perso") + "\n";
str += "acte: " + json.getJSONObject(i).getString("acte") + "\n";
map.put("titre",str);
str = "";
str += json.getJSONObject(i).getString("idrdv");
map.put("description",str);
rdvous.add(map);
}
SimpleAdapter adapt = new SimpleAdapter (getBaseContext(), rdvous, R.layout.list_rdv,
new String[] {"titre", "description"}, new int[] {R.id.titre, R.id.description});


rdv.setAdapter(adapt);


} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}


I receive data and listview is Ok but when I click on an item, i only receive the last item...


Can you help me?


0 comments:

Post a Comment