Skip to content Skip to sidebar Skip to footer

How To Send Values From A Selected Item On A Json Listview To Another Activity?

I'm making an app that gets countries data (name, latitude, longitude, ...) from JSON and creates a listview, where each item is a different country. That part is working but, eve

Solution 1:

HashMap implements Serializable so we can send HashMap object using putExtra and receive it using getSerializableExtra

TodasAsCategorias activity

  @Override
  public void onItemClick(AdapterView<?> pare, View view, int position, long id)     
  {
       Intent intent = new Intent(TodasAsCategorias.this, MapsActivity.class);
       intent.putExtra("data", listaPaises.get(position));
       startActivity(intent);
  }

In MapsActivity

protected void onCreate(Bundle bundle) {
    super.onCreate(savedInstanceState);

    Intent intent = getIntent();
    HashMap<String, String> hashMap = (HashMap<String, String>)intent.getSerializableExtra("data");
    String lat = hashMap.get("Coord_LAT");
    String longi = hashMap.get("Coord_LONG");
}

Solution 2:

Transmit your data to MapsActivity via Extras. Start activity like this:

Intent intent = new Intent(TodasAsCategorias.this, MapsActivity.class);
intent.putExtra("Coord_LAT", value);
intent.putExtra("Coord_LONG", value);
startActivity(intent);

And retrieve data in MapsActivity:

String Coord_LAT = getIntent().getStringExtra("Coord_LAT");
String Coord_LONG = getIntent().getStringExtra("Coord_LONG");

Solution 3:

The onItemClick() method, provides the "position" of the list item that was clicked. The item at the position contains the latitude and longitude that you need to pass to the other activity I guess. Then, the latitude and longitude can be passed as explained by the other answers, using Extras.


Solution 4:

Updated: since your listaPaises is an instance variable, @PavneetSingh answer would be more straight forward.

You may override the getItem(int position) of the SimpleAdapter to return the HashMap entry:

ListAdapter adapter = new SimpleAdapter(this, listaPaises, R.layout.list_item,
                new String[]{"Designacao", "Coord_LAT", "Coord_LONG", "Coord_Zoom"},
                new int[]{R.id.Designacao, R.id.Lat, R.id.Long, R.id.Zoom}) {
            @Override
            public Object getItem(int position) {
                if (listaPaises!=null) {
                    return listaPaises.get(position);
                }
                return super.getItem(position);
            }
        };

then in your ListView's setOnItemClickListener(...) method, you can get the returned entry by calling:

HashMap<String, String> pais = (HashMap<String, String>) adapter.getItem(position)

and pass the HashMap entry with Intent's putExtra() method to MapsActivity.

Kindly note that in order to call the adapter within the anonymous inner class, you would need to change the local variable ListAdapter adapter to instance variable of your class.


Post a Comment for "How To Send Values From A Selected Item On A Json Listview To Another Activity?"