从Firestore填充多个文档的微调框如何删除所选项目?

时间:2018-10-09 20:18:47

标签: android firebase google-cloud-firestore spinner

从我的标题中可以看到,我有一个微调器,其中填充了从Firestore检索的数据,现在我只是想不出如何从Firestore中删除所选项目,具体来说我如何获得选择一个项目,以便将其删除,这是任何有兴趣的人的解决方案:

private FirebaseFirestore db = FirebaseFirestore.getInstance();
private Spinner spinner;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_booking_dates);

    spinner = findViewById(R.id.spinner2);

    final Button save = findViewById(R.id.saveDate);

    final List<String> idlist = new ArrayList<>();
    final List<String> list = new ArrayList<>();

    //list.add("Choose a Date");

    db.collection("BookingDates").get().addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
        @Override
        public void onComplete(@NonNull Task<QuerySnapshot> task) {
            if (task.isSuccessful()) {
                for (QueryDocumentSnapshot document : task.getResult()) {

                    list.add(document.get("Date").toString());
                    idlist.add(document.getId());


                }


                final ArrayAdapter<String> adapter = new ArrayAdapter<>(BookingDates.this, android.R.layout.simple_spinner_item
                        ,list);
                adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
                spinner.setAdapter(adapter);

                spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
                    @Override
                    public void onItemSelected(AdapterView<?> adapterView, View view,final int i, long l) {
                        String id = idlist.get(i);
                        Toast.makeText(BookingDates.this, "ID: " + id , Toast.LENGTH_SHORT).show();
                        save.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View view) {
                                String id = idlist.get(i);
                                db.collection("BookingDates").document(id).delete();
                                Toast.makeText(BookingDates.this, "ID: " + id + " /DELETED", Toast.LENGTH_SHORT).show();
                            }
                        });

                    }

                    @Override
                    public void onNothingSelected(AdapterView<?> adapterView) {

                    }
                });

            } else {
                Toast.makeText(BookingDates.this, task.getException().toString(), Toast.LENGTH_SHORT).show();
            }
        }
    });
}

1 个答案:

答案 0 :(得分:0)

要删除文档,您需要知道其ID。因此,当您将日期添加到微调框时,您还需要将文档ID保留在某个位置,以便用户单击 Done 按钮时可以查找它。

一个过于简单的示例:

for (QueryDocumentSnapshot document : task.getResult()) {
    list.add(document.get("Date").toString());
    idlist.add(document.getId();
}

idlist只是ID列表,类似于list是日期列表。

final List<String> idlist = new ArrayList<>();

然后,当用户在微调器中选择一个项目时:

public void onItemSelected(AdapterView<?> adapterView, View view, int position, long id) {
  String id = idlist.get(position);
  db.collection("BookingDates").doc(id).delete();
}