【问题标题】:How to delete data in ListView and also from Firebase?如何删除 ListView 和 Firebase 中的数据?
【发布时间】:2019-01-25 04:13:33
【问题描述】:

我正在开发一个在 Firebase 中保存药品库存的 Android 应用程序。所有库存都完美保存在 Firebase 上。 我访问的链接是thisthis 我在 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>

这是我从 Firebase 检索数据的检索活动

public class RetreiveActivity extends AppCompatActivity {
ListView mylistView;
DatabaseReference db;
List<ClassMedicine> medicineList;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_retreive);
    mylistView= findViewById(R.id.mylist);
    medicineList= new ArrayList<>();
    db= FirebaseDatabase.getInstance().getReference("medicines");

}

@Override
protected void onStart() {
    super.onStart();
    db.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            medicineList.clear();
            for (DataSnapshot medicineSnapshot:dataSnapshot.getChildren()){
                ClassMedicine classMedicine=medicineSnapshot.getValue(ClassMedicine.class);
                medicineList.add(classMedicine);
            }
            MedicineList adapter=new MedicineList(RetreiveActivity.this,medicineList);
            mylistView.setAdapter(adapter);
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    });
}

}

这是我的模型类

public class ClassMedicine {
String medicineId;
String medicineName;
String medicineUsage;
String medicineGenre;
String mediDate;

public ClassMedicine(){
}
public ClassMedicine(String medicineId, String medicineName, String medicineUsage, String medicineGenre,String mediDate) {
    this.medicineId = medicineId;
    this.medicineName = medicineName;
    this.mediDate= mediDate;
    this.medicineUsage = medicineUsage;
    this.medicineGenre = medicineGenre;
}

public String getMedicineId() {
    return medicineId;
}

public String getMedicineName() {
    return medicineName;
}

public String getMedicineUsage() {
    return medicineUsage;
}

public String getMedicineGenre() {
    return medicineGenre;
}
public String getMediDate() {
    return mediDate;
}

}

这是我的列表活动

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

}

我想从列表中删除一个特定的列表项。随着列表项被删除,特定数据也应该从火力库中删除

【问题讨论】:

    标签: android firebase android-studio listview google-cloud-firestore


    【解决方案1】:

    首先你需要改变你的适配器类的实现,创建一个像下面这样的内部 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;
                        }
                    }
                }
            });
        }
    
    }
    

    【讨论】:

    • 已更新答案,立即尝试
    • 我的模型类是ClassMedicine
    • 在回答帖子中添加了整个适配器类,只需复制和粘贴即可。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-01-27
    • 2015-08-18
    • 1970-01-01
    相关资源
    最近更新 更多