Reputation: 394
I have an Android project with a Fragment
class:
package com.example.android.dtuguide;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import com.firebase.ui.database.FirebaseRecyclerAdapter;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
public class NoticesFragment extends Fragment {
private RecyclerView mNoticesList;
private DatabaseReference mDatabase;
public NoticesFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_notices, container, false);
mDatabase = FirebaseDatabase.getInstance().getReference().child("notice");
mNoticesList = (RecyclerView) view.findViewById(R.id.notices_list);
mNoticesList.setHasFixedSize(true);
mNoticesList.setLayoutManager(new LinearLayoutManager(getActivity()));
return view;
}
@Override
public void onStart() {
super.onStart();
FirebaseRecyclerAdapter<Notices,NoticeViewHolder> firebaseRecyclerAdapter
= new FirebaseRecyclerAdapter<Notices, NoticeViewHolder>(
Notices.class,
R.layout.list_view_row,
NoticeViewHolder.class,
mDatabase
) {
@Override
protected void populateViewHolder(NoticeViewHolder viewHolder, Notices model, int position) {
viewHolder.setRefertext(model.getRefer());
viewHolder.setDatetext(model.getDate());
}
};
mNoticesList.setAdapter(firebaseRecyclerAdapter);
}
public static class NoticeViewHolder extends RecyclerView.ViewHolder{
View mView;
public NoticeViewHolder(View itemView) {
super(itemView);
mView = itemView;
}
public void setRefertext(String refertext){
TextView referText = (TextView) mView.findViewById(R.id.refer_text);
referText.setText(refertext);
}
public void setDatetext (String datetext){
TextView dateText = (TextView) mView.findViewById(R.id.date_text);
dateText.setText(datetext);
}
}
}
and a Notices
:
package com.example.android.dtuguide;
public class Notices {
private String refer , link ;
private String date,count;
public Notices (){
}
public String getDate() {
return date;
}
public void setDate(String date) {
this.date = date;
}
public String getLink() {
return link;
}
public void setLink(String link) {
this.link = link;
}
public String getRefer() {
return refer;
}
public void setRefer(String refer) {
this.refer = refer;
}
}
When I run the app, it shows the following error:
.example.android.dtuguide E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.android.dtuguide, PID: 8456
com.google.firebase.database.DatabaseException: Can't convert object of type java.lang.String to type com.example.android.dtuguide.Notices
at com.google.android.gms.internal.zzanp.zze(Unknown Source)
at com.google.android.gms.internal.zzanp.zzb(Unknown Source)
at com.google.android.gms.internal.zzanp.zza(Unknown Source)
at com.google.firebase.database.DataSnapshot.getValue(Unknown Source)
at com.firebase.ui.database.FirebaseRecyclerAdapter.parseSnapshot(FirebaseRecyclerAdapter.java:147)
at com.firebase.ui.database.FirebaseRecyclerAdapter.getItem(FirebaseRecyclerAdapter.java:136)
at com.firebase.ui.database.FirebaseRecyclerAdapter.onBindViewHolder(FirebaseRecyclerAdapter.java:176)
at android.support.v7.widget.RecyclerView$Adapter.onBindViewHolder(RecyclerView.java:5471)
at android.support.v7.widget.RecyclerView$Adapter.bindViewHolder(RecyclerView.java:5504)
at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:4741)
at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:4617)
at android.support.v7.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:1994)
at android.support.v7.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1390)
at android.support.v7.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1353)
at android.support.v7.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:574)
at android.support.v7.widget.RecyclerView.dispatchLayoutStep2(RecyclerView.java:3028)
at android.support.v7.widget.RecyclerView.dispatchLayout(RecyclerView.java:2906)
at android.support.v7.widget.RecyclerView.consumePendingUpdateOperations(RecyclerView.java:1482)
at android.support.v7.widget.RecyclerView.access$400(RecyclerView.java:147)
at android.support.v7.widget.RecyclerView$1.run(RecyclerView.java:294)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:858)
at android.view.Choreographer.doCallbacks(Choreographer.java:670)
at android.view.Choreographer.doFrame(Choreographer.java:603)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:844)
at android.os.Handler.handleCallback(Handler.java:746)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5443)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:728)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
I've tried searching for some fix but nothing worked. I'd really appreciate if someone can help me understand what the problem is and if possible post a fix.
Thanks!
Upvotes: 0
Views: 927
Reputation: 1880
i got the answer for this create another reference for DatabaseReference and call the child to that Databasereference
Upvotes: 2
Reputation: 394
after some deep troubleshooting , theres was a problem with Notices constructor methods and JSON file which i was recieving from firebase , corrected my JSON file and voila !
Upvotes: 0
Reputation: 355
probably your error is in you adapter, here:
FirebaseRecyclerAdapter<Notices,NoticeViewHolder> firebaseRecyclerAdapter
= new FirebaseRecyclerAdapter<Notices, NoticeViewHolder>(
Notices.class,
R.layout.list_view_row,
NoticeViewHolder.class,
mDatabase <- you are passing a DatabaseReference
)
You probably need to change your mDatabase
to something related with a Notices
Upvotes: 0