Android 如何删除ListView中的数据以及Firebase中的数据?

Android 如何删除ListView中的数据以及Firebase中的数据?,android,firebase,android-studio,listview,google-cloud-firestore,Android,Firebase,Android Studio,Listview,Google Cloud Firestore,我正在开发一个Android应用程序,它可以在Firebase中保存药品库存。所有库存都完美地保存在Firebase上。 我访问过的链接是 我在Android中显示了列表中的所有库存。现在,我想做的是,通过在每个列表项上重新设置一个按钮,从列表中删除数据项。 下面是我的列表视图的XML代码 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.an

我正在开发一个Android应用程序,它可以在Firebase中保存药品库存。所有库存都完美地保存在Firebase上。 我访问过的链接是 我在Android中显示了列表中的所有库存。现在,我想做的是,通过在每个列表项上重新设置一个按钮,从列表中删除数据项。 下面是我的列表视图的XML代码

<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:background="@drawable/bgcolor"
android:layout_height="match_parent">

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_margin="10dp"
    android:background="@drawable/bgcolor">

    <TextView
        android:id="@+id/tvmediname"
        android:layout_width="250dp"
        android:layout_height="wrap_content"
        android:layout_below="@+id/tvname"
        android:layout_marginTop="14dp"
        android:paddingLeft="15dp"
        android:text="Name"
        android:textSize="20sp"
        tools:ignore="UnknownId" />

    <TextView
        android:id="@+id/tvmediusage"
        android:layout_width="250dp"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/tvmediname"
        android:layout_alignStart="@+id/tvmediname"
        android:layout_below="@+id/tvmediname"
        android:paddingLeft="15dp"
        android:text="Usage"
        android:textSize="20sp" />

    <TextView
        android:id="@+id/tvmedigenre"
        android:layout_width="250dp"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/tvmediname"
        android:layout_alignStart="@+id/tvmediname"
        android:layout_below="@+id/tvmediusage"
        android:paddingLeft="15dp"
        android:text="Company"
        android:textSize="20sp" />

    <TextView
        android:id="@+id/tvexpdate"
        android:layout_width="250dp"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_below="@+id/tvmedigenre"
        android:paddingLeft="15dp"
        android:text="Expiry Date"
        android:textSize="20sp" />

    <ImageButton
        android:id="@+id/btndelete"
        android:layout_width="90sp"
        android:layout_height="100sp"
        android:layout_alignParentEnd="true"
        android:layout_alignParentRight="true"
        android:layout_centerVertical="true"
        app:srcCompat="@drawable/btndelt" />

</RelativeLayout>
}

这是我的活动列表

public class MedicineList extends ArrayAdapter<ClassMedicine> {
private Activity context;
private List<ClassMedicine> medicineList;
public MedicineList(Activity context,List<ClassMedicine> medicineList){
    super(context,R.layout.mylistlayout,medicineList);
    this.context=context;
    this.medicineList=medicineList;
}

@NonNull
@Override
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
    LayoutInflater inflater= context.getLayoutInflater();
    View listViewItems= inflater.inflate(R.layout.mylistlayout,null,true);
    TextView textViewName=listViewItems.findViewById(R.id.tvmediname);
    TextView textViewGenre=listViewItems.findViewById(R.id.tvmedigenre);
    TextView textViewUsage=listViewItems.findViewById(R.id.tvmediusage);
    TextView textViewDate= listViewItems.findViewById(R.id.tvexpdate);
    ClassMedicine classMedicine= medicineList.get(position);
    textViewName.setText(classMedicine.getMedicineName());
    textViewGenre.setText(classMedicine.getMedicineGenre());
    textViewUsage.setText(classMedicine.getMedicineUsage());
    textViewDate.setText(classMedicine.getMediDate());
    return listViewItems;
}
公共类药物列表扩展了ArrayAdapter{
私人活动语境;
私人名单药物名单;
公共药物列表(活动上下文,列表药物列表){
super(上下文、右布局、mylistlayout、medicineList);
this.context=context;
this.medicineList=medicineList;
}
@非空
@凌驾
公共视图getView(int位置,@Nullable视图convertView,@NonNull视图组父级){
LayoutInflater充气器=上下文。getLayoutInflater();
View listViewItems=inflater.inflate(R.layout.mylistlayout,null,true);
TextView textViewName=listViewItems.findViewById(R.id.tVMIDName);
TextView textViewGenre=listViewItems.findViewById(R.id.tvmedigenre);
TextView textViewUsage=listViewItems.findViewById(R.id.tVMidUsage);
TextView textViewDate=listViewItems.findViewById(R.id.tvepDate);
ClassMedicine ClassMedicine=medicineList.get(位置);
textViewName.setText(classMedicine.getMedicineName());
textViewGenre.setText(classMedicine.getmedicinegree());
textViewUsage.setText(classMedicine.getMedicineUsage());
textViewDate.setText(classMedicine.getMediDate());
返回listViewItems;
}
}


