user3507621
user3507621

Reputation: 61

Pass the variables of the item I click on (ListView)

Here is my question. I can pass informations to my next activity, but since it is in a for, it always take the value of the last item. I got this code:

protected void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    setContentView(R.layout.afficher);
    ListView lvTrajets = (ListView)findViewById(R.id.lvTrajets);
    Bundle bundle = getIntent().getExtras();

    if(bundle.getString("json")!= null)
    {
        String ficJson = (String) bundle.get("json");
        try {
            jsonResponse = new JSONObject(ficJson);
            JSONArray jsonArray = jsonResponse.getJSONArray("propositions");

            for (int i = 0; i < jsonArray.length(); i++) {
                Map<String,String> proposition = new HashMap<String,String>(2);

                JSONObject jsonObj = jsonArray.getJSONObject(i);

                String propId = jsonObj.getString("id");
                Log.i("Valeur id", propId);
                extras.putString("id", propId);

                String propVille = jsonObj.getString("ville");
                Log.i("Valeur ville", propVille);
                extras.putString("ville", propVille);

                String propLieu = jsonObj.getString("lieu");
                Log.i("Valeur lieu", propLieu);
                extras.putString("lieu", propLieu);

                String propGare = jsonObj.getString("gare");
                Log.i("Valeur gare", propGare);
                extras.putString("gare", propGare);


                proposition.put("Date",propId);
                proposition.put("Trajet", propLieu+" de "+propVille+" --> "+propGare);
                propositions.add(proposition);
            }

            SimpleAdapter adapter = new SimpleAdapter(this, propositions, android.R.layout.simple_list_item_2,
                    new String[] {"Date", "Trajet"},
                    new int[] { android.R.id.text1,
                    android.R.id.text2});
            lvTrajets.setAdapter(adapter);

            lvTrajets.setOnItemClickListener(new OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> adapter, View view, int position, long arg) {
                    Intent iReserver = new Intent(Afficher.this, Reserver.class);
                    iReserver.putExtras(extras);
                    startActivity(iReserver);
                } 
            });

        } catch (JSONException e) {
            e.printStackTrace();
        }
    }
}

But I don't know how to get the informations of the item I click on.

If someone can help me please.

Upvotes: 0

Views: 889

Answers (1)

Raghunandan
Raghunandan

Reputation: 133560

You can use the position which is the index of list item.

lvTrajets.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,int position, long id) {

 HashMap<String,String> map =(HashMap<String,String>)adapter.getItemAtPosition(position);
 String date  = (String)map.get("Date");      
 String trajet  = (String)map.get("Trajet");   
 Intent iReserver = new Intent(Afficher.this, Reserver.class);
 iReserver.putExtras("key1",date);
 iReserver.putExtras("key2",trajet); 
 startActivity(iReserver);
}
});

Edit:

Add each item separately to hashmap

proposition.put("Trajet", propLieu);
proposition.put("Trajet1", propVille);
proposition.put("Trajet2", +propGare);

Then in onItemCLick

String trajet  = (String)map.get("Trajet"); 
String trajet1  = (String)map.get("Trajet1"); 
String trajet2  = (String)map.get("Trajet2"); 

Upvotes: 1

Related Questions