gpt4 book ai didi

android - 使用 Push() 生成的 Android 删除 Firebase 数据库中的单个项目

转载 作者:太空狗 更新时间:2023-10-29 13:13:05 24 4
gpt4 key购买 nike

我一直无法找到一个明确的答案来帮助我删除一个已作为 POJO 添加的节点,然后给出一个由 push() 生成的 key 。我想要做的就是在 ListView 上长按时删除给定的项目。我试过 removeValue() 但所做的只是删除整个父节点,这不是我想要的。我不知道如何获取生成的 key 。

您能否提供一个获取引用的示例,例如/node/以便我可以在 ListView 中对其使用 .removeValue()。我的问题是我试图在列表中获取正确的推送键,以便我可以在/nodes 下引用该键,然后删除该单个项目而不是整个节点引用,这就是 mDatabase.child("nodes").removeValue() 当前执行。当我想要像 mDatabase.child("nodes").child(pushedKey).removeValue() 这样的东西时。

当我向节点发送数据时,它看起来像这样。

mDatabase.child("nodes").push().setValue(POJO); 

我正在使用它来删除它...

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

//Delete node
listAdapter.mNodes.remove(position);
mDatabase.child("nodes").push().getRef().removeValue();
listAdapter.setNotifyOnChange(true);
Toast.makeText(getApplicationContext(),"Removed Item at: " + position,Toast.LENGTH_SHORT).show();
main_ListView.setAdapter(listAdapter);
return true;
}
});

示例

enter image description here

如有帮助,将不胜感激。 :)

最佳答案

我想通了。

使用 .push() 生成的节点来完成此操作。您需要更改代表您的数据的 POJO(普通旧 java 对象)。只需为 key 添加一个字符串字段。

然后删除现有数据或将关键字段添加到节点中的所有数据。

我使用这段代码,然后在将新对象添加到我的数据库时调用该方法。

private void GenerateKey() {
mKey = mNodeRef.push().getKey();
Log.i(TAG, "NODE CHILD KEY: " + mKey);
if(mKey.equalsIgnoreCase(mKey)){
mKey = mNodeRef.push().getKey();
Log.i(TAG, "NEW NODE CHILD KEY: " + mKey);
}
}

然后在我删除的地方,我用这段代码。具体来说,我是通过长按删除,然后让用户选择是否删除它。

main_ListView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> parent, View view, final int position, long id) {
//Dialog
final AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(MainActivity.this);
dialogBuilder.setTitle("Please Don't Kill Me!");
dialogBuilder.setPositiveButton("Kill Me...", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
//Delete node
Log.d(TAG, "NUM of NODES: " + listAdapter.mNodes.size());
NodeObject pos = listAdapter.mNodes.get(position);
Log.i(TAG, "Generated Key: " + mKey);
String selectedKey = listAdapter.mNodes.get(position).getKey();
Log.i(TAG, "Selected Key: " + selectedKey);
mNodeRef.child(selectedKey).removeValue();
listAdapter.mNodes.remove(pos);
listAdapter.notifyDataSetChanged();
main_ListView.setAdapter(listAdapter);
Toast.makeText(getApplicationContext(), "Node has been murdered terribly."
, Toast.LENGTH_SHORT).show();
}
}).
setNegativeButton("Don't Kill Me!", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(getApplicationContext(), "Fine, ya coward."
, Toast.LENGTH_SHORT).show();
}
});

dialogBuilder.create().show();
/*
Toast.makeText(getApplicationContext(),"Item at: " + position,Toast.LENGTH_SHORT).show();
Toast.makeText(getApplicationContext(), "" +listAdapter.mNodes.size(), Toast.LENGTH_SHORT).show();*/


return true;
}
});

最后,我调用了添加节点的方法。

private void addNode() {
//Get generated key set it to the node
GenerateKey();
Log.i(TAG, "ADD NODE GENERATED KEY: " + mKey);
.......
}

关于android - 使用 Push() 生成的 Android 删除 Firebase 数据库中的单个项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37912491/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com