com.google.firebase.database.DatabaseException: Can't convert object of type java.lang.String to type com.shubh.problemapp.modal2 [duplicate]
I do not understand what the problem is, according to me everything is working correctly.
Logcat:
com.google.firebase.database.DatabaseException: Can't convert object of type java.lang.String to type com.hamidmadarati.mobilsorgulari.Home.Dashboard.ui.PassengerRecyclerModel at com.google.firebase.database.core.utilities.encoding.CustomClassMapper.convertBean(CustomClassMapper.java:436) at com.google.firebase.database.core.utilities.encoding.CustomClassMapper.deserializeToClass(CustomClassMapper.java:232) at com.google.firebase.database.core.utilities.encoding.CustomClassMapper.convertToCustomClass(CustomClassMapper.java:80) at com.google.firebase.database.DataSnapshot.getValue(DataSnapshot.java:203)
Fragment:
public class PassengerFragment extends Fragment {
private RecyclerView listVehiclesWeekDay;
private FirebaseRecyclerOptions<PassengerRecyclerModel> options;
private FirebaseRecyclerAdapter<PassengerRecyclerModel, PassengerMyViewHolderRecycler> adapter;
private DatabaseReference databaseReference;
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_passenger, container,false);
listVehiclesWeekDay = view.findViewById(R.id.listVehiclesWeekDay);
listVehiclesWeekDay.setHasFixedSize(true);
listVehiclesWeekDay.setLayoutManager(new LinearLayoutManager(getActivity()));
databaseReference = FirebaseDatabase.getInstance().getReference().child("Travels").child("DaysOfTheWeekSimple");
options = new FirebaseRecyclerOptions.Builder<PassengerRecyclerModel>().setQuery(databaseReference, PassengerRecyclerModel.class).build();
adapter = new FirebaseRecyclerAdapter<PassengerRecyclerModel, PassengerMyViewHolderRecycler>(options){
@Override
protected void onBindViewHolder(@NonNull PassengerMyViewHolderRecycler holder, int position, @NonNull PassengerRecyclerModel model) {
if (model.getDay() != null){
holder.titleTextView.setText(model.getDay());
}
}
@Override
public PassengerMyViewHolderRecycler onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.recyclerow, parent, false);
return new PassengerMyViewHolderRecycler(view);
}
};
adapter.startListening();
listVehiclesWeekDay.smoothScrollToPosition(0);
listVehiclesWeekDay.setAdapter(adapter);
adapter.notifyDataSetChanged();
return view;
}
}
Class:
public class PassengerRecyclerModel {
private String day; //name
public PassengerRecyclerModel() {
//Empty - for Firebase Database
}
// model
public PassengerRecyclerModel(String day) {
this.day = day;
}
//Getter and Setter
public String getDay() {
return day;
}
public void setDay(String day) {
this.day = day;
}
}
When you are passing the following reference:
databaseReference = FirebaseDatabase.getInstance().getReference().child("Travels").child("DaysOfTheWeekSimple");
Along with "PassengerRecyclerModel" class to FirebaseRecyclerOptions.Builder's "setQuety()" method, means that you want to use the Firebase-UI library for displaying all "PassengerRecyclerModel" objects that exist within:
Travels -> DaysOfTheWeekSimple
Which actually is not possible, since under that reference it exists only a child of type String, hence the following error:
com.google.firebase.database.DatabaseException: Can't convert object of type java.lang.String to type com.hamidmadarati.mobilsorgulari.Home.Dashboard.ui.PassengerRecyclerModel
To solve this, you need to add an extra level under your "DaysOfTheWeekSimple" node. That can be simply achieved using the DatabaseReference's push() method. So when you are adding an object at the above reference, add it like this:
databaseReference.push().setValue(passengerRecyclerModel);
In this way, you database schema should look like this:
Firebase-root
|
--- Travels
|
--- DaysOfTheWeekSimple
|
--- $pushedId
|
--- day: "Day 5: 22-4-2021"
The code for getting the data may remain unchanged.
Edit:
According to your comments:
Nope, in fact, we do not create data
If you add the data directly in the Firebase Console, you won't be able to use push(). If you don't want to add data programmatically, the extra level of a layer is still required. So in that case, between the "DaysOfTheWeekSimple" node and the "day" property, add another node with at least a date, or an Id, like this:
Firebase-root
|
--- Travels
|
--- DaysOfTheWeekSimple
|
--- 22-4-2021 //Extra level added
|
--- day: "Day 5: 22-4-2021"
the problem is when it is obtained and firebase thinks it is an object and does not convert it to a string
Doesn't "think", the object is of type "PassengerRecyclerModel" and should be read accordingly in the adapter.