Reputation: 33
I have an app that saves in firebase current location (city name etc), id, and a list with points so i can save a polyline. Import in firebase is succesfull. The problem is when i go to another activity that contains a listview with all the city names of the firebase the app crashes. I tried to make it without the list in firebase and works fine. So the problem exists only when firebase contains the list with the points.
This is an example of my firebase. Here, "ola' is the list that contains the points and i have it only in the last one. If i remove the last one, the app works fine.
import java.util.List;
public class Paths {
String id;
String name;
String city;
String nomos;
String perifereia;
List<Ola> ola;
public Paths(){
}
public Paths(String id, String name, String city, String nomos, String perifereia, List<Ola> ola) {
this.id = id;
this.name = name;
this.city = city;
this.nomos = nomos;
this.perifereia = perifereia;
this.ola = ola;
}
public String getId() {
return id;
}
public String getName() {
return name;
}
public String getCity() {
return city;
}
public String getNomos() {
return nomos;
}
public String getPerifereia() {
return perifereia;
}
public List<Ola> getOla() {
return ola;
}
.
public class Ola {
public double latitude;
public double longitude;
public Ola() {}
public Ola(double latitude, double longitude) {
this.latitude = latitude;
this.longitude = longitude;
}
public double getLatitude() {
return latitude;
}
public void setLatitude(double latitude) {
this.latitude = latitude;
}
public double getLongitude() {
return longitude;
}
public void setLongitude(double longitude) {
this.longitude = longitude;
}
}
And here is the error:
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.spiros.mypath, PID: 28814
com.google.firebase.database.DatabaseException: Can't convert object of type java.util.ArrayList to type com.example.spiros.mypath.Ola
at com.google.android.gms.internal.firebase_database.zzkt.zzb(Unknown Source)
at com.google.android.gms.internal.firebase_database.zzkt.zza(Unknown Source)
at com.google.android.gms.internal.firebase_database.zzkt.zza(Unknown Source)
at com.google.android.gms.internal.firebase_database.zzkt.zzb(Unknown Source)
at com.google.android.gms.internal.firebase_database.zzku.zza(Unknown Source)
at com.google.android.gms.internal.firebase_database.zzkt.zzb(Unknown Source)
at com.google.android.gms.internal.firebase_database.zzkt.zza(Unknown Source)
at com.google.firebase.database.DataSnapshot.getValue(Unknown Source)
at com.example.spiros.mypath.CitiesList$1.onDataChange(CitiesList.java:44)
at com.google.android.gms.internal.firebase_database.zzfc.zza(Unknown Source)
at com.google.android.gms.internal.firebase_database.zzgx.zzdr(Unknown Source)
at com.google.android.gms.internal.firebase_database.zzhd.run(Unknown Source)
at android.os.Handler.handleCallback(Handler.java:761)
at android.os.Handler.dispatchMessage(Handler.java:98)
at android.os.Looper.loop(Looper.java:156)
at android.app.ActivityThread.main(ActivityThread.java:6595)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:942)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:832)
And that is the code that receives the data from firebase:
databasePaths = FirebaseDatabase.getInstance().getReference("Paths");
databasePaths.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
for (DataSnapshot items: dataSnapshot.getChildren())
{
info = items.getValue(Paths.class);
ArrayListCities.add(info.city);
}
adapterCities = new ArrayAdapter<>(CitiesList.this, android.R.layout.simple_list_item_1, ArrayListCities);
ListViewCities.setAdapter(adapterCities);
adapterCities.notifyDataSetChanged();
ListViewCities.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String selectedFromListCities = (ListViewCities.getItemAtPosition(position).toString());
Intent IntentGoN = new Intent(getApplicationContext(), FullListByCity.class);
IntentGoN.putExtra("city", selectedFromListCities);
startActivity(IntentGoN);
}
});
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
Upvotes: 0
Views: 111
Reputation: 3082
This is the problem
Class java.util.List has generic type parameters, please use GenericTypeIndicator instead
List needs to be told what kind of objects it's holding. So you will need to create a model class that can take in the variable ola
First Ensure your firebase database structure is
key-|
---city
---....
---ola|
-------0|
---------latitude
---------longitude
-------1|
---------...
Then create a model class for ola with the two objects(latitude and longitude) with constructors
And now you can use list as
List<Ola> ola;
Upvotes: 1