在我的应用程序解析json数据后,我希望借助Intent将数据从一个活动发送到另一个活动。但在我的第二个活动中,它只显示最后一个数组json数据。它没有在第二个活动中显示所有json解析数据。
第一项活动:
private class GetData extends AsyncTask<String, Void, JSONObject> {
@Override
protected void onPreExecute() {
super.onPreExecute();
progressDialog = ProgressDialog.show(SearchActivity.this, "", "");
}
@Override
protected JSONObject doInBackground(String... params) {
String response;
try {
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(params[0]);
List<NameValuePair> nameValuePair = new ArrayList<NameValuePair>(3);
nameValuePair.add(new BasicNameValuePair("FromCityid",fromcity_bus));
nameValuePair.add(new BasicNameValuePair("Tocityid",tocity_bus));
nameValuePair.add(new BasicNameValuePair("DOJ",journey_bus));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePair));
HttpResponse responce = httpclient.execute(httppost);
HttpEntity httpEntity = responce.getEntity();
response = EntityUtils.toString(httpEntity);
Log.d("response is", response);
return new JSONObject(response);
} catch (Exception ex) {
ex.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(JSONObject result)
{
super.onPostExecute(result);
// Log.v("TAG_RESULT",""+result);
progressDialog.dismiss();
if(result != null)
{
try
{
JSONObject jobj = result.getJSONObject("Response");
String message = jobj.getString("Message");
String issuceess = jobj.getString("IsSuccess");
Log.v("TAG_Message",""+message);
Log.v("TAG_Message",""+issuceess);
if(issuceess.equals("true"))
{
JSONArray routearray = result.getJSONArray("Route");
for(int i = 0; i<routearray.length(); i++)
{
companyid = routearray.getJSONObject(i).getString("CompanyId");
CompanyName = routearray.getJSONObject(i).getString("CompanyName");
deptime = routearray.getJSONObject(i).getString("DepTime");
routeScheduleId = routearray.getJSONObject(i).getString("RouteScheduleId");
arrtime =routearray.getJSONObject(i).getString("ArrTime");
fare =routearray.getJSONObject(i).getString("Fare");
hasac = routearray.getJSONObject(i).getString("HasAC");
hasnac = routearray.getJSONObject(i).getString("HasNAC");
hasseater = routearray.getJSONObject(i).getString("HasSeater");
hassleeper = routearray.getJSONObject(i).getString("HasSleeper");
isvolvo = routearray.getJSONObject(i).getString("IsVolvo");
buslabel = routearray.getJSONObject(i).getString( "BusLabel");
avaliableseats = routearray.getJSONObject(i).getString("AvailableSeats");
bustypename = routearray.getJSONObject(i).getString("BusTypeName");
Intent intent=new Intent(SearchActivity.this,FromtoActivity.class);
intent.putExtra("COMPANYNAME", CompanyName);
Log.v("TAG_COMPANYNAME",""+CompanyName);
intent.putExtra("COMPANYID", companyid);
intent.putExtra("BUSFARE", fare);
intent.putExtra("BUSLABEL", buslabel);
intent.putExtra("BUSTYPENAME", bustypename);
intent.putExtra("AVALIABLESEATS", avaliableseats);
// intent.putExtra("arrayListIdentifier",);
startActivity(intent);
}
}
}
catch (Exception e)
{
e.printStackTrace();
}
}
secondActivity:
Intent intent = getIntent();
String company_name = intent.getStringExtra("COMPANYNAME");
String company_id = intent.getStringExtra("COMPANYID");
String bus_fare = intent.getStringExtra("BUSFARE");
String bus_typename = intent.getStringExtra("BUSTYPENAME");
String bus_label = intent.getStringExtra("BUSLABEL");
String avaliable_seats = intent.getStringExtra("AVALIABLESEATS");
BusData bs = new BusData();
bs.setCompanyname(company_name);
bs.setCompanyid(company_id);
bs.setFare(bus_fare);
bs.setBuslabel(bus_label);
bs.setBustypename(bus_typename);
bs.setAvaliableseats(avaliable_seats);
bdata.add(bs);
BusDataAdapter adapter = new BusDataAdapter(this, bdata);
fromto.setAdapter(adapter);
fromto是一个列表视图。
bdata的大小为1。
答案 0 :(得分:1)
使用Bundle将值从当前活动传递到下一个活动
传递数据的当前活动
if(issuceess.equals("true"))
{
Intent intent=new Intent(SearchActivity.this,FromtoActivity.class);
intent.putExtra("json_objcet", result.toString());//result is a json object
startActivity(intent);
}
在接收数据的下一个活动中
Intent intent = getIntent();
String json_object = intent.getStringExtra("json_objcet");
try
{
JSONObject result = new JSONObject(json_object);
JSONArray routearray = result.getJSONArray("Route");
for (int i = 0; i < routearray.length(); i++) {
String companyid = routearray.getJSONObject(i).getString("CompanyId");
String CompanyName = routearray.getJSONObject(i).getString("CompanyName");
String deptime = routearray.getJSONObject(i).getString("DepTime");
String routeScheduleId = routearray.getJSONObject(i).getString("RouteScheduleId");
String arrtime = routearray.getJSONObject(i).getString("ArrTime");
String fare = routearray.getJSONObject(i).getString("Fare");
String hasac = routearray.getJSONObject(i).getString("HasAC");
String hasnac = routearray.getJSONObject(i).getString("HasNAC");
String hasseater = routearray.getJSONObject(i).getString("HasSeater");
String hassleeper = routearray.getJSONObject(i).getString("HasSleeper");
String isvolvo = routearray.getJSONObject(i).getString("IsVolvo");
String buslabel = routearray.getJSONObject(i).getString("BusLabel");
String avaliableseats = routearray.getJSONObject(i).getString("AvailableSeats");
String bustypename = routearray.getJSONObject(i).getString("BusTypeName");
BusData bs = new BusData();
bs.setCompanyname(CompanyName);
bs.setCompanyid(companyid);
bs.setFare(fare);
bs.setBuslabel(buslabel);
bs.setBustypename(bustypename);
bs.setAvaliableseats(avaliableseats);
bdata.add(bs);
}
BusDataAdapter adapter = new BusDataAdapter(this, bdata);
fromto.setAdapter(adapter);
}
catch (Exception e)
{
e.printStackTrace();
}
答案 1 :(得分:0)
试试这段代码 第一项活动
private class GetData extends AsyncTask<String, Void, JSONObject> {
ArrayList<BusData> arrayBusData = new ArrayList<BusData>();
@Override
protected void onPreExecute() {
super.onPreExecute();
progressDialog = ProgressDialog.show(SearchActivity.this, "", "");
}
@Override
protected JSONObject doInBackground(String... params) {
String response;
try {
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(params[0]);
List<NameValuePair> nameValuePair = new ArrayList<NameValuePair>(3);
nameValuePair.add(new BasicNameValuePair("FromCityid",fromcity_bus));
nameValuePair.add(new BasicNameValuePair("Tocityid",tocity_bus));
nameValuePair.add(new BasicNameValuePair("DOJ",journey_bus));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePair));
HttpResponse responce = httpclient.execute(httppost);
HttpEntity httpEntity = responce.getEntity();
response = EntityUtils.toString(httpEntity);
Log.d("response is", response);
return new JSONObject(response);
} catch (Exception ex) {
ex.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(JSONObject result)
{
super.onPostExecute(result);
// Log.v("TAG_RESULT",""+result);
progressDialog.dismiss();
if(result != null)
{
try
{
JSONObject jobj = result.getJSONObject("Response");
String message = jobj.getString("Message");
String issuceess = jobj.getString("IsSuccess");
Log.v("TAG_Message",""+message);
Log.v("TAG_Message",""+issuceess);
if(issuceess.equals("true"))
{
JSONArray routearray = result.getJSONArray("Route");
for(int i = 0; i<routearray.length(); i++)
{
companyid = routearray.getJSONObject(i).getString("CompanyId");
CompanyName = routearray.getJSONObject(i).getString("CompanyName");
deptime = routearray.getJSONObject(i).getString("DepTime");
routeScheduleId = routearray.getJSONObject(i).getString("RouteScheduleId");
arrtime =routearray.getJSONObject(i).getString("ArrTime");
fare =routearray.getJSONObject(i).getString("Fare");
hasac = routearray.getJSONObject(i).getString("HasAC");
hasnac = routearray.getJSONObject(i).getString("HasNAC");
hasseater = routearray.getJSONObject(i).getString("HasSeater");
hassleeper = routearray.getJSONObject(i).getString("HasSleeper");
isvolvo = routearray.getJSONObject(i).getString("IsVolvo");
buslabel = routearray.getJSONObject(i).getString( "BusLabel");
avaliableseats = routearray.getJSONObject(i).getString("AvailableSeats");
bustypename = routearray.getJSONObject(i).getString("BusTypeName");
BusData bData = new BusData(companyid,CompanyName,fare,buslabel,bustypename,avaliableseats,);
arrayBusData.add(bData);
}
Intent intent=new Intent(SearchActivity.this,FromtoActivity.class);
Bundle bundle = new Bundle();
bundle.putSerializable("bus_data", arrayBusData);
intent.putExtras(bundleObject);
startActivity(intent);
}
}
catch (Exception e)
{
e.printStackTrace();
}
}
第二项活动
ArrayList<BusData> arrayBusData = new ArrayList<BusData>();
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
Bundle bundleObject = getIntent().getExtras();
arrayBusData = (ArrayList<BusData>) bundleObject
.getSerializable("requestProductItem");
BusDataAdapter adapter = new BusDataAdapter(this, arrayBusData);
fromto.setAdapter(adapter);
}
BusData类还在BusData类中创建构造函数
public class BusData implements Serializable{
/**
*
*/
private static final long serialVersionUID = 1L;