使用FirebaseRecyclerAdapter时查询数据

时间:2016-10-30 15:36:36

标签: java android firebase firebase-realtime-database firebaseui

我试图查询我的数据,但我没有将其附加到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_);


    }

1 个答案:

答案 0 :(得分:0)

您首先声明的内容

Query queryRef;

和说;

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

然后在您的onStart放置querymDatabase

这样做

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


    }

希望这很好用