如何在Fire-Base数据库Android中更新嵌套数据

时间:2018-09-03 21:08:03

标签: android firebase firebase-realtime-database

Nested data in firebase database snap pic  嗨,大家好,我已经将数据存储在包含嵌套子项的实时数据库中,并且为了避免覆盖问题,我已经通过使用push功能存储了此数据,但是现在我不知道如何获取已经突出显示的特定数据了在快照图像中。不幸的是,我在如何获取嵌套数据以执行编辑或删除功能方面苦苦挣扎。以下是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);



    }



    }

1 个答案:

答案 0 :(得分: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) {

        }
    });