从Firebase数据库中删除特定数据

时间:2018-08-17 06:23:57

标签: android firebase android-layout firebase-realtime-database

我正在使用一个Android应用程序,该应用程序将药物和到期日期(如this)存储在一起

数据完美存储;现在,我要做的是按日期删除特定数据。我是Android的新手,已经搜索过视频,但是我很难理解我的教程。 我可以在Android中查看Firebase中的数据,但现在我想将其删除。我已经搜索了堆栈溢出thisthis

我检索数据的代码是

public class RetreiveActivity extends AppCompatActivity {
ListView mylistView;
DatabaseReference db;
List<ClassMedicine> medicineList;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_retreive);
    mylistView= findViewById(R.id.mylist);
    medicineList= new ArrayList<>();
    db= FirebaseDatabase.getInstance().getReference("medicines");
}

@Override
protected void onStart() {
    super.onStart();
    db.addValueEventListener(new ValueEventListener() {


        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            medicineList.clear();
            for (DataSnapshot medicineSnapshot:dataSnapshot.getChildren()){
                ClassMedicine classMedicine=medicineSnapshot.getValue(ClassMedicine.class);
                medicineList.add(classMedicine);
            }
            MedicineList adapter=new MedicineList(RetreiveActivity.this,medicineList);
            mylistView.setAdapter(adapter);
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    });
}

}

我保存数据的代码是

public class MedicineActivity extends AppCompatActivity {
EditText MedicineName,MedicineUsage;
TextView ExpiryDate;
Spinner spinnerSelectCompany;
Button MedicinesAddition;
DatabaseReference databaseMedicines;
DatePickerDialog datePickerDialog;
Calendar calendar;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_medicine);
    MedicineName= findViewById(R.id.edt_medicine_name);
    ExpiryDate= findViewById(R.id.tvtdate);
    ExpiryDate.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            FunDate();
        }

        private void FunDate() {
            calendar= Calendar.getInstance();
            int mYear = calendar.get(Calendar.YEAR);
            int mMonth= calendar.get(Calendar.MONTH);
            int mDay= calendar.get(Calendar.DAY_OF_MONTH);
            datePickerDialog= new DatePickerDialog(MedicineActivity.this, R.style.DatPickerDialogTheme, new DatePickerDialog.OnDateSetListener() {
                @Override
                public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
                    ExpiryDate.setText(dayOfMonth +"-"+ (month+1) + "-"+year );


                }
            },mYear,mMonth,mDay);
            datePickerDialog.show();
        }
    });
    MedicineUsage= findViewById(R.id.usage);
    spinnerSelectCompany= findViewById(R.id.spinner_company_name);
    MedicinesAddition= findViewById(R.id.btn_add_medicines);
    databaseMedicines = FirebaseDatabase.getInstance().getReference("medicines");
    MedicinesAddition.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            FunAddMedicines();
        }
    });
}
private void FunAddMedicines(){
    String name=MedicineName.getText().toString().trim();
     String mediusage= MedicineUsage.getText().toString().trim();
    String medicinecompanies= spinnerSelectCompany.getSelectedItem().toString();
   String mediDate = ExpiryDate.getText().toString().trim();
    if (name.isEmpty() || mediusage.isEmpty() || medicinecompanies.isEmpty()  ){
        Toast.makeText(this, "Fill all above Fields", Toast.LENGTH_LONG).show();
    }
    else {

        String id = databaseMedicines.push().getKey();
        ClassMedicine classMedicine = new ClassMedicine(id,name,mediusage,medicinecompanies,mediDate);
        databaseMedicines.child(id).setValue(classMedicine);
        Toast.makeText(this, "Medicine Added", Toast.LENGTH_LONG).show();
    }

}

}

这是模型类

public class ClassMedicine {
String medicineId;
String medicineName;
String medicineUsage;
String medicineGenre;
String mediDate;

public ClassMedicine(){
}
public ClassMedicine(String medicineId, String medicineName, String medicineUsage, String medicineGenre,String mediDate) {
    this.medicineId = medicineId;
    this.medicineName = medicineName;
    this.mediDate= mediDate;
    this.medicineUsage = medicineUsage;
    this.medicineGenre = medicineGenre;
}

public String getMedicineId() {
    return medicineId;
}

public String getMedicineName() {
    return medicineName;
}

public String getMedicineUsage() {
    return medicineUsage;
}

public String getMedicineGenre() {
    return medicineGenre;
}
public String getMediDate() {
    return mediDate;
}

我要删除或删除卡片后的数据查看此 this image

1 个答案:

答案 0 :(得分:1)

尝试此代码。

2018-08-17 14:22:38,670 INFO [org.apache.hadoop.hdfs.DFSClient] - Exception in createBlockOutputStream

java.io.IOException: Got error, status message , ack with firstBadLink as 172.16.1.148:50010
at org.apache.hadoop.hdfs.protocol.datatransfer.DataTransferProtoUtil.checkBlockOpStatus(DataTransferProtoUtil.java:140)
at org.apache.hadoop.hdfs.DFSOutputStream$DataStreamer.createBlockOutputStream(DFSOutputStream.java:1363)
at org.apache.hadoop.hdfs.DFSOutputStream$DataStreamer.nextBlockOutputStream(DFSOutputStream.java:1266)
at org.apache.hadoop.hdfs.DFSOutputStream$DataStreamer.run(DFSOutputStream.java:449)

在适配器添加界面中单击事件。

=IIF(Fields!OrderNumber.Value = 1234, "Red", "Blue")

在活动绑定适配器中进入列表视图,然后调用此代码...

mFirebaseDatabase = mFirebaseInstance.getReference("usersDb/UserTable");


mFirebaseDatabase.child("User").removeValue();
            Toast.makeText(getApplicationContext(), "Delete Successfuly", Toast.LENGTH_SHORT).show();