Java 在Hashmap中为不同的键获取相同的值

Java 在Hashmap中为不同的键获取相同的值,java,android,arraylist,hashmap,Java,Android,Arraylist,Hashmap,我已经实现了hashmap,在其中我将接收到的json对象分配给一些字符串,如rvalue、price、dsvalue,并将这些字符串作为一个值传递给KEY_EXTRA、KEY_PLEVEL、KEY_DISTANCE。然后我传递这个hashmap并将其添加到arraylist中。我已经为listview的每个项目定义了cutom视图,我想在其中设置这些值,如rating,距离当我使用此数据设置textview的文本时,它仅将所有其他textview的KEY_DISTANCE值用作文本。rvalu

我已经实现了hashmap,在其中我将接收到的json对象分配给一些字符串,如rvalue、price、dsvalue,并将这些字符串作为一个值传递给KEY_EXTRA、KEY_PLEVEL、KEY_DISTANCE。然后我传递这个hashmap并将其添加到arraylist中。我已经为listview的每个项目定义了cutom视图,我想在其中设置这些值,如rating,距离当我使用此数据设置textview的文本时,它仅将所有其他textview的KEY_DISTANCE值用作文本。rvalue、price、dsvalue的值是不同的

初始化:

     public static String KEY_REFERENCE = "reference"; // id of the place
     public static String KEY_NAME = "name"; // name of the place
     public static String KEY_VICINITY = "vicinity"; // Place area name
     public static String KEY_EXTRA ;// Place area name
     public static String KEY_PLEVEL ;
     public static String KEY_DISTANCE;
下面是hashmap代码:

          if(status.equals("OK")){
                    // Successfully got places details
                    if (nearPlaces.results != null) {
                        // loop through each place
                        for (Place p : nearPlaces.results) {

                            String rvalue="-NA-";
                            String price="-NA-";


                            HashMap<String, String> map = new HashMap<String, String>();
                            double rate=p.rating;
                             rvalue = String.valueOf(rate);
                            //Log.d("Rating",rvalue );
                            double latitude = p.geometry.location.lat;
                            double longitude = p.geometry.location.lng;
                            Location selected_location=new Location("locationA");
                            selected_location.setLatitude(userlat);
                            selected_location.setLongitude(userlng);
                        Location near_locations=new Location("locationA");
                            near_locations.setLatitude(latitude);
                            near_locations.setLongitude(longitude);

                        double distance=selected_location.distanceTo(near_locations);
                        double dvalue=(Math.round(distance));
                         String dsvalue = String.valueOf(dvalue)
     // Place reference is used to get "place full details"
                            map.put(KEY_REFERENCE, p.reference);

                            // Place name
                            map.put(KEY_NAME, p.name);


                            map.put(KEY_EXTRA,rvalue);
                             price= p.price_level;
                            map.put(KEY_PLEVEL,price);
                            placesListItems.add(map);
                       //    Log.d("price level",price);

                        map.put(KEY_DISTANCE, dsvalue+" M");
                            placesListItems.add(map);



                        }

                        // list adapter
                        ListAdapter adapter = new SimpleAdapter(nearby_mainactivity.this, placesListItems,
                                R.layout.list_item,
                                new String[] { KEY_REFERENCE, KEY_NAME, KEY_EXTRA, KEY_PLEVEL, KEY_DISTANCE}, new int[] {
                                        R.id.reference, R.id.name, R.id.textView3,R.id.textView5 ,R.id.textView7});

                        // Adding data into listview
                        lv.setAdapter(adapter);
以下是每个litview项的视图的xml布局:

   <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" 
>

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="70dp" >

    <TextView android:id="@+id/reference"
    android:visibility="gone"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"/>

<TextView
    android:id="@+id/name"
    android:layout_width="265dp"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentRight="true"
    android:layout_marginLeft="5dp"
    android:padding="10dip"
    android:textSize="16dip"
    android:textStyle="bold" />

<TextView
    android:id="@+id/textView2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_below="@+id/name"
    android:text="Rating : "
    android:textStyle="bold"
    android:layout_marginLeft="15dp"
    android:textAppearance="?android:attr/textAppearanceSmall" />

<TextView
    android:id="@+id/textView4"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBaseline="@+id/textView2"
    android:layout_alignBottom="@+id/textView2"
    android:layout_marginLeft="15dp"
    android:layout_toRightOf="@+id/textView3"
     android:textStyle="bold"
    android:text="Price Level : "
    android:textAppearance="?android:attr/textAppearanceSmall" />

<TextView
    android:id="@+id/textView3"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/name"
    android:layout_toRightOf="@+id/textView2"


    android:textAppearance="?android:attr/textAppearanceSmall" />

<TextView
    android:id="@+id/textView5"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBaseline="@+id/textView4"
    android:layout_alignBottom="@+id/textView4"
    android:layout_toRightOf="@+id/textView4"


    android:textAppearance="?android:attr/textAppearanceSmall" />

<TextView
    android:id="@+id/textView7"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBaseline="@+id/textView6"
    android:layout_alignBottom="@+id/textView6"
    android:layout_toRightOf="@+id/textView6"

    android:textAppearance="?android:attr/textAppearanceSmall" />

<TextView
    android:id="@+id/textView6"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBaseline="@+id/textView5"
    android:layout_alignBottom="@+id/textView5"
    android:layout_marginLeft="16dp"
    android:layout_toRightOf="@+id/textView5"
     android:textStyle="bold"
    android:text="Distance : "
    android:textAppearance="?android:attr/textAppearanceSmall" />

</RelativeLayout>

你能发布密钥常量的初始化吗?嘿,朋友,我已经发布了初始化,你在哪里初始化:密钥额外,密钥水平和密钥距离?Thanx guyz。初始化成功了。因为我们身处面向对象编程的世界,定义一个简单的POJO来描述当前正在推送到地图中的数据不是更有意义吗?也就是说,创建一个包含相关字段、setter和getter的Place类不仅可以在语法和语义上改进代码,还可以使调试变得更加容易。只是一个想法。