我想从列表中删除特定的列表项。当列表项被删除时,特定数据也应该从firebase中删除。首先,您需要更改适配器类的实现,创建一个内部ViewHolder类,如下所示

public class ViewHolder {
        TextView textViewName;
        TextView textViewGenre;
        TextView textViewUsage;
        TextView textViewDate;
        ImageButton btnDelete;
        View itemView;
        int position;

        public ViewHolder(View view) {
            itemView = view;
            textViewName = view.findViewById(R.id.tvmediname);
            textViewGenre = view.findViewById(R.id.tvmedigenre);
            textViewUsage = view.findViewById(R.id.tvmediusage);
            textViewDate = view.findViewById(R.id.tvexpdate);
            btnDelete = view.findViewById(R.id.btndelete);

            // initialize all your views

        }

        public void setPosition(int position) {
            this.position = position;
        }


        public void bindViews() {
            ClassMedicine classMedicine = medicineList.get(position)
            textViewName.setText(classMedicine.getMedicineName());
            textViewGenre.setText(classMedicine.getMedicineGenre());
            textViewUsage.setText(classMedicine.getMedicineUsage());
            textViewDate.setText(classMedicine.getMediDate());
            btnDelete.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                   onDeleteItem(position);
                }
            });
        }

    }
在适配器类中添加以下方法

private void onDeleteItem(int position) {
           ClassMedicine classMedicine = medicineList.get(position);
                DatabaseReference db = FirebaseDatabase.getInstance().getReference("medicines");
                db.child("medicines").addListenerForSingleValueEvent(new ValueEventListener() {
                    @Override
                    public void onDataChange(DataSnapshot dataSnapshot) {
                        for (DataSnapshot snapshot : dataSnapshot.getChildren()) {
                            ClassMedicine medicine = snapshot.getValue(ClassMedicine.class);
                            if (classMedicine.getMedicineId().equals(medicine.getMedicineId())) {
                                databaseReference.child("medicines").child(snapshot.getKey().toString()).removeValue();
                                medicineList.remove(position);
                                notifyDataSetChanged();
                                break;
                            }
                        }
                    }
                });
            }
然后相应地修改
getView
方法

@Override
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            LayoutInflater inflater= context.getLayoutInflater();
            convertView = inflater.inflate(R.layout.mylistlayout,null,true);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
        }
        else {
            holder = (ViewHolder) convertView.getTag();
        }
        holder.setPosition(position);
        holder.bindViews();
        return convertView;
}
注意:我刚刚编写了伪代码,但应该可以工作

这是最后一个适配器类,用于避免混淆

public class MedicineList extends ArrayAdapter<ClassMedicine> {
    private Activity context;
    private List<ClassMedicine> medicineList;
    public MedicineList(Activity context,List<ClassMedicine> medicineList){
        super(context,R.layout.mylistlayout,medicineList);
        this.context=context;
        this.medicineList=medicineList;
    }

