我正在开发一个android应用程序,它必须通过单击列表项从Firebase
中删除数据。
在列表中从Firebase
中检索的数据,但是当我单击列表项删除数据时,它显示了白屏幕,然后退出到活动中。
我已经创建了一个当我单击一个项目时应该打开的Dialog Box
。
这是我的Dialog Box
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingTop="200dp"
android:background="#3E80B4"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="@+id/txt_dia"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_margin="10dp"
android:text="Do you realy want to delete Medicine ?"
android:textColor="@android:color/white"
android:textSize="15dp"
android:textStyle="bold"/>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:background="#3E80B4"
android:orientation="horizontal" >
<Button
android:id="@+id/btn_yes"
android:layout_width="100dp"
android:layout_height="30dp"
android:background="@android:color/white"
android:clickable="true"
android:text="Yes"
android:textColor="#5DBCD2"
android:textStyle="bold" />
<Button
android:id="@+id/btn_no"
android:layout_width="100dp"
android:layout_height="30dp"
android:layout_marginLeft="5dp"
android:background="@android:color/white"
android:clickable="true"
android:text="No"
android:textColor="#5DBCD2"
android:textStyle="bold" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
这是我的删除项目活动,当我单击项目时,它会打开
Dialog Box
public class ExpiryItemActivity extends AppCompatActivity {
ListView mylistView;
DatabaseReference db;
public Dialog MyDialog;
TextView tvDeleteDiaog;
public Button Yes, No;
Integer value;
List<ClassMedicine> medicineList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_expiry_item);
mylistView= findViewById(R.id.mylistexpiry);
Yes = findViewById(R.id.btn_yes);
No = findViewById(R.id.btn_no);
tvDeleteDiaog= findViewById(R.id.txt_dia);
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(ExpiryItemActivity.this,medicineList);
mylistView.setAdapter(adapter);
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
mylistView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
value=mylistView.getId();
Toast.makeText(ExpiryItemActivity.this, "Here" + value.toString(), Toast.LENGTH_SHORT).show();
customDialog();
}
private void customDialog() {
MyDialog = new Dialog(ExpiryItemActivity.this);
MyDialog.setContentView(R.layout.xmldialog);
MyDialog.setTitle("Delete Medicine?");
Toast.makeText(ExpiryItemActivity.this, value.toString(), Toast.LENGTH_SHORT).show();
final DatabaseReference child1 = db.child("medicine");
Toast.makeText(ExpiryItemActivity.this, "Here me", Toast.LENGTH_SHORT).show();
Yes.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
child1.child("mediDate").child(String.valueOf(value)).removeValue();
MyDialog.cancel();
}
});
No.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
MyDialog.cancel();
}
});
MyDialog.show();
}
});
}
}
这个类用于如何从
Firebase
中获取列表中的数据。 最佳答案
为了从列表中删除Dialog Box
中的数据,布局文件将保持与您设计的相同。
以及从列表中以及从Firebase
中删除列表项的Java文件。
public class ExpiryItemActivity extends AppCompatActivity {
ListView mylistView;
DatabaseReference db;
public Dialog MyDialog;
TextView tvDeleteDiaog;
public Button Yes, No;
String value;
List<ClassMedicine> medicineList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_expiry_item);
mylistView= findViewById(R.id.mylistexpiry);
tvDeleteDiaog= findViewById(R.id.txt_dia);
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(ExpiryItemActivity.this,medicineList);
mylistView.setAdapter(adapter);
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
mylistView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//value=ClassMedicine.getMedicineId();
ClassMedicine c=medicineList.get(position);
value=c.getMedicineId().toString();
customDialog();
}
private void customDialog() {
MyDialog = new Dialog(ExpiryItemActivity.this);
MyDialog.setContentView(R.layout.xmldialog);
MyDialog.setTitle("Delete Medicine?");
final DatabaseReference child1 = db.child("medicine");
Yes = MyDialog.findViewById(R.id.btn_yes);
No = MyDialog.findViewById(R.id.btn_no);
Yes.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String x= value.toString();
db.child(String.valueOf(value)).removeValue();
MyDialog.cancel();
}
});
No.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
MyDialog.cancel();
}
});
MyDialog.show();
}
});
}
}