在我的 android 项目中,有一个列表显示来自 Google Places API 的一些列表,Google API 将评分作为字符串值返回我想将其添加到简单适配器以在列表中显示评分栏请帮我解决这个问题问题....
import java.util.ArrayList;
import java.util.HashMap;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.app.ListActivity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
public class MyListActivity extends ListActivity {
// JSON Node names
private static final String TAG_CONTACTS = "results";
private static final String TAG_ID = "id";
private static final String TAG_NAME = "name";
private static final String TAG_GENDER = "rating";
// private static final String TAG_ADDRESS = "formatted_address";
private static final String TAG_REFERENCE = "reference";
private static final String TAG_ICON = "icon";
// contacts JSONArray
JSONArray result = null;
String url;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// url to make request
Intent in = getIntent();
String location = in.getStringExtra("TAG_LOCATION");
String type = in.getStringExtra("TAG_TYPE");
url = "https://maps.googleapis.com/maps/api/place/textsearch/json?query="+type+"+in+"+location+"&sensor=true&key=AIzaSyD38pak_katUfSR92WE2_O2b9y0JSp5htA";
//}
// LoadData ld = new LoadData();
// ld.onPreExecute();
new LoadData().execute();
}
class LoadData extends AsyncTask<String, String, String>
{
// ProgressDialog pDialog;
// protected void onPreExecute() {
//
//pDialog = new ProgressDialog(MyListActivity.this);
//pDialog.setMessage("Populating list please wait...");
// pDialog.setIndeterminate(false);
// pDialog.setCancelable(false);
// pDialog.show();
//}
// Hashmap for ListView
ArrayList<HashMap<String, String>> contactList = new ArrayList<HashMap<String, String>>();
protected String doInBackground(String... args) {
// Creating JSON Parser instance
JSONParser jParser = new JSONParser();
// getting JSON string from URL
JSONObject json = jParser.getJSONFromUrl(url);
try {
// Getting Array of Contacts
result = json.getJSONArray(TAG_CONTACTS);
// looping through All Contacts
for(int i = 0; i < result.length(); i++){
JSONObject c = result.getJSONObject(i);
// Storing each json item in variable
String id = c.getString(TAG_ID);
String name = c.getString(TAG_NAME);
// String email = c.getString(TAG_EMAIL);
// String icon = c.getString(TAG_ICON);
String rating = c.getString(TAG_GENDER);
// Phone number is agin JSON Object
// JSONObject phone = c.getJSONObject(TAG_PHONE);
// String mobile = phone.getString(TAG_PHONE_MOBILE);
// String home = phone.getString(TAG_PHONE_HOME);
// String office = phone.getString(TAG_PHONE_OFFICE);
// creating new HashMap
HashMap<String, String> map = new HashMap<String, String>();
// adding each child node to HashMap key => value
map.put(TAG_ID, id);
map.put(TAG_NAME, name);
// map.put(TAG_ICON, icon);
map.put(TAG_GENDER, rating);
// map.put(TAG_EMAIL, email);
// map.put(TAG_PHONE_MOBILE, mobile);
// adding HashList to ArrayList
contactList.add(map);
}
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
protected void onPostExecute(String file_url) {
// this.pDialog.cancel();
// dismiss the dialog after getting all products
/**
* Updating parsed JSON data into ListView
* */
ListAdapter adapter = new SimpleAdapter(MyListActivity.this, contactList,
R.layout.listview_item_row,
new String[] { TAG_NAME, TAG_GENDER,}, new int[] {
R.id.txtTitle, R.id.ratingbar });
setListAdapter(adapter);
// selecting single ListView item
ListView lv = getListView();
// Launching new screen on Selecting Single ListItem
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// getting values from selected ListItem
// String name = ((TextView) view.findViewById(R.id.name)).getText().toString();
// String cost = ((TextView) view.findViewById(R.id.formatted_address)).getText().toString();
// String description = ((TextView) view.findViewById(R.id.mobile)).getText().toString();
// Starting new intent
Intent in = new Intent(getApplicationContext(), ProfileViewActivity.class);
// in.putExtra(TAG_NAME, name);
// in.putExtra(TAG_EMAIL, cost);
// in.putExtra(TAG_PHONE_MOBILE, description);
startActivity(in);
}
});
}
}
}