在recyclerview>中刷入cardview时,如何从Firebase中删除对象

时间:2019-07-11 14:06:41

标签: android firebase android-recyclerview

我有recyclerview列表,该列表也存储在firebase数据库中,我希望在刷卡视图时将其从recyclerview以及firebase中删除,但是我不知道如何从firebase中删除它。

代码:-

    new ItemTouchHelper(new ItemTouchHelper.SimpleCallback( 0,ItemTouchHelper.LEFT|ItemTouchHelper.RIGHT) {

        @Override
        public boolean onMove(@NonNull RecyclerView recyclerView, @NonNull RecyclerView.ViewHolder viewHolder, @NonNull RecyclerView.ViewHolder viewHolder1) {
            return false;
        }

        @Override
        public void onSwiped(@NonNull final RecyclerView.ViewHolder viewHolder, final int i) {

            contactList.remove(viewHolder.getAdapterPosition());
            mAdapter.notifyDataSetChanged();
            Toast.makeText(getApplicationContext(),"Contact Removed",Toast.LENGTH_LONG).show();
        }
    }).attachToRecyclerView(mRecyclerView);

适配器:

class saveContactAdapter extends RecyclerView.Adapter<saveContactAdapter.ExampleViewHolder> {

private ArrayList<SaveContact> contactList;

public static class ExampleViewHolder extends RecyclerView.ViewHolder {

    public TextView mTextView1;
    public TextView mTextView2;

    public ExampleViewHolder(@NonNull View itemView) {
        super(itemView);
        mTextView1 = itemView.findViewById(R.id.textView);
        mTextView2 = itemView.findViewById(R.id.textView2);

        itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                v.getContext().startActivity(new Intent(v.getContext(), AllUsersActivity.class));
            }
        });
    }
}

public saveContactAdapter(ArrayList<SaveContact> contactList) {
    this.contactList = contactList;
}
//Context context;

// LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

//View view =inflater.inflate(R.layout.contact_item,null);
@NonNull
@Override
public ExampleViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
    View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.contact_item,viewGroup,false);
    ExampleViewHolder evh = new ExampleViewHolder(v);
    return evh;
}

@Override
public void onBindViewHolder(@NonNull final saveContactAdapter.ExampleViewHolder contactViewHolder, final int i) {
    final SaveContact currentItem = contactList.get(i);
    contactViewHolder.mTextView1.setText(currentItem.getmText1());
    contactViewHolder.mTextView2.setText(currentItem.getmText2());

}


@Override
public int getItemCount() {
    return contactList.size();
}

} The highlighted part means one cardview and the next is the other cardview

在刷卡视图时,如何从Firebase中删除此信息?

1 个答案:

答案 0 :(得分:0)

使用此值并将“”替换为您的值。

FirebaseDatabase.getInstance().getReference("").child("").removeValue().addOnCompleteListener(new OnCompleteListener<Void>() {
                @Override
                public void onComplete(@NonNull Task<Void> task) {
                    if (task.isSuccessful()) {
                        if (!cu.getPfp().equals("Default")) {
                            FirebaseStorage.getInstance().getReference().child(cu.getUid() + ".jpg").delete().addOnCompleteListener(new OnCompleteListener<Void>() {
                                @Override
                                public void onComplete(@NonNull Task<Void> task) {
                                    if (task.isSuccessful()) {
                                      //Success
                                    } else{
                                       // Error
                                    }
                                }
                            });