I have Firebase App with Realtime Database, I have db.json as
我有实时数据库的Firebase应用程序,我有db.json
{
"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
}
]
}
]
}
}
]
}
}
Now in android App I want to retrieve the routes in a list as
现在在Android应用程序中我想要检索列表中的路由为
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();
}
}
});
through this code I'm getting
通过这个代码,我得到了
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
are there any parts which I'm missing here to read routes list.
有没有我在这里缺少阅读路线列表的部分。
1 个解决方案
#1
5
instead of this:
而不是这个:
GenericTypeIndicator t = new GenericTypeIndicator () {};
List<Object> routes = (List<Object>) dataSnapshot.getValue(t);
use this
用这个
GenericTypeIndicator<List<Object>> t = new GenericTypeIndicator <List<Object>>() {};
List<Object> routes = dataSnapshot.getValue(t);
#1
5
instead of this:
而不是这个:
GenericTypeIndicator t = new GenericTypeIndicator () {};
List<Object> routes = (List<Object>) dataSnapshot.getValue(t);
use this
用这个
GenericTypeIndicator<List<Object>> t = new GenericTypeIndicator <List<Object>>() {};
List<Object> routes = dataSnapshot.getValue(t);