    @NonNull
    @Override
    public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            LayoutInflater inflater= context.getLayoutInflater();
            convertView = inflater.inflate(R.layout.mylistlayout,null,true);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
        }
        else {
            holder = (ViewHolder) convertView.getTag();
        }
        holder.setPosition(position);
        holder.bindViews();
        return convertView;
    }

    public class ViewHolder {
        TextView textViewName;
        TextView textViewGenre;
        TextView textViewUsage;
        TextView textViewDate;
        ImageButton btnDelete;
        View itemView;
        int position;

        public ViewHolder(View view) {
            itemView = view;
            textViewName = view.findViewById(R.id.tvmediname);
            textViewGenre = view.findViewById(R.id.tvmedigenre);
            textViewUsage = view.findViewById(R.id.tvmediusage);
            textViewDate = view.findViewById(R.id.tvexpdate);
            btnDelete = view.findViewById(R.id.btndelete);

            // initialize all your views

        }

        public void setPosition(int position) {
            this.position = position;
        }


        public void bindViews() {
            ClassMedicine classMedicine = medicineList.get(position)
            textViewName.setText(classMedicine.getMedicineName());
            textViewGenre.setText(classMedicine.getMedicineGenre());
            textViewUsage.setText(classMedicine.getMedicineUsage());
            textViewDate.setText(classMedicine.getMediDate());
            btnDelete.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                   onDeleteItem(position);
                }
            });
        }

    }

    private void onDeleteItem(int position) {
        ClassMedicine classMedicine = medicineList.get(position);
        DatabaseReference db = FirebaseDatabase.getInstance().getReference("medicines");
        db.child("medicines").addListenerForSingleValueEvent(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                for (DataSnapshot snapshot : dataSnapshot.getChildren()) {
                    ClassMedicine medicine = snapshot.getValue(ClassMedicine.class);
                    if (classMedicine.getMedicineId().equals(medicine.getMedicineId())) {
                        databaseReference.child("medicines").child(snapshot.getKey().toString()).removeValue();
                        medicineList.remove(position);
                        notifyDataSetChanged();
                        break;
                    }
                }
            }
        });
    }

}
公共类药物列表扩展了ArrayAdapter{
私人活动语境;
私人名单药物名单;
公共药物列表(活动上下文,列表药物列表){
super(上下文、右布局、mylistlayout、medicineList);
this.context=context;
this.medicineList=medicineList;
}
@非空
@凌驾
公共视图getView(int位置,@Nullable视图convertView,@NonNull视图组父级){
视窗座;
if(convertView==null){
LayoutInflater充气器=上下文。getLayoutInflater();
convertView=充气机。充气(R.layout.mylistlayout,null,true);
支架=新的视图支架(convertView);
convertView.setTag(支架);
}
否则{
holder=(ViewHolder)convertView.getTag();
}
保持架。设置位置(位置);
holder.bindViews();
返回视图;
}
公共类视图持有者{
TextView textViewName;
文本视图文本视图类型;
TextView TextView用法;
text查看text查看日期;
图像按钮btnDelete;
查看项目视图;
内部位置;
公共视图持有者(视图){
itemView=视图;
textViewName=view.findViewById(R.id.tVMIDName);
text视图流派=view.findviewbyd(R.id.tvmedigenre);
textViewUsage=view.findViewById(R.id.tVMidUsage);
textViewDate=view.findViewById(R.id.tvepDate);
btnDelete=view.findViewById(R.id.btnDelete);
//初始化所有视图
}
公共无效设置位置(内部位置){
这个位置=位置;
}
公共视图(){
ClassMedicine ClassMedicine=medicineList.get(位置)
textViewName.setText(classMedicine.getMedicineName());
textViewGenre.setText(classMedicine.getmedicinegree());
textViewUsage.setText(classMedicine.getMedicineUsage());
textViewDate.setText(classMedicine.getMediDate());
btnDelete.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图){
项目(位置);
}
});
}
}
私有void onDeleteItem(内部位置){
ClassMedicine ClassMedicine=medicineList.get(位置);
DatabaseReference db=FirebaseDatabase.getInstance().getReference(“药物”);
db.child(“药物”).addListenerForSingleValueEvent(新值EventListener()){
@凌驾
公共void onDataChange(DataSnapshot DataSnapshot){
for(datanapshot快照:datanapshot.getchildrents()){
ClassMedicine=snapshot.getValue(ClassMedicine.class);
if(classMedicine.getMedicineId().equals(medicine.getMedicineId())){
databaseReference.child(“药物”).child(snapshot.getKey().toString()).removeValue();
药物清单。移除(位置);
notifyDataSetChanged();
打破
}
}
}
});
}
}

首先,您需要更改适配器类的实现,创建一个内部ViewHolder类,如下所示

public class ViewHolder {
        TextView textViewName;
        TextView textViewGenre;
        TextView textViewUsage;
        TextView textViewDate;
        ImageButton btnDelete;
        View itemView;
        int position;

        public ViewHolder(View view) {
            itemView = view;
            textViewName = view.findViewById(R.id.tvmediname);
            textViewGenre = view.findViewById(R.id.tvmedigenre);
            textViewUsage = view.findViewById(R.id.tvmediusage);
            textViewDate = view.findViewById(R.id.tvexpdate);
            btnDelete = view.findViewById(R.id.btndelete);

            // initialize all your views

        }

        public void setPosition(int position) {
            this.position = position;
        }


