如何从Firebase数据库列表中获取属性

时间:2019-07-19 23:54:00

标签: android

Database format image

我有用户类和请求类,需要在用户个人资料中显示 属于该用户的请求,因此请参见上图。

我已经有当前的用户ID

        uid = FirebaseAuth.getInstance().getCurrentUser().getUid();

我需要在属性所有者中搜索所有子级“请求”才能看到 如果它与当前用户ID匹配

据我了解,我需要编写以下代码

       myRef = FirebaseDatabase.getInstance().getReference().child("requests");

    myRef.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot snapshot) {
            List<String> requestes_uid = new ArrayList<String>(); // Result will be holded Here
            for (DataSnapshot postSnapshot: snapshot.getChildren()) {


            /*
                if (owner.equals(uid))
                {
                    // this request belong to this user
                    modelrequests_list.add(request);
                }
            */
            }
        }

        @Override
        public void onCancelled(@NonNull DatabaseError databaseError) {

        }

    });

那我该怎么做

1 个答案:

答案 0 :(得分:0)

将此添加到您的for循环

//For setting time
    /*CarouselModel carouselModel = new CarouselModel("Tomorrow's QuizPoint at 10:00 PM","₹50,000","1562587387000","1562587389000");
    quizTiming.push().setValue(carouselModel);*/

    Query query = FirebaseDatabase.getInstance()
            .getReference()
            .child("quizTimings");

    FirebaseRecyclerOptions<CarouselModel> options =
            new FirebaseRecyclerOptions.Builder<CarouselModel>()
                    .setQuery(query, CarouselModel.class)
                    .build();

    scrollView = (RecyclerView) findViewById(R.id.picker);

    adapter = new FirebaseRecyclerAdapter<CarouselModel, TimeViewHolder>(options) {

        @Override
        public TimeViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
            View view = LayoutInflater.from(viewGroup.getContext())
                    .inflate(R.layout.item_time, viewGroup, false);

            return new TimeViewHolder(view);
        }

        @Override
        protected void onBindViewHolder(TimeViewHolder timeViewHolder, int i, final CarouselModel carouselModel) {

            try {
                if (carouselModel.getTime()!=null && !carouselModel.getTime().isEmpty()){
                    String quizTimings = carouselModel.getTime();
                    timeViewHolder.timeShow.setText(quizTimings);
                }else {
                    timeViewHolder.timeShow.setText("error Time");
                }

                if (carouselModel.getTimeMoney()!=null && !carouselModel.getTimeMoney().isEmpty()){
                    String timeMoney = carouselModel.getTimeMoney();
                    timeViewHolder.timeMoney.setText(timeMoney);
                }else {
                    timeViewHolder.timeMoney.setText("error TimeMoney");
                }
            } catch (Exception ex) {
                String err = (ex.getMessage()==null)?"QuizTiming error":ex.getMessage();
                Log.e("quiztiming logs","error "+err);
            }
        }
    };

    CustomLinearLayout linearLayoutManager = new CustomLinearLayout(this,LinearLayoutManager.HORIZONTAL,false);

    scrollView.addItemDecoration(new LinePagerIndicatorDecoration(MainActivity.this));
    scrollView.setLayoutManager(linearLayoutManager);
    scrollView.setAdapter(adapter);

我没有测试这段代码,但是您的答案应该是这样的。