查询未订购Firebase子项

问题描述 投票:0回答:2

我希望按时间戳排序的循环器视图中获取我的事件。在我的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);



        }


    }

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

java android firebase firebase-realtime-database
2个回答
0
投票

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

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

用这两行代码:

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

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


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循环在这些子级内部进行迭代并获取所有值

© www.soinside.com 2019 - 2024. All rights reserved.