我有一个自定义类的飞行和内部票价
public class Flight {
private String airlineCode;
private String airlineClass;
private String originCode;
private String destinationCode;
private String departureTime;
private String ArrivalTime;
public Flight (String originCode,String destinationCode,String departureTime,String arrivalTime,
String airlineCode, String airlineClass){
this.originCode=originCode;
this.destinationCode=destinationCode;
this.departureTime=departureTime;
this.ArrivalTime=arrivalTime;
this.airlineCode=airlineCode;
this.airlineClass=airlineClass;
}
public String getAirlineCode() {
return airlineCode;
}
public void setAirlineCode(String airlineCode) {
this.airlineCode = airlineCode;
}
public String getAirlineClass() {
return airlineClass;
}
public void setAirlineClass(String airlineClass) {
this.airlineClass = airlineClass;
}
public String getOriginCode() {
return originCode;
}
public void setOriginCode(String originCode) {
this.originCode = originCode;
}
public String getDestinationCode() {
return destinationCode;
}
public void setDestinationCode(String destinationCode) {
this.destinationCode = destinationCode;
}
public String getDepartureTime() {
return departureTime;
}
public void setDepartureTime(String departureTime) {
this.departureTime = departureTime;
}
public String getArrivalTime() {
return ArrivalTime;
}
public void setArrivalTime(String arrivalTime) {
ArrivalTime = arrivalTime;
}
//Inner Class
class Fare{
private String providerId;
private String fare;
public Fare(String providerId,String fare){
this.providerId=providerId;
this.fare=fare;
}
public String getProviderId() {
return providerId;
}
public String getFare() {
return fare;
}
}
}
我有一个示例JSON,我必须遍历并将Value放在该自定义类中。以下是JSON的简短示例。
{
"flights": [
{
"originCode": "DEL",
"destinationCode": "BOM",
"departureTime": 1396614600000,
"arrivalTime": 1396625400000,
"fares": [
{
"providerId": 1,
"fare": 11500
},
{
"providerId": 2,
"fare": 10500
}
],
"airlineCode": "G8",
"class": "Business"
},
{
"originCode": "DEL",
"destinationCode": "BOM",
"departureTime": 1396616400000,
"arrivalTime": 1396623600000,
"fares": [
{
"providerId": 1,
"fare": 14400
},
{
"providerId": 3,
"fare": 14000
}
],
"airlineCode": "AI",
"class": "Business"
}
]
}
我正在解析并创建像此一样的ArrayList
public static final ArrayList<Flight> extractdata(String jsonResponse) {
ArrayList<Flight> flights = new ArrayList<Flight>();
try {
JSONObject rootobject = new JSONObject(jsonResponse);
JSONArray flight = rootobject.getJSONArray("flights");
for (int i = 0; i < flight.length(); i++) {
JSONObject arrayObject = flight.getJSONObject(i);
String originCode = arrayObject.getString("originCode");
Log.d(LOG_TAG,"originalcode"+ originCode);
String destinationCode = arrayObject.getString("destinationCode");
Log.d(LOG_TAG,"destinationcode"+ destinationCode);
String departureTime = arrayObject.getString("departureTime");
Log.d(LOG_TAG,"departuretime"+ departureTime);
String arrivalTime = arrayObject.getString("arrivalTime");
Log.d(LOG_TAG,"arrivaltime"+ arrivalTime);
String airlineCode = arrayObject.getString("airlineCode");
Log.d(LOG_TAG,"airlinecode"+ airlineCode);
String airlineClass = arrayObject.getString("class");
Log.d(LOG_TAG,"class"+ airlineClass);
Long eTime = Long.parseLong(arrivalTime);
Date dateObject = new Date(eTime);
Long dTime = Long.parseLong(departureTime);
Date dateObject2 = new Date(dTime);
SimpleDateFormat dateFormat = new SimpleDateFormat("HH mm ");
String arrival = dateFormat.format(dateObject);
String depart = dateFormat.format(dateObject2);
JSONArray arrayFare=arrayObject.getJSONArray("fares");
String providerId;
String fare;
int number=arrayFare.length();
Flight flightObject=new Flight(originCode,destinationCode,depart,arrival,airlineCode,airlineClass);
for (int j=0;j<arrayFare.length();j++){
JSONObject arrayFareObject=arrayFare.getJSONObject(j);
providerId=arrayFareObject.getString("providerId");
fare=arrayFareObject.getString("fare");
Log.d(LOG_TAG,"providerId & fare :"+providerId+" "+fare);
Flight.Fare fareObject[]= new Flight.Fare[number];
fareObject[j]=flightObject.new Fare(providerId,fare);
}
flights.add(flightObject);
}
} catch (JSONException e) {
Log.e(LOG_TAG, "problem parsing jsone", e);
}
return flights;
}
现在我需要访问InnerClass对象,无论单击哪个Flight Object。我怎么做。我无法访问这些对象。
我是这样想的。
public void updateUi(final ArrayList<Flight> flights1)
{
final FlightAdapter flightAdapter=new FlightAdapter(this,flights1);
ListView flightlistview=(ListView)findViewById(R.id.list);
flightlistview.setAdapter(flightAdapter);
flightlistview.setOnItemClickListener(new AdapterView.OnItemClickListener()
{
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id)
{
Flight flight=flightAdapter.getItem(position);
Intent fareintent=new Intent(FlightSearchActivity.this,FareActivity.class);
startActivity(fareintent);
}
});
}
答案 0 :(得分:2)
这样做的一种方法是,因为每次航班都会有不同航空公司的票价。您可以将票价的ArrayList作为每个航班的属性。
List<Fare> fares
然后在阅读json响应时,您可以阅读并创建单个Fare类实例,并将这些实例添加到票价列表中。一旦您将单个航班的所有票价添加到列表中,请将此列表设置为Flight实例的票价属性。
List<Fare>fares=new ArrayList<Fare>();
//This code should be in a loop so you can add all the fares to the ArrayList
Fare f=new Fare();
f.setProviderId("AI");
f.setFare("12000");
fares.add(f);
现在将票价列表添加到Flight实例
flight.setFares(fares)
希望这有帮助。
答案 1 :(得分:1)
您需要为您的航班提供票价对象。
如果这样做,只需创建一个Fare类型的对象,不与特定的Flight Object连接。
Flight.Fare fareObject[]= new Flight.Fare[number];
fareObject[j]=flightObject.new Fare(providerId,fare);
相反,在Fligt类中声明Fare对象和其他变量:
...
private String destinationCode;
private String departureTime;
private String ArrivalTime;
Fare fare;
编辑:为航班的多个票价添加票价数组
..
ArrayList<Fare> fares;
答案 2 :(得分:1)
将您的Flight
课程修改为:
public class Flight {
private String airlineCode;
private String airlineClass;
private String originCode;
private String destinationCode;
private String departureTime;
private String ArrivalTime;
private List<Fare> fares;
public Flight (String originCode,String destinationCode,String departureTime,String arrivalTime, String airlineCode, String airlineClass, List<Fare> fares){
this.originCode=originCode;
this.destinationCode=destinationCode;
this.departureTime=departureTime;
this.ArrivalTime=arrivalTime;
this.airlineCode=airlineCode;
this.airlineClass=airlineClass;
this.fares = fares;
}
... //Other getters and setters
public List<Fare> getFares() {
return fares;
}
public void setFares(List<Fare> fares) {
this.fares = fares;
}
... //Inner class code
}
现在使用extractdata
方法,
public static final ArrayList<Flight> extractdata(String jsonResponse) {
ArrayList<Flight> flights = new ArrayList<Flight>();
try {
JSONObject rootobject = new JSONObject(jsonResponse);
JSONArray flight = rootobject.getJSONArray("flights");
for (int i = 0; i < flight.length(); i++) {
JSONObject arrayObject = flight.getJSONObject(i);
String originCode = arrayObject.getString("originCode");
String destinationCode = arrayObject.getString("destinationCode");
String departureTime = arrayObject.getString("departureTime");
String arrivalTime = arrayObject.getString("arrivalTime");
String airlineCode = arrayObject.getString("airlineCode");
String airlineClass = arrayObject.getString("class");
Long eTime = Long.parseLong(arrivalTime);
Date dateObject = new Date(eTime);
Long dTime = Long.parseLong(departureTime);
Date dateObject2 = new Date(dTime);
SimpleDateFormat dateFormat = new SimpleDateFormat("HH mm ");
String arrival = dateFormat.format(dateObject);
String depart = dateFormat.format(dateObject2);
JSONArray arrayFare = arrayObject.getJSONArray("fares");
List<Fare>fares=new ArrayList<Fare>();
for (int j=0;j<arrayFare.length();j++){
JSONObject arrayFareObject = arrayFare.getJSONObject(j);
String providerId = arrayFareObject.getString("providerId");
String fare = arrayFareObject.getString("fare");
Flight.Fare fareObject = new Flight.Fare(providerId, fare);
fares.add(fareObject);
}
Flight flightObject = new Flight(originCode, destinationCode, depart, arrival, airlineCode, airlineClass, fares);
flights.add(flightObject);
}
} catch (JSONException e) {
Log.e(LOG_TAG, "problem parsing jsone", e);
}
return flights;
}
现在,在updateUi
功能中,您可以通过调用fares
方法获取getFares
:
public void updateUi(final ArrayList<Flight> flights1) {
final FlightAdapter flightAdapter = new FlightAdapter(this,flights1);
ListView flightlistview = (ListView)findViewById(R.id.list);
flightlistview.setAdapter(flightAdapter);
flightlistview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Flight flight = flightAdapter.getItem(position);
List<Fare> fares = flight.getFares(); //This will have all the fares.
Intent fareintent = new Intent(FlightSearchActivity.this,FareActivity.class);
startActivity(fareintent);
}
});
}
警告:我没有测试过这段代码,但它应该与这些代码类似。