使用 FirebaseRecyclerAdapter 查询数据

Querying data while using FirebaseRecyclerAdapter

我正在尝试查询我的数据,但未能将其附加到 FirebaseRecyclerAdapter

我已经在 logcat 中打印了输出并且它成功了,下面是我的代码。

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) {

            }
        });

但我想将这些结果附加到 FirebaseRecyclerAdapter。下面是我的代码。

@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_);


    }

首先要做什么声明

Query queryRef;

然后说;

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

然后在你的 onStart 中放置 query mDatabase

这样做

@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_);


    }

希望这一切顺利