0

I have an an main activity from which I want to send an info to a second activity. The info is in a hashmap which helps populate an arraylist which in turn populates a ListView.

package com.example.tp5;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.SimpleAdapter;

import java.util.ArrayList;
import java.util.HashMap;

public class MainActivity extends AppCompatActivity {

    ListView ls;
    ArrayList<HashMap<String,String>> values = new ArrayList<HashMap<String, String>>();
    HashMap<String,String> map;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        ls= findViewById(R.id.listview);
        map=new HashMap<String, String>();
        map.put("ville","Sejnane");
        map.put("type", "Gares");
        map.put("img", String.valueOf(R.mipmap.Station6));
        values.add(map);

        map=new HashMap<String, String>();
        map.put("ville","Sened");
        map.put("type", "Gares");
        map.put("img", String.valueOf(R.mipmap.Station6));
        values.add(map);

        map=new HashMap<String, String>();
        map.put("ville","Sfax");
        map.put("type", "Gares");
        map.put("img", String.valueOf(R.mipmap.Station6));
        values.add(map);

        map=new HashMap<String, String>();
        map.put("ville","Sidi Abid");
        map.put("type", "Gares");
        map.put("img", String.valueOf(R.mipmap.Station6));
        values.add(map);

        map=new HashMap<String, String>();
        map.put("ville","Sidi Mtir");
        map.put("type", "Gares");
        map.put("img", String.valueOf(R.mipmap.Station6));
        values.add(map);

        SimpleAdapter adapter = new SimpleAdapter(MainActivity.this, values, R.layout.item, new String[] {"ville", "type", "img"}, new int[] {R.id.ville, R.id.type, R.id.img});
        ls.setAdapter(adapter);

        ls.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent i = new Intent(MainActivity.this, SecondActivity.class);
                //i dont know what to do here
                startActivity(i);
            }
        });

    }
}

I want to send for example the "ville" value from the click item to the second activity. What should i do?

  • You want get Value of key "ville" from Map, and send that value for Second Activity? – Zeroes May 01 '20 at 18:07
  • you can use intent.putExtra. Read more: [this answer](https://stackoverflow.com/questions/5265913/how-to-use-putextra-and-getextra-for-string-data). Or [intent](https://developer.android.com/reference/android/content/Intent) – Zeroes May 01 '20 at 18:10
  • @Zeroes Ok but how do i get the value of "ville" from the clicked item. – MikeWazowski May 01 '20 at 19:14

0 Answers0