Lutaaya Huzaifah Lutaaya Huzaifah - 1 month ago 9
Java Question

Querying data while using FirebaseRecyclerAdapter

Am trying to query my data but I have failed to attach it on to the

FirebaseRecyclerAdapter
.

I have printed the output in the logcat and it comes successfully ,below is my code.

mDatabase = FirebaseDatabase.getInstance().getReference("Blog").child("Events");
Query queryRef = mDatabase.orderByChild("EventType").equalTo("Graduation Party");
queryRef.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
System.out.println("My Values :"+ dataSnapshot);


}

@Override
public void onCancelled(DatabaseError databaseError) {

}
});


But I want to attach those results on to the
FirebaseRecyclerAdapter
. Below is my code.

@Override
protected void onStart() {
super.onStart();
// mAuth.addAuthStateListener(mAuthLitsener);

FirebaseRecyclerAdapter<Event,EventViewHolder> firebaseRecyclerAdapter_ = new FirebaseRecyclerAdapter<Event, GraduationListActivity.EventViewHolder>(Event.class,
R.layout.item_event_view,
GraduationListActivity.EventViewHolder.class,
mDatabase) {
@Override
protected void populateViewHolder(GraduationListActivity.EventViewHolder viewHolder, final Event model, final int position) {
final String key_post = getRef(position).getKey();
viewHolder.setEventLocation(model.getEventlocation());
viewHolder.setEventName(model.getEventName());
viewHolder.setEventType(model.getEventType());
viewHolder.mView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// Toast.makeText(EventViewActivity.this,"This is : "+position,Toast.LENGTH_LONG).show();

Intent loadMainActivity = new Intent(GraduationListActivity.this,MainActivity.class);
loadMainActivity.putExtra(EventViewHolder.KEY_PARTY_NAME, model.getEventName());
startActivity(loadMainActivity);

}
});





}
};

mRecyclerview_event_view.setAdapter(firebaseRecyclerAdapter_);


}

Answer

What you do first declare

Query queryRef;

and the say;

mDatabase = FirebaseDatabase.getInstance().getReference("Blog").child("Events");
queryRef = mDatabase.orderByChild("EventType").equalTo("Graduation Party");

Then Afterwards in your onStart put query where there's mDatabase

Do it like this

@Override
    protected void onStart() {
        super.onStart();
        //   mAuth.addAuthStateListener(mAuthLitsener);

        FirebaseRecyclerAdapter<Event,EventViewHolder> firebaseRecyclerAdapter_ = new FirebaseRecyclerAdapter<Event, GraduationListActivity.EventViewHolder>(Event.class,
                R.layout.item_event_view,
                GraduationListActivity.EventViewHolder.class,
                queryRef) {
            @Override
            protected void populateViewHolder(GraduationListActivity.EventViewHolder viewHolder, final Event model, final int position) {
                final String key_post = getRef(position).getKey();
                viewHolder.setEventLocation(model.getEventlocation());
                viewHolder.setEventName(model.getEventName());
                viewHolder.setEventType(model.getEventType());
                viewHolder.mView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        // Toast.makeText(EventViewActivity.this,"This is : "+position,Toast.LENGTH_LONG).show();

                        Intent loadMainActivity = new Intent(GraduationListActivity.this,MainActivity.class);
                        loadMainActivity.putExtra(EventViewHolder.KEY_PARTY_NAME, model.getEventName());
                        startActivity(loadMainActivity);

                    }
                });





            }
        };

        mRecyclerview_event_view.setAdapter(firebaseRecyclerAdapter_);


    }

Hope this Works well