Note that there are some explanatory texts on larger screens.

plurals
  1. POlist items of HashMap in a list view, android
    text
    copied!<p>I am writing an android application to display the nearby locations, I have two activities; one to show locations on the map and the other is to list them in a ListView.</p> <p>in the first activity, I store the information about each place in a hashMap, these information includes: place name, place longitude and latitude, this is the code to store info in a HashMap:</p> <p>// Clears all the existing markers mGoogleMap.clear();</p> <pre><code> 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>in this activity I have a button to direct me to the second activity which must list the nearby places in a ListView; this is the code for the button:</p> <pre><code>public void list_airports(View v) { Intent intent; switch (v.getId()) { case R.id.list_items: intent = new Intent(getApplicationContext(), List_airports.class); intent.putExtra("com.example.dashboard_our.hmPlace",hmPlace); startActivity(intent); } } </code></pre> <p>in the second activity I do this:</p> <pre><code>protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.list_airports); Bundle extras = getIntent().getExtras(); HashMap&lt;String, String&gt; places1=(HashMap&lt;String, String&gt;) extras.getSerializable("com.example.dashboard_our.hmPlace"); final ListView listview = (ListView) findViewById(R.id.list); list = new ArrayList&lt;String&gt;(); for (int i = 0; i &lt; places1.size(); ++i) { list.addAll(places1.values()); } } </code></pre> <p>but it just prints the first place's information many times, How could I solve this problem??</p> <p>This is the rest of the code:</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; } } public static void printMap(Map mp) { Iterator it = mp.entrySet().iterator(); while (it.hasNext()) { Map.Entry pairs = (Map.Entry)it.next(); // System.out.println(pairs.getKey() + " = " + pairs.getValue()); list.add(pairs.toString()); it.remove(); // avoids a ConcurrentModificationException } } </code></pre> <p>this is the code of the 'do in background' which parses the Json and return places:</p> <pre><code>/** A class to parse the Google Places in JSON format */ private class ParserTask extends AsyncTask&lt;String, Integer, List&lt;HashMap&lt;String,String&gt;&gt;&gt;{ JSONObject jObject; // Invoked by execute() method of this object @Override protected List&lt;HashMap&lt;String,String&gt;&gt; doInBackground(String... jsonData) { // List&lt;HashMap&lt;String, String&gt;&gt; places = null; PlaceJSONParser placeJsonParser = new PlaceJSONParser(); try{ jObject = new JSONObject(jsonData[0]); /** Getting the parsed data as a List construct */ places = placeJsonParser.parse(jObject); }catch(Exception e){ Log.d("Exception",e.toString()); } return places; } </code></pre> <p>where places:</p> <pre><code> List&lt;HashMap&lt;String, String&gt;&gt; places; </code></pre>
 

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