I did recyclerView retrieve data from firebase real-time database, and created button for delete item from recycelerView and from database
I have 2 questions
1- after i add new item in recyclerView and then try delete it not delete
directly , just when add item and run code again then i can delete it
public class WorkerRecyclerViewAdapter extends RecyclerView.Adapter<WorkerRecyclerViewAdapter.ViewHolder> {
private Context context;
private ArrayList<Worker> workersList;
private onItemClickListener mListener;
public WorkerRecyclerViewAdapter(Context context, ArrayList <Worker> workersList) {
this.context = context;
this.workersList = workersList;
}
@NonNull
@Override
public WorkerRecyclerViewAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.worker_card,parent,false);
return new ViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
final Worker worker = this.workersList.get(position);
holder.name.setText(workersList.get(position).getName());
holder.phone.setText(workersList.get(position).getPhone());
holder.deleteWorker.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
DatabaseReference databaseReference = FirebaseDatabase.getInstance().getReference("Employees").child(worker.getPhone());
databaseReference.removeValue();
}
});
}
@Override
public int getItemCount() {
return workersList.size();
}
public class ViewHolder extends RecyclerView.ViewHolder{
TextView name,phone;
ImageView deleteWorker;
public ViewHolder(@NonNull View itemView) {
super(itemView);
name = itemView.findViewById(R.id.name);
phone = itemView.findViewById(R.id.phone);
deleteWorker = itemView.findViewById(R.id.delete_worker);
}
}
}
add data to recycler view (java)
final FirebaseDatabase database = FirebaseDatabase.getInstance();
final DatabaseReference table_employee = database.getReference("Employees");
addWorker.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
final ProgressDialog dialog = new ProgressDialog(Add_worker.this);
dialog.setMessage("?????!");
dialog.show();
if (name.length()==0){
dialog.dismiss();
name.setError("???? ????");
}else if (phone.length()==0){
dialog.dismiss();
phone.setError("???? ??? ???????");
}else{
table_employee.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot snapshot) {
//check if already phone exist
if (snapshot.child(phone.getText().toString()).exists()){
dialog.dismiss();
Toast.makeText(Add_worker.this, "??? ???? ?? ???", Toast.LENGTH_LONG).show();
}else{
dialog.dismiss();
Worker user = new Worker(name.getText().toString(),phone.getText().toString());
table_employee.child(phone.getText().toString()).setValue(user);
Toast.makeText(Add_worker.this, "?? ??????? ?????", Toast.LENGTH_LONG).show();
Intent intent =new Intent(Add_worker.this,Workerr.class);
startActivity(intent);
finish();
}
}
@Override
public void onCancelled(@NonNull DatabaseError error) {
}
});
}}
recyclerView page (java)
recyclerView_worker = findViewById(R.id.worker_recyclerView);
LinearLayoutManager layoutManager =new LinearLayoutManager(this);
recyclerView_worker.setLayoutManager(layoutManager);
recyclerView_worker.setHasFixedSize(true);
databaseReference = FirebaseDatabase.getInstance().getReference();
workerList = new ArrayList <>();
ClearList();
GetDataFromFirebase();
}
private void GetDataFromFirebase() {
Query query = databaseReference.child("Employees");
query.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
ClearList();
for (DataSnapshot snapshot: dataSnapshot.getChildren()){
Worker worker = new Worker();
worker.setName(snapshot.child("name").getValue().toString());
worker.setPhone(snapshot.child("phone").getValue().toString());
workerList.add(worker);
}
workerRecyclerViewAdapter = new WorkerRecyclerViewAdapter(getApplicationContext(),workerList);
recyclerView_worker.setAdapter(workerRecyclerViewAdapter);
workerRecyclerViewAdapter.notifyDataSetChanged();
}
@Override
public void onCancelled(@NonNull DatabaseError error) {
}
});
}
private void ClearList(){
if (workerList != null){
workerList.clear();
if (workerRecyclerViewAdapter != null){
workerRecyclerViewAdapter.notifyDataSetChanged();
}
}
workerList= new ArrayList <>();
}
Workers page (java)
public class Worker {
private String name;
private String phone;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public Worker() {
}
public Worker(String name, String phone) {
this.name = name;
this.phone = phone;
}
}
2- the key in child is same the value of this child
ex: when user add phone is added as key and value
[enter image description here][1]
[1]: https://i.stack.imgur.com/FJfzc.jpg
Please help please
Thanks in advance
question from:
https://stackoverflow.com/questions/65949055/how-can-i-delete-from-firebase-realtime-database-before-app-close 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…