0
votes

I want to read all the elements from my Firebase realtime database (which I fill by import a .json file (not filled using my app). So I have tried a lot of other solutions but they didn't work. Here is my real time database:

Real time database structure

Here is how i read the data in main activity:

 public  final  ArrayList<Parking> parkingArrayList=new ArrayList<Parking>();

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    FirebaseDatabase firebaseDatabase=FirebaseDatabase.getInstance();
    firebaseDatabase.setPersistenceEnabled(true);
    DatabaseReference databaseReference=firebaseDatabase.getReference();
    databaseReference.child("Parking").addListenerForSingleValueEvent(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
           for(DataSnapshot dataSnapshot1:dataSnapshot.getChildren()){
            Parking p=dataSnapshot1.getValue(Parking.class);
           }
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    });

Here is my Parking class:

public class  Parking implements  Comparable<Parking>,Parcelable {
public  String name;
public  String zone;
public String location;
public int capacity;
public double latitude;
public double longtitude;
int free=0;
int taken=0;
int id;
public  Parking(){
}
public Parking(int capacity,double latitude,String location, double longtitude,String name,String zone){
    this.capacity=capacity;
    this.location=location;
    this.name=name;
    this.zone=zone;
    this.latitude=latitude;
    this.longtitude=longtitude;
    this.taken=getTaken();
    this.free=getFree();
}

protected Parking(Parcel in) {
    name = in.readString();
    zone = in.readString();
    location = in.readString();
    capacity = in.readInt();
    latitude = in.readDouble();
    longtitude = in.readDouble();
    free = in.readInt();
    taken = in.readInt();
}

public static final Creator<Parking> CREATOR = new Creator<Parking>() {
    @Override
    public Parking createFromParcel(Parcel in) {
        return new Parking(in);
    }

    @Override
    public Parking[] newArray(int size) {
        return new Parking[size];
    }
};

public int getCapacity() {
    return capacity;
}

public int getFree() {
    return capacity-getTaken();
}

public int getTaken() {
    Random random=new Random();
    this.taken=random.nextInt(capacity+1);
    return  taken;
}

public  void  setFree(int taken){
    free=capacity-taken;
}
public void setTaken(int taken) {
    Random random=new Random();
    this.taken=random.nextInt(capacity+1);
}
public  double getLatitude(){
    return  latitude;
}
public  double getLongtidue(){
    return  longtitude;
}


@Override
public String toString() {
    return String.format("%s\t\tZone: %s\nlocation: %s\nCapacity: %d\t\tFree: %d",name,zone,location,capacity,getFree());
}


@Override
public int compareTo(@NonNull Parking parking) {
    return Integer.compare(parking.free,this.free);
}

@Override
public int describeContents() {
    return 0;
}

@Override
public void writeToParcel(Parcel parcel, int i) {
    parcel.writeInt(capacity);
    parcel.writeDouble(latitude);
    parcel.writeString(location);
    parcel.writeDouble(longtitude);
    parcel.writeString(name);
    parcel.writeString(zone);
   parcel.writeInt(getTaken());
    parcel.writeInt(getFree());
}

}

I have connected it with Firebase, install the google-services.json file (by using the firebase tool) but when i start the app it crashes("Unfortunately,najdiparking (my app name) has stopped) . When i fill the list manually (without reading from database) the application works. This is the dependencies 'com.google.firebase:firebase-database:10.0.1.

I also check my .json file names with the names in the class.

my logcat (i hope this will help you)

08-27 15:41:31.884 19384-19384/com.example.simeon.najdiparking E/AndroidRuntime: FATAL EXCEPTION: main Process: com.example.simeon.najdiparking, PID: 19384 java.lang.NoSuchMethodError: No virtual method zzTt()Z in class Lcom/google/firebase/FirebaseApp; or its super classes (declaration of 'com.google.firebase.FirebaseApp' appears in /data/app/com.example.simeon.najdiparking-1/split_lib_dependencies_apk.apk:classes20.dex) at com.google.firebase.database.FirebaseDatabase.getInstance(Unknown Source) at com.google.firebase.database.FirebaseDatabase.getInstance(Unknown Source) at com.example.simeon.najdiparking.MainActivity.onCreate(MainActivity.java:48) at android.app.Activity.performCreate(Activity.java:6237) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476) at android.app.ActivityThread.-wrap11(ActivityThread.java) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:148) at android.app.ActivityThread.main(ActivityThread.java:5417) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)

1
you can provide logs for better understanding of your issue - Sergey
post logcat error. - Bruno Ferreira
Looks like you copied the wrong section of the logcat. - OneCricketeer
i think that this will help more - Simeon iloski
thanks guys. The problem was haven't installed the latest Google Repository and the latest Firebase library com.google.firebase:firebase-database:11.2.0 - Simeon iloski

1 Answers

0
votes

You have to read from parcel in thesame order you wrote to it.