我使用Firebase数据库和回收站视图。 我使用push()向Firebase添加项目列表;命令。 在使用push();时,firebase为每个添加的项目创建唯一的ID。
现在,我想单击“删除”按钮删除项目,但是当我尝试removeValue()时;对我的数据库引用起作用,它将删除所有存储库,而不仅仅是项目。
我现在所拥有的:
从Firebase读取数据的片段
users.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull final DataSnapshot dataSnapshot) {
notes.clear();
for (DataSnapshot dataSnapshot1 : dataSnapshot.getChildren()) {
note = dataSnapshot1.getValue(Note.class);
notes.add(note);
}
adapter = new NotesAdapter(notes, getActivity());
RecyclerView.LayoutManager layoutManager = new GridLayoutManager(getActivity(), 3);
RVNotesList.setLayoutManager(layoutManager);
RVNotesList.setAdapter(adapter);
adapter.setOnNoteClickListener(new NotesAdapter.OnNoteClickListener() {
@Override
public void onNoteClick(final int position) {
users.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
//TODO delete pressed item.
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
}
});
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
我的onClick按钮
@Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.saveNoteBTN:
String title = ETnoteTitle.getText().toString().trim();
String desc = ETnoteDesc.getText().toString().trim();
String date = ETnoteLastDate.getText().toString().trim();
if (title.isEmpty() || desc.isEmpty() || date.isEmpty()) {
return;
}
note = new Note(title, desc, date);
users.push().setValue(note).addOnSuccessListener(new OnSuccessListener<Void>() {
@Override
public void onSuccess(Void aVoid) {
Log.d("NoteAdded", "true");
getFragmentManager().beginTransaction().
remove(getFragmentManager().findFragmentById(R.id.bottom_container)).commit();
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
Log.d("NoteAdded", "false");
}
});
break;
case R.id.deleteNoteBTN:
getFragmentManager().beginTransaction().
remove(getFragmentManager().findFragmentById(R.id.bottom_container)).commit();
break;
}
}
适配器 包com.enjoyapp.eventmanagement.Adapters;
import android.app.Dialog;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.cardview.widget.CardView;
import androidx.recyclerview.widget.RecyclerView;
import com.enjoyapp.eventmanagement.Models.Note;
import com.enjoyapp.eventmanagement.R;
import java.util.ArrayList;
public class NotesAdapter extends RecyclerView.Adapter<NotesAdapter.NotesViewHolder> {
private ArrayList<Note> notes;
private Context context;
private OnNoteClickListener onNoteClickListener;
public NotesAdapter(ArrayList<Note> notes, Context context) {
this.notes = notes;
this.context = context;
}
public interface OnNoteClickListener {
void onNoteClick(int position);
}
public void setOnNoteClickListener(OnNoteClickListener onNoteClickListener) {
this.onNoteClickListener = onNoteClickListener;
}
@NonNull
@Override
public NotesViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.note_list_item, parent, false);
return new NotesAdapter.NotesViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull final NotesViewHolder holder, final int position) {
final Note note = notes.get(position);
holder.mNoteTitle.setText(notes.get(position).getmTitle());
holder.mNoteDesc.setText(notes.get(position).getmDescription());
String lastDayToConfirmNote = (notes.get(position).getmDay() + "/"
+ notes.get(position).getmMonth() + "/" + notes.get(position).getmYear());
holder.mLastDay.setText(lastDayToConfirmNote);
@Override
public int getItemCount() {
return notes.size();
}
public class NotesViewHolder extends RecyclerView.ViewHolder {
private TextView mNoteTitle;
private TextView mNoteDesc;
private TextView mLastDay;
private CardView note_card;
public NotesViewHolder(@NonNull View itemView) {
super(itemView);
mNoteTitle = itemView.findViewById(R.id.note_title);
mNoteDesc = itemView.findViewById(R.id.note_description);
mLastDay = itemView.findViewById(R.id.note_last_date);
note_card = itemView.findViewById(R.id.note_card);
itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (onNoteClickListener!=null){
onNoteClickListener.onNoteClick(getAdapterPosition());
}
}
});
}
}
public void removeItem(ArrayList arrayList, int position) {
arrayList.remove(arrayList.get(position));
notifyItemRemoved(position);
notifyItemRangeChanged(position, arrayList.size());
}
}
答案 0 :(得分:4)
根据您的代码,看来 users 是您的父级根,并且您想要删除用户的一个子级。为此,您将需要该孩子的ID。您可以获取ID并将其保存在ArrayList中,例如
ArrayList<String> ids = new ArrayList
users.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull final DataSnapshot dataSnapshot) {
notes.clear();
for (DataSnapshot dataSnapshot1 : dataSnapshot.getChildren()) {
note = dataSnapshot1.getValue(Note.class);
notes.add(note);
ids.add(dataSnapshot1.getKey());//saving id of each child
}
adapter = new NotesAdapter(notes, getActivity());
RecyclerView.LayoutManager layoutManager = new GridLayoutManager(getActivity(), 3);
RVNotesList.setLayoutManager(layoutManager);
RVNotesList.setAdapter(adapter);
adapter.setOnNoteClickListener(new NotesAdapter.OnNoteClickListener() {
@Override
public void onNoteClick(final int position) {
users.child(ids.get(position)).removeValue();
}
});
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
注意:要更新注释arraylist,必须更新ids arraylist。