gpt4 book ai didi

java - 如何从 firebase 列表中删除项目

转载 作者:行者123 更新时间:2023-12-01 19:34:03 25 4
gpt4 key购买 nike

我使用 firebase 数据库和回收器 View 。我使用 push () 将项目列表添加到 firebase;命令。使用push()时; ,firebase 为每个添加的项目创建一个唯一的 id。

现在,我想通过单击删除按钮删除项目,但是当我尝试removeValue();函数到我的数据库引用,它删除所有存储库而不仅仅是项目。

我现在拥有的:

从 firebase 读取数据的 fragment

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());
}
}

最佳答案

根据您的代码,似乎 users 是您的父根,您想要删除用户的子级之一。为此,您需要该 child 的 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) {

}
});

注意:当你更新notes数组列表时,你必须更新ids数组列表。

关于java - 如何从 firebase 列表中删除项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58476864/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com