firebaserecycleradapter

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的位置

像这样做

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


    }


希望这很好

10-08 07:09