我在Android中创建了一个listview,但现在我试图让listview在传递参数的同时可以点击一个新的活动。 listview的数据来自api。代码如下。点击进入ListView的新活动
private static String urlString;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_market_place);
urlString = "http://websiteforapi/api/market";
new ProcessJSON().execute(urlString);
}
private class ProcessJSON extends AsyncTask<String, Void, String> {
protected String doInBackground(String...strings){
String stream = null;
String urlString = strings[0];
HTTPDataHandler hh = new HTTPDataHandler();
stream = hh.GetHTTPData(urlString);
// Return the data from specified url
return stream;
}
protected void onPostExecute(String stream){
//..........Process JSON DATA................
if(stream !=null){
try {
// Get the full HTTP Data as JSONObject
JSONObject reader = new JSONObject(stream);
JSONArray businessArray = reader.getJSONArray("data");
ArrayList<String> businesses = new ArrayList<String>();
for(int i = 0; i < businessArray.length(); i++) {
JSONObject business_object_0 = businessArray.getJSONObject(i);
final String businessname = business_object_0.getString("name");
final String business = business_object_0.getString("business");
businesses.add(businessname);
}
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getListView().getContext(),android.R.layout.simple_list_item_1, businesses);
getListView().setAdapter(adapter);
}catch(JSONException e){
e.printStackTrace();
}
} // if statement end
} // onPostExecute() end
} // ProcessJSON class end
我从API使用JSON是这样的......
{"data":[{"id":"3","name":"My Test Business 3","phone":"6785555555","email":"[email protected]","address":"123 This Avenue","city":"Atlanta","state":"GA","zip":"30308","business":"Technology 33333"},{"id":"4","name":"My Test Business 2 ","phone":"6783571004","email":"[email protected]","address":"123 Some Street","city":"Lawrenceville","state":"GA","zip":"30043","business":"Website building"},{"id":"5","name":"My Test Business 3","phone":"6785555555","email":"[email protected]","address":"123 This Avenue","city":"Atlanta","state":"GA","zip":"30308","business":"Technology 3"},{"id":"6","name":"My Test Business 5","phone":"6785555555","email":"[email protected]","address":"123 This Avenue","city":"Atlanta","state":"GA","zip":"30308","business":"Technology 4"}]}
到目前为止,我可以得到ListView控件来显示,但到目前为止,我无法让他们点击到一个新的活动。
不确定你的意思是主细节。但代码工作。有没有办法将选定的数组列表项目的参数传递给下一个活动?我尝试使用以下内容。 Intent intent = new Intent(MarketPlaceActivity.this,SelectedBusinessActivity.class); intent.putExtra(“name”,business); startActivity(intent); – teej2542
看到这个传递数据[链接](http://stackoverflow.com/questions/3510649/how-to-pass-a-value-from-one-activity-to-another-in-android) – Esty