gpt4 book ai didi

android - java.lang.ClassCastException : java. util.HashMap 无法转换为 Ride

转载 作者:行者123 更新时间:2023-11-29 14:45:42 25 4
gpt4 key购买 nike

DatabaseReference ridesRef = database.getReference("rides");
ridesRef.equalTo(from).orderByChild("from").addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
Log.i(TAG, "dataSnapshot.getChildrenCount(): " + dataSnapshot.getChildrenCount());
if (dataSnapshot.getChildrenCount() > 0) {
ArrayList<Ride> value = (ArrayList<Ride>) dataSnapshot.getValue();

Log.i(TAG, value.toString());

for (Ride r : value) { // error occurs here
Log.i(TAG, "r.getTime:" + r.getTime());
Log.i(TAG, "getFrom:" + r.getFrom());
}
}
}
});

日志输出为:

09-02 18:08:25.070 23651-23651 I/RidesFragment: dataSnapshot.getChildrenCount(): 1

09-02 18:08:25.070 23651-23651 I/RidesFragment: [{to=Hackerscher Markt, userID=0, time=1472831718565, regularly=false, price=0, chosenUserID=0, active=true, places=1, from=Hauptbahnhof Berlin, meetingPointDescription=blaues ei}]

public class Ride {
private String from;
private String to;
private long time;
private int places;
private String meetingPointDescription;
private boolean regularly;
private boolean active;
private float price;
private int userID;
private int chosenUserID;

public Ride() {
// Default constructor required for calls to DataSnapshot.getValue(Ride.class)
}

public Ride(String from, String to, long time, int places, String meetingPointDescription, boolean regularly,
boolean active, float price, int userID, int chosenUserID) {
this.from = from;
this.to = to;
this.time = time;
this.places = places;
this.meetingPointDescription = meetingPointDescription;
this.regularly = regularly;
this.active = active;
this.price = price;
this.userID = userID;
this.chosenUserID = chosenUserID;
}

// automatically generated getters and setters...
}

enter image description here

最佳答案

第三次我得到了我自己问题的答案:/

ArrayList<Ride> value = (ArrayList<Ride>) dataSnapshot.getValue();

必须替换为

GenericTypeIndicator<ArrayList<Ride>> t = new GenericTypeIndicator<ArrayList<Ride>>() {};
ArrayList<Ride> value = dataSnapshot.getValue(t);

一切都按预期进行。

关于android - java.lang.ClassCastException : java. util.HashMap 无法转换为 Ride,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39296767/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com