我尝试使用 OnDeleteData 从我的 RecyclerView 中删除一项。但我在适配器文件上有错误。(如果我将这种方法用于 Activity 很好,但我将我的 Activity 更改为片段)
适配器文件:
public class menuUserAdapter extends RecyclerView.Adapter<menuUserAdapter.ViewHolder> {
public interface dataListener{
void onDeleteData(Transaksi transaksi , int position);
}
dataListener listener;
public menuUserAdapter(ArrayList<Transaksi> listtransaksi, Context context) {
this.listtransaksi = listtransaksi;
//this.listuser = listuser;
this.context = context;
listener = (homeFragment) context;
}
public void onBindViewHolder(@NonNull ViewHolder holder, final int position) {
holder.listitem.setOnLongClickListener(new View.OnLongClickListener() {
public boolean onLongClick(final View v) {
listener.onDeleteData(listtransaksi.get(position),position);
}
};
}
}
在片段文件上:
public class homeFragment extends Fragment implements menuUserAdapter.dataListener {
public void onDeleteData(Transaksi transaksi, int position) {
if(reference != null){
reference.child("transaksi").child(transaksi.getKey()).removeValue().addOnSuccessListener(new OnSuccessListener<Void>() {
@Override
public void onSuccess(Void aVoid) {
Toast.makeText(getActivity(), "Data Berhasil Dihapus", Toast.LENGTH_SHORT).show();
}
});
}
}
}
listener = (homeFragment) context; 我在这条线上有错误
慕田峪4524236
相关分类