gpt4 book ai didi

java - 在Android中使用Gson读取自定义类的ArrayList

转载 作者:太空宇宙 更新时间:2023-11-04 14:42:14 27 4
gpt4 key购买 nike

尝试从文件加载用户行程列表以携带用户的运行总计。

这是我从中读取它的对象,以及 UserTrip 的类文件

         public static ArrayList<UserTrip> tripList = new ArrayList<UserTrip>();

StringBuilder stringBuilder = null;
try {
FileInputStream inputStream = openFileInput("ridefile.txt");
InputStreamReader isr = new InputStreamReader(inputStream);
BufferedReader bufferedReader = new BufferedReader(isr);
stringBuilder = new StringBuilder();
String line;
while ((line = bufferedReader.readLine()) != null) {
stringBuilder.append(line);
}

} catch (IOException e) {
e.printStackTrace();
}
String json = stringBuilder.toString();
Gson gson = new Gson();

MapsActivity.tripList = gson.fromJson(json,ArrayList.class);

公共(public)类UserTrip { 私有(private)日历pickupDate;//保存下车的日期和时间 私有(private)日历 dropoffDate;//保存下车日期和时间

private Location pickupLocation;
private Location dropoffLocation;

private LatLng pickupLatLng;
private LatLng dropoffLatLng;


private String pickupAddress; //holds readable pick address
private String dropoffAddress; //holds readable drop address

private double tripMileage; //holds trip mileage
private double traveltoMileage; //holds distance traveled empty to pickup



//track time and tripMileage to next trip


public UserTrip(){ //Constructor for individual user trips

}

public void setPickupDate(Calendar n){

pickupDate = n;
}

public void setDropoffDate(Calendar n){

dropoffDate = n;
}

public void setPickupLocation(Location n) {
pickupLocation = n;
setPickupLatLng(new LatLng(n.getLatitude(),n.getLongitude()));
}

public void setDropoffLocation(Location n) {
dropoffLocation = n;
setDropoffLatLng(new LatLng(n.getLatitude(),n.getLongitude()));
}

public void setPickupAddress(String n){
pickupAddress = n;
}

public void setDropoffAddress(String n){
dropoffAddress = n;
}

public void setTripMileage(double n){
tripMileage = n;
}

public void setTraveltoMileage(double n) {
traveltoMileage = n;
}

public void setPickupLatLng(LatLng pickupLatLng) {
this.pickupLatLng = pickupLatLng;
}

public void setDropoffLatLng(LatLng dropoffLatLng) {
this.dropoffLatLng = dropoffLatLng;
}

public Calendar getDropoffDate(){
return(dropoffDate);
}

public Calendar getPickupDate(){
return (pickupDate);
}

public String getPickupAddress(){
return pickupAddress;
}

public String getDropoffAddress(){
return dropoffAddress;
}

public double getTripMileage(){
return tripMileage;
}

public Location getPickupLocation() {
return pickupLocation;
}

public Location getDropoffLocation() {
return dropoffLocation;
}

public double getTraveltoMileage() {
return traveltoMileage;
}

public LatLng getDropoffLatLng() {
return dropoffLatLng;
}

public LatLng getPickupLatLng() {
return pickupLatLng;
}

}

最佳答案

我相信你想做的就是详细的here之类的事情

基本上,改变你的线路

MapsActivity.tripList = gson.fromJson(json,ArrayList.class);

MapsActivity.tripList = gson.fromJson(json,new TypeToken<ArrayList<UserTrip>>(){}.getType());

关于java - 在Android中使用Gson读取自定义类的ArrayList,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24815194/

27 4 0