2016-09-02 4 views
0
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()); 
      } 
     } 
    } 
}); 

ログ出力に乗るためにキャストできないです:とjava.lang.ClassCastException:java.util.HashMapのは

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

9月2日18:08:25.070 23651から23651 I/RidesFragment:[{= Hackerscher マルクト、ユーザID = 0、時刻= 1472831718565に、定期的に偽、価格= = 0、 chosenUserID = 0、アクティブ=真、場所= 1、from = Hauptbahnhof Berlin、 meetingPointDescription = blaues/

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

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

と、すべてで交換する必要があります: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

答えて

5

3回目は、私は自分の質問への答えを得ました期待どおりに動作します。

+1

Phew。私は 'GenericTypeIndicator'を使って良い例を見つけられないので、あなたが見つけたことをうれしく思います。彼らは存在しますが、見つけにくいです(私にとっては少なくとも)。 –

関連する問題