Android 通过活动解析JSON对象
我有一个活动,它当前显示了一个来自GoogleAPI的条列表,工作正常,我正在尝试获取它,以便用onclick解析“名称”和其他细节。目前,它打开了一个新的活动,但我无法获取用putExtra发送的解析JSON。 谢谢你的帮助 javaAndroid 通过活动解析JSON对象,android,json,Android,Json,我有一个活动,它当前显示了一个来自GoogleAPI的条列表,工作正常,我正在尝试获取它,以便用onclick解析“名称”和其他细节。目前,它打开了一个新的活动,但我无法获取用putExtra发送的解析JSON。 谢谢你的帮助 java public class Bars extends AppCompatActivity { private String TAG = Bars.class.getSimpleName(); private ProgressDialog pDi
public class Bars extends AppCompatActivity {
private String TAG = Bars.class.getSimpleName();
private ProgressDialog pDialog;
ListView lv;
// URL to get Results JSON
private static String url = "https://maps.googleapis.com/maps/api/place/nearbysearch/json?location=40.785091,-73.968285&radius=1000&type=restaurant,bar,food,point_of_interest&keyword=bar&opennow=true&key=MYAPICODE";
ArrayList<HashMap<String, String>> resultsList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_bars);
resultsList = new ArrayList<>();
lv = (ListView) findViewById(R.id.list);
new Getresults().execute();
}
//Async task class to get json by making HTTP call
private class Getresults extends AsyncTask<Void, Void, Void> {
@Override
protected void onPreExecute() {
super.onPreExecute();
// Showing progress dialog
pDialog = new ProgressDialog(Bars.this);
pDialog.setMessage("Please wait...");
pDialog.setCancelable(false);
pDialog.show();
}
@Override
protected Void doInBackground(Void... arg0) {
HttpConnect sh = new HttpConnect();
// Making a request to url and getting response
String jsonStr = sh.makeServiceCall(url);
Log.e(TAG, "Response from url: " + jsonStr);
if (jsonStr != null) {
try {
JSONObject jsonObj = new JSONObject(jsonStr);
// Getting JSON Array node
JSONArray results = jsonObj.getJSONArray("results");
// looping through All results
for (int i = 0; i < results.length(); i++) {
JSONObject c = results.getJSONObject(i);
String name = c.getString("name");
String vicinity = c.getString("vicinity");
String rating = c.getString("rating");
// tmp hash map for single resu
HashMap<String, String> result = new HashMap<>();
// adding each child node to HashMap key => value
result.put("name", name);
result.put("vicinity", vicinity);
result.put("rating", rating) ;
// adding result to result list
resultsList.add(result);
}
} catch (final JSONException e) {
Log.e(TAG, "Json parsing error: " + e.getMessage());
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(getApplicationContext(),
"Json parsing error: " + e.getMessage(),
Toast.LENGTH_LONG)
.show();
}
});
}
} else {
Log.e(TAG, "Couldn't get json from server.");
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(getApplicationContext(),
"Couldn't get json from server. Check LogCat for possible errors!",
Toast.LENGTH_LONG)
.show();
}
});
}
return null;
}
@Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
// Dismiss the progress dialog
if (pDialog.isShowing())
pDialog.dismiss();
/**
* Updating parsed JSON data into ListView
* */
ListAdapter adapter = new SimpleAdapter(
Bars.this, resultsList,
R.layout.list_item, new String[]{"name", "vicinity",
"rating" }, new int[]{name,
R.id.vicinity, R.id.rating});
lv.setAdapter(adapter);
// on lick listener to send name position
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
Intent newActivity = new Intent(Bars.this,
SecondActivity.class);
newActivity.putExtra("position", position);
newActivity.putExtra("name", name);
startActivity(newActivity);
}
});
}
}
}
我看不出你在哪里初始化了名字。但是我可以在您的
onPostexecute
块中看到您编写了此代码
ListAdapter adapter = new SimpleAdapter(
Bars.this, resultsList,
R.layout.list_item, new String[]{"name", "vicinity",
"rating" }, new int[]{name,
R.id.vicinity, R.id.rating});
在上面的代码中,您提到了name
作为int
。看这一行
int[]{name,R.id.vicinity, R.id.rating});
因此,在第二个活动中调用
intent.getStringExtra('name')
无法获取名称的值。您需要从adapterView访问适配器,然后按位置获取hashMap并拉取该项
@Override public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
ListAdapter adapter = (ListAdapter) adapterView.getAdapter()
HashMap<String, String> map = (HashMap) adapter.getItem(position)
String name = map.get("name") // may be a null
Intent newActivity = new Intent(Bars.this, SecondActivity.class);
newActivity.putExtra("position", position);
newActivity.putExtra("name", name);
startActivity(newActivity);
}
@Override public void onItemClick(AdapterView AdapterView,视图视图,整型位置,长id){
ListAdapter=(ListAdapter)adapterView.getAdapter()
HashMap=(HashMap)适配器.getItem(位置)
String name=map.get(“name”)//可能为空
Intent newActivity=newintent(bar.this,SecondActivity.class);
newActivity.putExtra(“位置”,位置);
newActivity.putExtra(“名称”,名称);
星触觉(新活动);
}
好的,那么你知道我如何在第二次活动中获得它吗?或者,如果有一种方式提到它不是intGet,则通过resultList.get(selectedPosition)获取hashmap。然后将其发送到第二个活动谢谢您将尝试
@Override public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
ListAdapter adapter = (ListAdapter) adapterView.getAdapter()
HashMap<String, String> map = (HashMap) adapter.getItem(position)
String name = map.get("name") // may be a null
Intent newActivity = new Intent(Bars.this, SecondActivity.class);
newActivity.putExtra("position", position);
newActivity.putExtra("name", name);
startActivity(newActivity);
}