如何在Cloud Firestore上获取文档ID并将其删除?

时间:2018-12-16 07:48:23

标签: android firebase google-cloud-firestore

我一直在努力删除Cloud Firestore上的数据:(

有人知道我该如何解决这个问题?在Android上将其删除 应用程序,但不在Firestore上。如果您能提供帮助,那将是很好 对我来说:)

enter image description here

此外,这些是用于MainActivity的函数。

public class MainActivity extends AppCompatActivity  {


FloatingActionButton send;

ListView twitListView;


TwitAdapter twitAdapter;

String twitld;



FirebaseFirestore db;


CollectionReference twitRef;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);


    db = FirebaseFirestore.getInstance();


    twitRef = db.collection("twits");

    setContentView(R.layout.activity_main_coding);

    send = findViewById(R.id.send);
    twitListView = findViewById(R.id.twitList);

    twitAdapter = new TwitAdapter();

    twitListView.setAdapter(twitAdapter);


    twitld = doc.getId();


    twitListView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
        @Override
        public boolean onItemLongClick(AdapterView<?> parent, View view, final int position, final long id) {
            android.app.AlertDialog.Builder alert_confirm = new android.app.AlertDialog.Builder(MainActivity.this);
            alert_confirm.setMessage("Are you sure delete this memo?").setCancelable(false).setPositiveButton("YES",
                    new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            // 'YES'

                            ConnectivityManager connectivityManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
                            NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();
                            if (networkInfo != null) {
                                if (networkInfo.getType() == ConnectivityManager.TYPE_WIFI || networkInfo.getType() == ConnectivityManager.TYPE_MOBILE) {
                                    twitAdapter.deleteItem(position);
                                    twitAdapter.notifyDataSetChanged();
                                    twitRef.document(twitld).delete();
                                    Toast toast = Toast.makeText(getApplicationContext(),"Complete deleting!",Toast.LENGTH_SHORT);
                                    toast.show();
                                }
                            } else { // doesn't connect to network
                                android.app.AlertDialog.Builder alert = new android.app.AlertDialog.Builder(MainActivity.this);
                                alert.setPositiveButton("OK", new DialogInterface.OnClickListener() {
                                    @Override
                                    public void onClick(DialogInterface dialog, int which) {
                                        WifiManager wifiManager = (WifiManager) getApplicationContext().getSystemService(Context.WIFI_SERVICE);
                                        if (!wifiManager.isWifiEnabled()) {
                                            if (wifiManager.getWifiState() != WifiManager.WIFI_STATE_ENABLING) {

                                                wifiManager.setWifiEnabled(true);
                                            }
                                        }
                                        dialog.dismiss();     
                                    }
                                });
                                alert.setMessage("Try again after connecting network");
                                alert.show();
                            }

                        }
                    }).setNegativeButton("Cancel",
                    new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            // 'No'
                            Toast t = Toast.makeText(getApplicationContext(), "Deleting has been cancelled.", Toast.LENGTH_SHORT);
                            t.show();
                            return;
                        }
                    });
            android.app.AlertDialog alert = alert_confirm.create();
            alert.show();
            return false;
        }
    });


    send.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

           Intent intent = new Intent(MainActivity.this, NewTwitActivity.class);


            startActivityForResult(intent, 100);

        }
    });


    twitRef.orderBy("timestamp").addSnapshotListener(new EventListener<QuerySnapshot>() {
        @Override
        public void onEvent(@Nullable QuerySnapshot queryDocumentSnapshots, @Nullable FirebaseFirestoreException e) {


            for (DocumentChange doc : queryDocumentSnapshots.getDocumentChanges()) {


                if (doc.getType() == DocumentChange.Type.ADDED) {


                    Twit twit = doc.getDocument().toObject(Twit.class);

                    twitAdapter.addItem(twit);


                } else if (doc.getType() == DocumentChange.Type.REMOVED) {

                         **This part** 

                    twitRef.document(twitld).delete();
                    Twit twit = doc.getDocument().toObject(Twit.class);
                    twitAdapter.deleteItem2(twit);
                   twitAdapter.notifyDataSetChanged();
                }
            }
        }
    });

}








@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {


    if (requestCode == 100) {


        if (resultCode == RESULT_OK) {

            String msg = data.getStringExtra("message");

            HashMap<String, Object> twit = new HashMap<>();


            twit.put("writer", "rico");
            twit.put("message", msg);


            twit.put("timestamp", new Date());


            twitRef.add(twit);

        }
    }

}
}

0 个答案:

没有答案