如何删除Firebase数据库Android上的所选子项

时间:2016-12-24 12:19:30

标签: java android firebase firebase-realtime-database

我试图通过将唯一键设置为null来从数据库中删除子项。我所做的是 - 按住列表项以获取警报对话框,然后按删除我选择的条目。问题是,当我检索到密钥时,它会返回所有密钥。最接近我设法到目前为止是删除数据库中的所有内容。请检查下面的代码快照:

 mLocationListView.setLongClickable(true);
    mLocationListView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
        @Override
        public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {

            AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this)
                    .setTitle("Delete a Location")
                    .setMessage("Do you want to delete your location?")
                    .setPositiveButton("Delete", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {

//Problem is here. if I exclude .child(pushKey)everything is deleted from the database. 
                                DatabaseReference db_node = FirebaseDatabase.getInstance().getReference().getRoot().child("locations").child(pushKey);

                        db_node.setValue(null);
                        }
                    });
                AlertDialog alert = builder.create();
                alert.show();
            return true;
        }
    });

以下是我检索pushKey的方法。有任何建议如何以更恰当的方式做到这一点? (如果有的话)

 queryRef.addChildEventListener(new ChildEventListener() {
        @Override
        public void onChildAdded(DataSnapshot dataSnapshot, String s) {
            locationCurrent = dataSnapshot.getValue(LocationCurrent.class);
            mLocationAdapter.add(locationCurrent);
            pushKey = dataSnapshot.getKey();
            Log.i("PUSH KEY ", pushKey);

        }

        @Override
        public void onChildChanged(DataSnapshot dataSnapshot, String s) {
            locationCurrent = dataSnapshot.getValue(LocationCurrent.class);


        }

        @Override
        public void onChildRemoved(DataSnapshot dataSnapshot) {

        }

        @Override
        public void onChildMoved(DataSnapshot dataSnapshot, String s) {

        }

        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    });

我正在考虑将密钥放到数据库中,但没有必要,而且必须有最有效的解决方案。

更新:

我尝试使用将单个值事件侦听器附加到查询的另一个方法来执行此操作,但它不起作用,因为我无法确定单击哪个列表项,因此我删除了整个数据库。基本上,我需要单击列表项并删除它。但它不起作用。我完全按照下面的链接做了:

How to delete from firebase realtime database?

有什么建议吗?

1 个答案:

答案 0 :(得分:2)

在LocationCurrent.class中添加一个名为refKey的瞬态变量。保存参考onChildAdded

public void onChildAdded(DataSnapshot dataSnapshot, String s) {
    locationCurrent = dataSnapshot.getValue(LocationCurrent.class);
    locationCurrent.setRefKey(dataSnapshot.getKey()); 
    mLocationAdapter.add(locationCurrent);
}

在删除时,请执行以下操作:

mLocationListView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
    @Override
    public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
    DatabaseReference db_node = FirebaseDatabase.getInstance().getReference().child("locations").child(mLocationAdapter.getItem(position).getRefKey());
    db_node.removeValue();
});