2016-07-20 1 views
2

Je Firebase App avec la base de données en temps réel, j'ai db.json commePas une sous-classe directe de GenericTypeIndicator: classe com.google.firebase.database.GenericTypeIndicator

{ 
    "brs" : { 
    "route": [ 
     { 
     "routeDestination": "DDDD1", 
     "routeOrigin": "OOOO1", 
     "bus" : { 
      "busArrivalTime" : "created_at_timestamp", 
      "busDepartureTime" : "created_at_timestamp", 
      "busName" : "SOME NAME", 
      "busSeatCost" : "0000", 
      "busTotalSeats" : "000", 
      "reservations": [ 
       { 
       "reservationId": 1, 
       "reservationDate": "Wed Jul 06 23:54:56 EDT 2016", 
       "seats": [ 
        {     
        "seatNumber": 1 
        }, 
        {    
        "seatNumber": 2 
        } 
       ] 
       } 
      ] 
     }    
     }, 
     { 
     "routeDestination": "DDDD2", 
     "routeOrigin": "OOOO2", 
     "bus" : { 
      "busArrivalTime" : "created_at_timestamp", 
      "busDepartureTime" : "created_at_timestamp", 
      "busName" : "SOME NAME", 
      "busSeatCost" : "0000", 
      "busTotalSeats" : "000", 
      "reservations": [ 
       { 
       "reservationId": 1, 
       "reservationDate": "Wed Jul 06 23:54:56 EDT 2016", 
       "seats": [ 
        {     
        "seatNumber": 1 
        }, 
        {    
        "seatNumber": 2 
        } 
       ] 
       } 
      ] 
     }    
     }, 
     { 
     "routeDestination": "DDDD3", 
     "routeOrigin": "OOOO3", 
     "bus" : { 
      "busArrivalTime" : "created_at_timestamp", 
      "busDepartureTime" : "created_at_timestamp", 
      "busName" : "SOME NAME", 
      "busSeatCost" : "0000", 
      "busTotalSeats" : "000", 
      "reservations": [ 
       { 
       "reservationId": 1, 
       "reservationDate": "Wed Jul 06 23:54:56 EDT 2016", 
       "seats": [ 
        {     
        "seatNumber": 1 
        }, 
        {    
        "seatNumber": 2 
        } 
       ] 
       } 
      ] 
     }    
     } 
    ] 
    } 
} 

maintenant dans l'App Android Je veux récupérer la routes dans une liste comme

button.setOnClickListener(new View.OnClickListener() { 
    @Override 
    public void onClick(final View view) { 
     FirebaseDatabase firebaseDatabase = FirebaseDatabase.getInstance(); 
     DatabaseReference myRef = firebaseDatabase.getReference("route"); 

     try { 

      // Read from the database 
      myRef.addValueEventListener(new ValueEventListener() { 
       @Override 
       public void onDataChange(DataSnapshot dataSnapshot) { 
        // This method is called once with the initial value and again 
        // whenever data at this location is updated. 
        GenericTypeIndicator t = new GenericTypeIndicator() {}; 
        List<Object> routes = (List<Object>) dataSnapshot.getValue(t); 

        if(routes == null) { 
         Snackbar.make(view, "No value " ,Snackbar.LENGTH_LONG).setAction("Action", null).show(); 
        } 
        else { 
         Snackbar.make(view, "The routes Size is " + routes.size(), Snackbar.LENGTH_LONG) 
           .setAction("Action", null).show(); 

        }       
       } 

       @Override 
       public void onCancelled(DatabaseError error) { 
        // Failed to read value 
        Log.w("BUS_TAG", "Failed to read value.", error.toException()); 
       } 
      }); 
     } catch (Exception e) { 
      e.printStackTrace(); 
     }  

    } 
}); 

à travers ce code, je suis en train de

E/AndroidRuntime: FATAL EXCEPTION: main 
    Process: com.rhcloud.escot.bsr, PID: 1750 
    com.google.firebase.database.DatabaseException: Not a direct subclass of GenericTypeIndicator: class com.google.firebase.database.GenericTypeIndicator 
     at com.google.android.gms.internal.zzalq.zza(Unknown Source) 
     at com.google.firebase.database.DataSnapshot.getValue(Unknown Source) 
     at com.rhcloud.escot.bsr.MainActivity$2$1.onDataChange(MainActivity.java:61) 
     at com.google.android.gms.internal.zzaih.zza(Unknown Source) 
     at com.google.android.gms.internal.zzajh.zzctc(Unknown Source) 
     at com.google.android.gms.internal.zzajk$1.run(Unknown Source) 
     at android.os.Handler.handleCallback(Handler.java:815) 
     at android.os.Handler.dispatchMessage(Handler.java:104) 
     at android.os.Looper.loop(Looper.java:194) 
     at android.app.ActivityThread.main(ActivityThread.java:5631) 
     at java.lang.reflect.Method.invoke(Native Method) 
     at java.lang.reflect.Method.invoke(Method.java:372) 
     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:959) 
     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:754) 
07-20 09:07:05.756 757-1407/? W/ActivityManager: Force finishing activity 1 com.rhcloud.escot.bsr/.MainActivity 

il des éléments ce qui me manque ici pour lire la liste des routes.

+0

Quelle est la version des services Google Play utilisez-vous? –

+0

Pas n'importe! Est-il nécessaire d'ajouter que 'compile 'com.google.android.gms: play-services: 9.2.1' ' –

Répondre

3

au lieu de ceci:

GenericTypeIndicator t = new GenericTypeIndicator() {}; 
List<Object> routes = (List<Object>) dataSnapshot.getValue(t); 

utilisent ce

GenericTypeIndicator<List<Object>> t = new GenericTypeIndicator <List<Object>>() {}; 
List<Object> routes = dataSnapshot.getValue(t);