如何从ListView中删除Firebase Android中的特定节点

时间:2019-09-30 16:48:38

标签: android firebase listview crud onitemclicklistener

我有一个问题。我想删除选定的ListView和引用键号(突出显示为20191,20192,...)的特定节点及其子级。 here's my Database Structure

这是我的OnItemClickListener,重点关注案例2以删除ListView

listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(final AdapterView<?> parent, View view, final int position, long id) {
                final Aduanclass aduanclass = aduanclassList.get(position);
//                selectedPosition = position;
//                itemSelected = true;
//                final Dialog dialog = new Dialog(MainActivity.this);
//                    dialog.setContentView(R.layout.activity_common_detail_aduan);
//                                dialog.setTitle("Pilih Aksi");
//                dialog.show();
//                final String Selection=arrayList.get(position);

                final CharSequence [] dialog ={"Lihat Detail", "Ubah", "Hapus"};
                AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
                builder.setTitle("Pilihan");

                builder.setItems(dialog, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {


                        switch (which){
                            case 0:
                                Intent intentCommondetail= new Intent(MainActivity.this,CommonDetailAduan.class);
                                intentCommondetail.putExtra("primekeyy", aduanclass.getPrimekey());
                                intentCommondetail.putExtra("kategory", aduanclass.getKategori());
                                intentCommondetail.putExtra("deskripsy",aduanclass.getDeskripsi());
//                                intentCommondetail.putExtra("kategori",Selection);
//                                intentCommondetail.putExtra("deskripsi",Selection);
                                startActivity(intentCommondetail);
                                break;
                            case 1:
                                Intent intentCommonedit= new Intent(MainActivity.this,CommonUpdateAduan.class);
//                                intentCommonedit.putExtra("primekeyy", aduanclass.getPrimekey());
                                intentCommonedit.putExtra("primekeyy", aduanclass.getPrimekey());
                                intentCommonedit.putExtra("kategory", aduanclass.getKategori());
                                intentCommonedit.putExtra("deskripsy",aduanclass.getDeskripsi());
//                                intentCommondetail.putExtra("kategori",Selection);
//                                intentCommondetail.putExtra("deskripsi",Selection);
                                startActivity(intentCommonedit);
                                break;
                            case 2:
                                deleteData(primkey);
                        }
                    }
                });
                builder.create().show();
            }
        });
        ((ArrayAdapter)listView.getAdapter()).notifyDataSetChanged();

这是我的deleteData方法

private void deleteData(final String primkey) {
        DatabaseReference mDatabase1 =FirebaseDatabase.getInstance().getReference("aduan").getRef();
        mDatabase1.removeValue();

我尝试运行代码,但没有删除Firebase数据库中的特定listView和特定节点,而是删除了“ aduan”的所有子级

仅供参考,我将数据输入中每次添加的键号设置为自动增加+1,但我不知道如何引用键号。

感谢您的帮助

2 个答案:

答案 0 :(得分:0)

您从未在数据库参考中使用过主键。代码应该是这样

private void deleteData(final String primkey) {
        DatabaseReference mDatabase1 =FirebaseDatabase.getInstance().getReference("aduan").child(primkey);
        mDatabase1.removeValue();
}

答案 1 :(得分:0)

private void deleteData(final String primkey) {
    DatabaseReference mDatabase1 =FirebaseDatabase.getInstance().getReference("aduan").getRef();
    mDatabase1.removeValue();

上面获得的参考是您的数据库ref,其中父节点返回了aduan。您应该使用

在aduan节点内导航
private void deleteData(final String primkey) {
    DatabaseReference mDatabase1 =FirebaseDatabase.getInstance().getReference("aduan").child("replace with child node key here");
    mDatabase1.removeValue();

完成后,必须将所做的更改通知给listViewAdapter,以便它可以刷新内容。 我希望这会有所帮助。