查询未订购Firebase子项

时间:2017-12-16 06:48:45

标签: java android firebase firebase-realtime-database

我想在时间戳排序的循环播放器视图中获取我的活动。 在我的Firebase中,我有一些事件,它们都有很长的时间戳(年+“”+月+“”+天),我希望最早的事件在我的列表之上,但方法Query lastEventQuery = mEventsDatabase.child(list_event_id).orderByChild("Timestamp")没有订购任何东西

Firebase Structure: FIREBASE --> Events --> exampleOne   ---> Timestamp: 19000101


                                        --> exampleTwo   ---> Timestamp: 20500131
                                        --> exampleThree ---> Timestamp: 20301101
                                        --> exampleFour  ---> Timestamp: 20501210

使用Recycler View查看应该订购的碎片。

       @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                 Bundle savedInstanceState) {


            mMainView2 = inflater.inflate(R.layout.fragment_requests, container, false);
            mEventsDatabase = FirebaseDatabase.getInstance().getReference().child("Events");
            mEventsDatabase.keepSynced(true);


            mLayoutManager = new LinearLayoutManager(getContext());
            create_event_btn = (Button) mMainView2.findViewById(R.id.create_btn);
            mEventList = (RecyclerView) mMainV

iew2.findViewById(R.id.event_list);
        mEventList.setHasFixedSize(true);
        mEventList.setLayoutManager(mLayoutManager);


        create_event_btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(getActivity(), Create_EventActivity.class);
                startActivity(intent);
            }
        });


        // Inflate the layout for this fragment

        return mMainView2;
    }

    @Override
    public void onStart() {
        super.onStart();


        FirebaseRecyclerAdapter<Events, EventsViewHolder> firebaseRecyclerAdapter = new FirebaseRecyclerAdapter<Events, EventsViewHolder>(

                Events.class,
                R.layout.event_single_layout,
                EventsViewHolder.class,
                mEventsDatabase

        ) {
            @Override
            protected void populateViewHolder(final EventsViewHolder eventsViewHolder, final Events events, int i) {


                final String list_event_id = getRef(i).getKey();
                Query lastEventQuery = mEventsDatabase.child(list_event_id).orderByChild("Timestamp");

                lastEventQuery.addValueEventListener(new ValueEventListener() {
                    @Override
                    public void onDataChange(DataSnapshot dataSnapshot) {
                        if (!dataSnapshot.exists()) {
                            return;
                        }

                       ...

                    @Override
                    public void onCancelled(DatabaseError databaseError) {

                    }
                });

            }
        };


        mEventList.setAdapter(firebaseRecyclerAdapter);

    }

    public static class EventsViewHolder extends RecyclerView.ViewHolder {

        View mView2;

        public EventsViewHolder(View itemView2) {
            super(itemView2);

            mView2 = itemView2;

        }

        public void setEventName(String name) {

            TextView eventName = (TextView) mView2.findViewById(R.id.event_name);
            eventName.setText(name);
        }

        public void setEventZeit(String time) {

            TextView eventTime = (TextView) mView2.findViewById(R.id.event_time);
            eventTime.setText(time);
        }

        public void setEventLocation(String location) {

            TextView eventTime = (TextView) mView2.findViewById(R.id.event_location);
            eventTime.setText(location);


        }

        public void setEventPreis(String price) {
            TextView eventPrice = (TextView) mView2.findViewById(R.id.event_price);
            eventPrice.setText(price);



        }


    }

我为这个问题切断了一些不重要的部分,所以现在可能有些{}错了。

2 个答案:

答案 0 :(得分:0)

要解决此问题,请更改以下代码行:

Query lastEventQuery = mEventsDatabase.child(list_event_id).orderByChild("Timestamp");

这两行代码:

DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
Query lastEventQuery = rootRef.child("Events").orderByChild("Timestamp");

在您的代码中,您的引用是错误的。使用orderByChild()方法时,您需要将父级位置上的引用指向Events节点而不是单个事件节点。

答案 1 :(得分:0)

这样做:

  DatabaseReference mEventsDatabase=FirebaseDatabase.getInstance().getReference().child("Events")

            mEventsDatabase.addValueEventListener(new ValueEventListener() {
                @Override
                public void onDataChange(DataSnapshot dataSnapshot) {
                  for(DataSnapshot data: dataSnapshot.getChildren()){
                        String name=data.child("Name").getValue().toString();
                        String price=data.child("price").getValue().toString();
                       }
                    }

将引用放在父节点示例Events上,然后使用addvalueventlistener检索数据。由于您的数据位于子级(位于Events下),因此请使用for循环在这些子级内迭代并获取所有值