How can i fix this error retrieving data from firebase
This is the image of my data which is stored in the database.
'E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.pahalunitedfoundation, PID: 31028
com.google.firebase.database.DatabaseException: Can't convert object of type java.lang.String to type com.example.pahalunitedfoundation.mainModel
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)
at com.firebase.ui.database.ClassSnapshotParser.parseSnapshot(ClassSnapshotParser.java:29)
at com.firebase.ui.database.ClassSnapshotParser.parseSnapshot(ClassSnapshotParser.java:15)
at com.firebase.ui.common.BaseCachingSnapshotParser.parseSnapshot(BaseCachingSnapshotParser.java:36)
at com.firebase.ui.common.BaseObservableSnapshotArray.get(BaseObservableSnapshotArray.java:52)
at com.firebase.ui.database.FirebaseRecyclerAdapter.getItem(FirebaseRecyclerAdapter.java:108)
at com.firebase.ui.database.FirebaseRecyclerAdapter.onBindViewHolder(FirebaseRecyclerAdapter.java:148)
at androidx.recyclerview.widget.RecyclerView$Adapter.onBindViewHolder(RecyclerView.java:7254)
at androidx.recyclerview.widget.RecyclerView$Adapter.bindViewHolder(RecyclerView.java:7337)
at androidx.recyclerview.widget.RecyclerView$Recycler.tryBindViewHolderByDeadline(RecyclerView.java:6194)
at androidx.recyclerview.widget.RecyclerView$Recycler.tryGetViewHolderForPositionByDeadline(RecyclerView.java:6460)
at androidx.recyclerview.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:6300)
at androidx.recyclerview.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:6296)
at androidx.recyclerview.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:2330)
at androidx.recyclerview.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1631)
at androidx.recyclerview.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1591)
at androidx.recyclerview.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:668)
at androidx.recyclerview.widget.RecyclerView.dispatchLayoutStep2(RecyclerView.java:4309)
at androidx.recyclerview.widget.RecyclerView.dispatchLayout(RecyclerView.java:4012)
at androidx.recyclerview.widget.RecyclerView.onLayout(RecyclerView.java:4578)
at android.view.View.layout(View.java:23059)
at android.view.ViewGroup.layout(ViewGroup.java:6479)
at androidx.constraintlayout.widget.ConstraintLayout.onLayout(ConstraintLayout.java:1873)
at android.view.View.layout(View.java:23059)
at android.view.ViewGroup.layout(ViewGroup.java:6479)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:332)
at android.widget.FrameLayout.onLayout(FrameLayout.java:270)
at android.view.View.layout(View.java:23059)
at android.view.ViewGroup.layout(ViewGroup.java:6479)
at androidx.appcompat.widget.ActionBarOverlayLayout.onLayout(ActionBarOverlayLayout.java:536)
at android.view.View.layout(View.java:23059)
at android.view.ViewGroup.layout(ViewGroup.java:6479)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:332)
at android.widget.FrameLayout.onLayout(FrameLayout.java:270)
at android.view.View.layout(View.java:23059)
at android.view.ViewGroup.layout(ViewGroup.java:6479)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1829)
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1673)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1582)
at android.view.View.layout(View.java:23059)
at android.view.ViewGroup.layout(ViewGroup.java:6479)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:332)
at android.widget.FrameLayout.onLayout(FrameLayout.java:270)
at com.android.internal.policy.DecorView.onLayout(DecorView.java:804)
at android.view.View.layout(View.java:23059)
at android.view.ViewGroup.layout(ViewGroup.java:6479)
at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:3622)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:3082)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:2075)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:8512)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:1076)
at android.view.Choreographer.doCallbacks(Choreographer.java:897)
at android.view.Choreographer.doFrame(Choreographer.java:826)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:1061)
at android.os.Handler.handleCallback(Handler.java:938)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:236)
at android.app.ActivityThread.main(ActivityThread.java:8056)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:656)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:967)
below is the code written in my main main.java file below and i have deleted the imports in this question cos that was showing error
package com.example.pahalunitedfoundation;
public class Events extends AppCompatActivity {
eventAdapter eventAdapter;
RecyclerView recyclerView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_events);
recyclerView = findViewById(R.id.rv);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
FirebaseRecyclerOptions<mainModel> options =
new FirebaseRecyclerOptions.Builder<mainModel>()
.setQuery(FirebaseDatabase.getInstance().getReference().child("data"), mainModel.class)
.build();
eventAdapter = new eventAdapter(options);
recyclerView.setAdapter(eventAdapter);
}
@Override
protected void onStart() {
super.onStart();
eventAdapter.startListening();
}
@Override
protected void onStop() {
super.onStop();
eventAdapter.startListening();
}
}
below is the code of my adapter class (minus imports)
package com.example.pahalunitedfoundation;
public class eventAdapter extends FirebaseRecyclerAdapter <mainModel,eventAdapter.myViewHolder> {
/**
* Initialize a {@link RecyclerView.Adapter} that listens to a Firebase query. See
* {@link FirebaseRecyclerOptions} for configuration options.
*
* @param options
*/
public eventAdapter(@NonNull FirebaseRecyclerOptions<mainModel> options) {
super(options);
}
@Override
protected void onBindViewHolder(@NonNull myViewHolder holder, int position, @NonNull mainModel model) {
holder.ename.setText(model.getEname());
holder.date.setText(model.getDate());
holder.description.setText(model.getDescription());
Glide.with(holder.img.getContext())
.load(model.getSurl())
.placeholder(R.drawable.common_google_signin_btn_icon_dark)
.circleCrop()
.error(R.drawable.common_google_signin_btn_icon_dark)
.into(holder.img);
}
@NonNull
@Override
public myViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.event_item,parent,false);
return new myViewHolder(view);
}
class myViewHolder extends RecyclerView.ViewHolder{
// enter code here
CircleImageView img;
TextView ename,date,description;
public myViewHolder(@NonNull View itemView) {
super(itemView);
img = (CircleImageView)itemView.findViewById(R.id.img1);
ename = itemView.findViewById(R.id.enametext);
date = itemView.findViewById(R.id.Date);
description = itemView.findViewById(R.id.description);
}
}
}
3 answers
-
answered 2022-01-19 17:08
Greg Giacovelli
A Few issues: the listener probably should be stopped during onStop
@Override protected void onStop() { super.onStop(); eventAdapter.stopListening(); // was startListening }
Also the stack trace directly seems to be related to the absence of any real adapter code for the
mainModel
class. If you could include this class it may help a bit. Right now the recyclerview is setup just try to instantiate amainModel
by passing it whatever is indata
I assume it's a string.FirebaseRecyclerOptions<mainModel> options = new FirebaseRecyclerOptions.Builder<mainModel>() .setQuery(FirebaseDatabase.getInstance().getReference().child("data"), mainModel.class) .build();
If the constructor of
mainModel
doesn't take aString
, an adapter would need to be made ORmainModel
need to be updated to take aString
-
answered 2022-01-19 17:10
DHAVAL A.
Error shows that you have string data in
data
node of firebase database. So this line of code unable to convert that string tomainModel
type.FirebaseRecyclerOptions<mainModel> options = new FirebaseRecyclerOptions.Builder<mainModel>() .setQuery(FirebaseDatabase.getInstance().getReference().child("data"), mainModel.class) .build();
So look out for the database on the firebase console and remove string if you have any inside
data
node.Best Practice,
Always give your class a capitalized name.
For example,
Named like this,
public class MainModel
Instead of this,
public class mainModel
-
answered 2022-01-20 20:21
Alex Mamo
When you are passing the following reference:
FirebaseDatabase.getInstance().getReference().child("data")
Along with "mainModel" class to FirebaseRecyclerOptions.Builder's "setQuety()" method, it means that you want to use the Firebase-UI library for displaying all "mainModel" objects that exist within:
root -> data
Which is actually 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.example.pahalunitedfoundation.mainModel
To solve this, you need to add an extra level under your "data" node. This can be simply done using the DatabaseReference's push() method. So when you are adding an object at the above reference, add it like this:
DatabaseReference db = FirebaseDatabase.getInstance().getReference(); DatabaseReference dataRef = db.child("data"); dataRef.push().setValue(mainModelObject);
In this way, your database schema should look like this:
Firebase-root | --- data | --- $pushedId | --- date: "dd/mm/yyyy" | --- description: "this is a des" | --- name: "Pahal..." | --- surl: "https://..."
The code for getting the data may remain unchanged.
do you know?
how many words do you know