阅读ListView Android中的项目

时间:2015-03-26 15:03:22

标签: java android listview hashmap

我想获取使用hasmap制作的列表视图的信息,为此我已经制作了这段代码:

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();
        }
    }
}

我收到数据并且listview没问题,但是当我点击某个项目时,我只收到最后一项......

你能帮助我吗?

1 个答案:

答案 0 :(得分:0)

拉米找到了这个解决方案:

Toast.makeText(getBaseContext(), rdvous.get(position).get("titre"),Toast.LENGTH_LONG).show();

这是工作

感谢Rami; - )