Retriving data from firebase to populate a RecyclerView

Retriving data from firebase to populate a RecyclerView



I want to retrieve some data from FireBase in order to populate a RecyclerView.My app idea is : a user picks some locations from a map fragment and then stores them in Firebase Database.Those saved locations are displayed in a Places fragment where I want to use a RecylcerView.When I run the app i get the following errors





08-29 23:01:35.257 25039-25039/com.aplicatiechat.mcristi.firebaseandmapsdemoforlearning E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.aplicatiechat.mcristi.firebaseandmapsdemoforlearning, PID: 25039
com.google.firebase.database.DatabaseException: Class com.aplicatiechat.mcristi.firebaseandmapsdemoforlearning.Places is missing a constructor with no arguments
at com.google.android.gms.internal.zzekq.zze(Unknown Source)
at com.google.android.gms.internal.zzekp.zzb(Unknown Source)
at com.google.android.gms.internal.zzekp.zza(Unknown Source)
at com.google.firebase.database.DataSnapshot.getValue(Unknown Source)
at com.firebase.ui.database.ClassSnapshotParser.parseSnapshot(ClassSnapshotParser.java:22)
at com.firebase.ui.database.ObservableSnapshotArray.getObject(ObservableSnapshotArray.java:141)
at com.firebase.ui.database.CachingObservableSnapshotArray.getObject(CachingObservableSnapshotArray.java:40)
at com.firebase.ui.database.FirebaseRecyclerAdapter.getItem(FirebaseRecyclerAdapter.java:127)
at com.firebase.ui.database.FirebaseRecyclerAdapter.onBindViewHolder(FirebaseRecyclerAdapter.java:164)
at android.support.v7.widget.RecyclerView$Adapter.onBindViewHolder(RecyclerView.java:6673)
at android.support.v7.widget.RecyclerView$Adapter.bindViewHolder(RecyclerView.java:6714)
at android.support.v7.widget.RecyclerView$Recycler.tryBindViewHolderByDeadline(RecyclerView.java:5647)
at android.support.v7.widget.RecyclerView$Recycler.tryGetViewHolderForPositionByDeadline(RecyclerView.java:5913)
at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5752)
at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5748)
at android.support.v7.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:2232)
at android.support.v7.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1559)
at android.support.v7.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1519)
at android.support.v7.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:614)
at android.support.v7.widget.RecyclerView.dispatchLayoutStep2(RecyclerView.java:3812)
at android.support.v7.widget.RecyclerView.dispatchLayout(RecyclerView.java:3529)
at android.support.v7.widget.RecyclerView.consumePendingUpdateOperations(RecyclerView.java:1767)
at android.support.v7.widget.RecyclerView$1.run(RecyclerView.java:356)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:777)
at android.view.Choreographer.doCallbacks(Choreographer.java:590)
at android.view.Choreographer.doFrame(Choreographer.java:559)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:763)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:145)
at android.app.ActivityThread.main(ActivityThread.java:5951)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1400)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1195)





This is my Places Fragment:



public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState)
mView = inflater.inflate(R.layout.places_fragment, container, false);
mRecylcerView = mView.findViewById(R.id.places);
mRecylcerView.setHasFixedSize(true);
mRecylcerView.setLayoutManager(new LinearLayoutManager(getContext()));

return mView;



@Override
public void onStart()
super.onStart();
mFirebaseUser = FirebaseAuth.getInstance().getCurrentUser();
mDataBase = FirebaseDatabase.getInstance().getReference().child("users").child(mFirebaseUser.getUid());
DatabaseReference places = FirebaseDatabase.getInstance().getReference().child("users").child(mFirebaseUser.getUid()).child("place");
DatabaseReference keys = FirebaseDatabase.getInstance().getReference().child("users").child(mFirebaseUser.getUid()).child("key");
FirebaseIndexRecyclerAdapter<Places,PlacesViewHolder> firebaseRecyclerAdapter = new FirebaseIndexRecyclerAdapter<Places,PlacesViewHolder>(Places.class,R.layout.place_layout,PlacesViewHolder.class,places,keys)
@Override
protected void populateViewHolder(PlacesViewHolder viewHolder, Places model, int position)
viewHolder.setLat(model.getLat());
viewHolder.setLon(model.getLon());

;
mRecylcerView.setAdapter(firebaseRecyclerAdapter);


}
class PlacesViewHolder extends RecyclerView.ViewHolder
View mView;
private TextView lat;
private TextView lon;


public PlacesViewHolder(View itemView)
super(itemView);
mView = itemView;
lat = mView.findViewById(R.id.lat_view);
lon = mView.findViewById(R.id.lon_view);


public void setLat(String n)
lat.setText(n);


public void setLon(String e)
lon.setText(e);






and here it is my Model Places class:





public class Places extends RecyclerView.ViewHolder
private String lat;
private String lon;

public Places(View itemView)
super(itemView);



public String getLat()
return lat;


public void setLat(String lat)
this.lat = lat;


public String getLon()
return lon;


public void setLon(String lon)
this.lon = lon;







Here is a screenshow with my databse :
Firebase database




1 Answer
1



Add an empty constructor in your Places class as


Places


public Places()



because firebase recycler adapter did not find this empty constructor in your Places class. And replace this


FirebaseIndexRecyclerAdapter<Places,PlacesViewHolder> firebaseRecyclerAdapter = new FirebaseIndexRecyclerAdapter<Places,PlacesViewHolder>(Places.class,R.layout.place_layout,PlacesViewHolder.class,places,keys)



with


FirebaseIndexRecyclerAdapter<Places,PlacesViewHolder> firebaseRecyclerAdapter = new FirebaseIndexRecyclerAdapter<Places,PlacesViewHolder>(Places.class,R.layout.place_layout,PlacesViewHolder.class,keys,places) {



And you need to remove the extend from Places class.





I get an error when I tried to implement the empty constructor: " Default constructor not available in android.support.v7.widget.RecyclerView;"
– Daniel Iovescu
Aug 29 at 21:07





why you need to extends Places with RecyclerView.ViewHolder ? it is redundant
– faiizii
Aug 29 at 21:22





I removed the extended RecyclerView.ViewHolder but still the exception persists.
– Daniel Iovescu
Aug 29 at 21:23





after removing the extend and there is a empty constructor present but the problem is still there?
– faiizii
Aug 29 at 21:26





" com.google.firebase.database.DatabaseException: Can't convert object of type java.lang.String to type com.aplicatiechat.mcristi.firebaseandmapsdemoforlearning.Places" still the same problem
– Daniel Iovescu
Aug 29 at 21:26



Required, but never shown



Required, but never shown






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

How do I collapse sections of code in Visual Studio Code for Windows?

ャフサォクコ ケウ,コ,ワ メ,ロスョノ゙,クネ,フムカヤヲニ,エコ゚ツ ウイオン゙ケワサネォキモュキォウイノンコチ゚メヌナイゥフュ,カヒウネェ ネ,ホノケ,ムュキ ッボーミュハ,チ ツス ィ メウイマヤ,゙ウチ ヅ ロ,ォジヌェ ャヌット ェ,マャ,チナエヒネソキツテ トホヲヲミーァ