如何在fire-base数据库android中更新嵌套数据

问题描述 投票:1回答:1

Nested data in firebase database snap pic 大家好,我把数据存储在一个包含嵌套子项的实时数据库中,我通过使用推送功能存储了这些数据,以避免覆盖问题,但现在问题我不知道如何获取我已经知道的特定数据在快照图像中突出显示。不幸的是,我正在努力获取嵌套数据,以便执行编辑或删除功能。以下是addUser类以及将数据提取到RecyclerView中的部分

public class AddUserAppointment {

   private String StartBusinse;
   private String StartBusinseTime ;
    private String EndBusinseTime ;
   private String startFirstAppoint ;
   private String endSecondAppointment ;
   private String AppointmentStuts;
  // private String AppointmentKey;  

    public AddUserAppointment() {
        ////empty
    }

    public AddUserAppointment(String startBusinse, String startBusinseTime, String endBusinseTime, String startFirstAppoint, String endSecondAppointment, String appointmentStuts) {
        StartBusinse = startBusinse;
        StartBusinseTime = startBusinseTime;
        EndBusinseTime = endBusinseTime;
        this.startFirstAppoint = startFirstAppoint;
        this.endSecondAppointment = endSecondAppointment;
        AppointmentStuts = appointmentStuts;
    }

    public String getStartBusinse() {
        return StartBusinse;
    }

    public void setStartBusinse(String startBusinse) {
        StartBusinse = startBusinse;
    }

    public String getStartBusinseTime() {
        return StartBusinseTime;
    }

    public void setStartBusinseTime(String startBusinseTime) {
        StartBusinseTime = startBusinseTime;
    }

    public String getEndBusinseTime() {
        return EndBusinseTime;
    }

    public void setEndBusinseTime(String endBusinseTime) {
        EndBusinseTime = endBusinseTime;
    }

    public String getStartFirstAppoint() {
        return startFirstAppoint;
    }

    public void setStartFirstAppoint(String startFirstAppoint) {
        this.startFirstAppoint = startFirstAppoint;
    }

    public String getEndSecondAppointment() {
        return endSecondAppointment;
    }

    public void setEndSecondAppointment(String endSecondAppointment) {
        this.endSecondAppointment = endSecondAppointment;
    }

    public String getAppointmentStuts() {
        return AppointmentStuts;
    }

    public void setAppointmentStuts(String appointmentStuts) {
        AppointmentStuts = appointmentStuts;
    }

获取数据类

public class ListAppointmentCustomerClass extends AppCompatActivity {
    private DatabaseReference AppointmentRef,SeconDref;
    private FirebaseAuth mAuth;
    private String PostKey;
    private RecyclerView ListAllApointment;
    private String pushkey;
    private Button Book,Cancel;
    private String AppointmentKey;
    private String keyId;
    private String m_Text = "";
    private boolean AppointmentCondation = true;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_list_appointment_customer_class);
        //pushkey = AppointmentRef.getKey();

        PostKey = getIntent().getExtras().get("Postkey").toString();

        AppointmentRef = FirebaseDatabase.getInstance().getReference().child("Appointments").child(PostKey);

        ///

        ////
        ListAllApointment = (RecyclerView)findViewById(R.id.ListAppointmentView);
        LinearLayoutManager customerLayoutManager = new LinearLayoutManager(this);
        customerLayoutManager.setReverseLayout(true);
        customerLayoutManager.setStackFromEnd(true);
        ListAllApointment.setLayoutManager(customerLayoutManager);
        //TODO:All the methods shoud be list here
        DisplayuploadedAppointment();
    }

    public static class HolderClass extends RecyclerView.ViewHolder{
        View mView;
        Button bookAppointment , CancelAppoinment;

        public HolderClass(View itemView) {
            super(itemView);
            mView = itemView;

            bookAppointment = (Button)mView.findViewById(R.id.BookAnppointments);

        }

        public void setStartFirstAppoint(String startFirstAppoint){
          TextView StartAppointment = (TextView)mView.findViewById(R.id.StartAppointmet);
          StartAppointment.setText(startFirstAppoint);
        }
        public void setEndSecondAppointment(String endSecondAppointment){
            TextView EndAppointment = (TextView)mView.findViewById(R.id.EndAppointment);
            EndAppointment.setText(endSecondAppointment);

        }
        public void setAppointmentStuts(String appointmentStuts){
            TextView appointment = (TextView)mView.findViewById(R.id.BookingStatus);
            appointment.setText(appointmentStuts);
        }



    }

    private void DisplayuploadedAppointment() {
        FirebaseRecyclerAdapter<AddUserAppointment,HolderClass >AppointmentRecyclerAdapter =
                new FirebaseRecyclerAdapter<AddUserAppointment, HolderClass>
                        (
                                AddUserAppointment.class,
                                R.layout.listallapointment,
                                HolderClass.class,
                                AppointmentRef
                        ) {
                    @Override
                    protected void populateViewHolder(HolderClass viewHolder, AddUserAppointment model, final int position)
                    {


                        viewHolder.setStartFirstAppoint(model.getStartFirstAppoint());
                        viewHolder.setEndSecondAppointment(model.getEndSecondAppointment());
                        viewHolder.setAppointmentStuts(model.getAppointmentStuts());
                        viewHolder.mView.setTag(position);
                        viewHolder.bookAppointment.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View view) {
                                final DatabaseReference mDatabase = FirebaseDatabase.getInstance().getReference().child("Appointments").child(PostKey);

                                final String id = getRef(position).getKey();
                                mDatabase.child(id).addValueEventListener(new ValueEventListener() {
                                    @Override
                                    public void onDataChange(DataSnapshot dataSnapshot) {
                                        for (DataSnapshot snapshot : dataSnapshot.getChildren()){
                                            System.out.println("Id"+id);
                                            String bookingCondation = dataSnapshot.child("appointmentStuts").getValue().toString() ;
                                            HashMap hashMap = new HashMap<>();
                                            hashMap.put("appointmentStuts","Boooked");

                                           System.out.println("Postkey"+PostKey);
                                           mDatabase.updateChildren(hashMap).addOnCompleteListener(new OnCompleteListener() {
                                               @Override
                                               public void onComplete(@NonNull Task task) {

                                               }
                                           });



//                                            String key = snapshot.getKey().toString();
//                                            System.out.println(key);


//                                            String bookingCondation = dataSnapshot.child("appointmentStuts").getValue().toString() ;
//                                            System.out.println("Appointment Status "+bookingCondation);

                                           // Log.e("Appointment : " , "" + appointmentStuts+ap.getStartFirstAppoint()+key);


                                        }

                                    }

                                    @Override
                                    public void onCancelled(DatabaseError databaseError) {

                                    }
                                });

                            }
                        });


                    }
                };
        ListAllApointment.setAdapter(AppointmentRecyclerAdapter);



    }



    }
android firebase firebase-realtime-database
1个回答
1
投票

您可以遍历用户以获取他们的数据,如下所示:

  DatabaseReference databaseReference = FirebaseDatabase.getInstance().getReference().child("Appointments").addListenerForSingleValueEvent(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            Iterator<DataSnapshot> dataSnapshots = dataSnapshot.getChildren().iterator();

            while (dataSnapshots.hasNext()) {
                DataSnapshot dataSnapshotChild = dataSnapshots.next();
                AddUserAppointment user = dataSnapshotChild.getValue(AddUserAppointment.class);
            }
        }
        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    });
© www.soinside.com 2019 - 2024. All rights reserved.