Note that there are some explanatory texts on larger screens.

plurals
  1. POfill listView from a HashMap data passed from another activity, android
    primarykey
    data
    text
    <p>I am writing an android application to view nearby locations using Google places API, my application consists of two activity, the first activity finds the nearby locations, store info about these locations in a HashMap, and displays them on a map. in the second activity I want to get the names of locations from the HashMap from the first activity and display them in a ListView.</p> <p>the first activity:</p> <pre><code> HashMap&lt;String, String&gt; hmPlace; </code></pre> <p>protected void onPostExecute(List> list){</p> <pre><code> // Clears all the existing markers mGoogleMap.clear(); for(int i=0;i&lt;list.size();i++){ // Creating a marker MarkerOptions markerOptions = new MarkerOptions(); // Getting a place from the places list //HashMap&lt;String, String&gt; hmPlace = list.get(i); // Getting latitude of the place double lat = Double.parseDouble(hmPlace.get("lat")); // Getting longitude of the place double lng = Double.parseDouble(hmPlace.get("lng")); // Getting name String name = hmPlace.get("place_name"); // listP[i]=hmPlace.get("place_name"); Log.d("places=",hmPlace.get("place_name")); // Getting vicinity String vicinity = hmPlace.get("vicinity"); LatLng latLng = new LatLng(lat, lng); // Setting the position for the marker markerOptions.position(latLng); // Setting the title for the marker. //This will be displayed on taping the marker markerOptions.title(name + " : " + vicinity); // Placing a marker on the touched position Marker m = mGoogleMap.addMarker(markerOptions); // Linking Marker id and place reference mMarkerPlaceLink.put(m.getId(), hmPlace.get("reference")); } } </code></pre> <p>I passed the HashMap like this:</p> <pre><code>intent = new Intent(getApplicationContext(), List_airports.class); intent.putExtra("com.example.dashboard_our.hmPlace",hmPlace); startActivity(intent); </code></pre> <p>I get the values from the HashMap in the second activity :</p> <pre><code>final ArrayList&lt;String&gt; list = new ArrayList&lt;String&gt;(); for (int i = 0; i &lt; places1.size(); ++i) { list.addAll(places1.values()); } </code></pre> <p>where places1 is HashMap I got from another activity :</p> <pre><code>HashMap&lt;String, String&gt; places1=(HashMap&lt;String, String&gt;) extras.getSerializable("com.example.dashboard_our.hmPlace"); </code></pre> <p>but It only brings the first element and print it many times</p> <p>this is the rest of the second activity:</p> <pre><code>final StableArrayAdapter adapter = new StableArrayAdapter(this, android.R.layout.simple_list_item_1, list); listview.setAdapter(adapter); listview.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView&lt;?&gt; parent, final View view, int position, long id) { final String item = (String) parent.getItemAtPosition(position); view.animate().setDuration(2000).alpha(0) .withEndAction(new Runnable() { @Override public void run() { list.remove(item); adapter.notifyDataSetChanged(); view.setAlpha(1); } }); } }); } private class StableArrayAdapter extends ArrayAdapter&lt;String&gt; { HashMap&lt;String, Integer&gt; mIdMap = new HashMap&lt;String, Integer&gt;(); public StableArrayAdapter(Context context, int textViewResourceId, List&lt;String&gt; objects) { super(context, textViewResourceId, objects); for (int i = 0; i &lt; objects.size(); ++i) { mIdMap.put(objects.get(i), i); } } @Override public long getItemId(int position) { String item = getItem(position); return mIdMap.get(item); } @Override public boolean hasStableIds() { return true; } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload