计算孩子中的值的数量以检查重复数据
Posted
技术标签:
【中文标题】计算孩子中的值的数量以检查重复数据【英文标题】:count the number of value in a child to check for duplicate data 【发布时间】:2021-01-08 10:04:21 【问题描述】:我想计算这个孩子 date_time_doctor_active 的值数,如果计数大于 0,则无法插入 toast,如果计数为 0,则执行插入数据或 toast 消息的方法 我尝试了很多解决方案,但我无法正确解决
这是我的 json 代码
"appointment" :
"-MHp2gC9Wwk_NlbpdJRY" :
"active_appointment" : "true",
"active_appointment_patient_email" : "true_hanamoha075@gmail.com",
"date_appointment" : "23/Aug/2020",
"date_time_doctor_active" : "23/Aug/2020_10:15 AM_باسل الخالدي_true",
"doctor_number" : "باسل الخالدي",
"id_appointment" : "-MHp2gC9Wwk_NlbpdJRY",
"patient_email" : "hanamoha075@gmail.com",
"time_appointment" : "10:15 AM"
,
"-MHp2nWxB4jwx9K5NQGj" :
"active_appointment" : "true",
"active_appointment_patient_email" : "true_hanamoha075@gmail.com",
"date_appointment" : "23/Aug/2020",
"date_time_doctor_active" : "23/Aug/2020_10:15 AM_باسل الخالدي_true",
"doctor_number" : "باسل الخالدي",
"id_appointment" : "-MHp2nWxB4jwx9K5NQGj",
"patient_email" : "hanamoha075@gmail.com",
"time_appointment" : "10:15 AM"
,
"-MHp2rOv3xiY_ylkIdyf" :
"active_appointment" : "true",
"active_appointment_patient_email" : "true_hanamoha075@gmail.com",
"date_appointment" : "23/Aug/2020",
"date_time_doctor_active" : "23/Aug/2020_10:15 AM_باسل الخالدي_true",
"doctor_number" : "باسل الخالدي",
"id_appointment" : "-MHp2rOv3xiY_ylkIdyf",
"patient_email" : "hanamoha075@gmail.com",
"time_appointment" : "10:15 AM"
这是我的按钮单击侦听器中的 java 代码
submitButton.setOnClickListener(new View.OnClickListener()
@Override
public void onClick(View view)
final String patienttDate = txt_date.getText().toString().trim();
final String patienttTime = txt_time.getText().toString().trim();
final String patienttDoctor = choose_doctor.getSelectedItem().toString().trim();
final String patienttActive = "true";
appointment_DataBase.child("appointment")
.orderByChild("date_time_doctor_active")
.equalTo(patienttDate + "_" + patienttTime + "_" + patienttDoctor + "_" + patienttActive)
.addValueEventListener(new ValueEventListener()
@Override
public void onDataChange(@NonNull DataSnapshot snapshot)
if (snapshot.exists())
for (DataSnapshot dataSnapshot : snapshot.getChildren())
Appointment appointment = dataSnapshot.getValue(Appointment.class);
appointment.getDate_time_doctor_active();
size = size + 1;
else
if (size > 0)
Toast.makeText(booking.this, "cant inserrrrt", Toast.LENGTH_SHORT).show();
Toast.makeText(booking.this, "size is " + size, Toast.LENGTH_SHORT).show();
else
addAppointment();
@Override
public void onCancelled(@NonNull DatabaseError error)
);
);
【问题讨论】:
什么是appointment_DataBase
??
我的数据库引用@Omid.N
通过我要统计这个孩子date_time_doctor_active的值数是什么意思? date_time_doctor_active
不是数字,所以可以计算。
谢谢你的重播..我只是想通了.. date_time_doctor_active 是孩子的名字,它需要查询 4 个字段来检查约会是否存在
【参考方案1】:
我已经弄清楚了..我对代码做了一些更改,这就是结果
Query query = FirebaseDatabase.getInstance().getReference("appointment")
.orderByChild("date_time_doctor_active")
.equalTo(patienttDate+"_"+patienttTime+"_"+patienttDoctor+"_"+patienttActive);
query.addListenerForSingleValueEvent(new ValueEventListener()
@Override
public void onDataChange(@NonNull DataSnapshot snapshot)
size = 0;
for (DataSnapshot dataSnapshot : snapshot.getChildren())
Appointment appointment = dataSnapshot.getValue(Appointment.class);
appointment.getDate_time_doctor_active();
if (dataSnapshot.exists())
size = size +1;
if (size > 0)
Toast.makeText(booking.this, "this appointment is not available" , Toast.LENGTH_LONG).show();
else
addAppointment();
@Override
public void onCancelled(@NonNull DatabaseError error)
);
【讨论】:
以上是关于计算孩子中的值的数量以检查重复数据的主要内容,如果未能解决你的问题,请参考以下文章