        public void bindViews() {
            ClassMedicine classMedicine = medicineList.get(position)
            textViewName.setText(classMedicine.getMedicineName());
            textViewGenre.setText(classMedicine.getMedicineGenre());
            textViewUsage.setText(classMedicine.getMedicineUsage());
            textViewDate.setText(classMedicine.getMediDate());
            btnDelete.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                   onDeleteItem(position);
                }
            });
        }

    }
在适配器类中添加以下方法

private void onDeleteItem(int position) {
           ClassMedicine classMedicine = medicineList.get(position);
                DatabaseReference db = FirebaseDatabase.getInstance().getReference("medicines");
                db.child("medicines").addListenerForSingleValueEvent(new ValueEventListener() {
                    @Override
                    public void onDataChange(DataSnapshot dataSnapshot) {
                        for (DataSnapshot snapshot : dataSnapshot.getChildren()) {
                            ClassMedicine medicine = snapshot.getValue(ClassMedicine.class);
                            if (classMedicine.getMedicineId().equals(medicine.getMedicineId())) {
                                databaseReference.child("medicines").child(snapshot.getKey().toString()).removeValue();
                                medicineList.remove(position);
                                notifyDataSetChanged();
                                break;
                            }
                        }
                    }
                });
            }
然后相应地修改
getView
方法

@Override
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            LayoutInflater inflater= context.getLayoutInflater();
            convertView = inflater.inflate(R.layout.mylistlayout,null,true);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
        }
        else {
            holder = (ViewHolder) convertView.getTag();
        }
        holder.setPosition(position);
        holder.bindViews();
        return convertView;
}
注意:我刚刚编写了伪代码,但应该可以工作

这是最后一个适配器类,用于避免混淆

public class MedicineList extends ArrayAdapter<ClassMedicine> {
    private Activity context;
    private List<ClassMedicine> medicineList;
    public MedicineList(Activity context,List<ClassMedicine> medicineList){
        super(context,R.layout.mylistlayout,medicineList);
        this.context=context;
        this.medicineList=medicineList;
    }

    @NonNull
    @Override
    public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            LayoutInflater inflater= context.getLayoutInflater();
            convertView = inflater.inflate(R.layout.mylistlayout,null,true);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
        }
        else {
            holder = (ViewHolder) convertView.getTag();
        }
        holder.setPosition(position);
        holder.bindViews();
        return convertView;
    }

    public class ViewHolder {
        TextView textViewName;
        TextView textViewGenre;
        TextView textViewUsage;
        TextView textViewDate;
        ImageButton btnDelete;
        View itemView;
        int position;

        public ViewHolder(View view) {
            itemView = view;
            textViewName = view.findViewById(R.id.tvmediname);
            textViewGenre = view.findViewById(R.id.tvmedigenre);
            textViewUsage = view.findViewById(R.id.tvmediusage);
            textViewDate = view.findViewById(R.id.tvexpdate);
            btnDelete = view.findViewById(R.id.btndelete);

            // initialize all your views

        }

        public void setPosition(int position) {
            this.position = position;
        }


        public void bindViews() {
            ClassMedicine classMedicine = medicineList.get(position)
            textViewName.setText(classMedicine.getMedicineName());
            textViewGenre.setText(classMedicine.getMedicineGenre());
            textViewUsage.setText(classMedicine.getMedicineUsage());
            textViewDate.setText(classMedicine.getMediDate());
            btnDelete.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                   onDeleteItem(position);
                }
            });
        }

    }

    private void onDeleteItem(int position) {
        ClassMedicine classMedicine = medicineList.get(position);
        DatabaseReference db = FirebaseDatabase.getInstance().getReference("medicines");
        db.child("medicines").addListenerForSingleValueEvent(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                for (DataSnapshot snapshot : dataSnapshot.getChildren()) {
                    ClassMedicine medicine = snapshot.getValue(ClassMedicine.class);
                    if (classMedicine.getMedicineId().equals(medicine.getMedicineId())) {
                        databaseReference.child("medicines").child(snapshot.getKey().toString()).removeValue();
                        medicineList.remove(position);
                        notifyDataSetChanged();
                        break;
                    }
                }
            }
        });
    }

}
公共类药物列表扩展了ArrayAdapter{
私人活动语境;
私人名单药物名单;
公共药物列表(活动上下文,列表药物列表){
super(上下文、右布局、mylistlayout、medicineList);
this.context=context