我已经实现了hashmap,其中我已经将接收到的json对象分配给某些字符串,如rvalue,price,dsvalue,并将这些字符串作为值传递给KEY_EXTRA,KEY_PLEVEL,KEY_DISTANCE。然后我传递并将此hashmap添加到arraylist .I已为listview的每个项目定义了cutom视图,我想在其中设置这些值,如rating,distance。当我使用这些数据来设置textview的文本时,它只使用KEY_DISTANCE的值作为text的所有其他textview。价值,价格,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>