英文:
How to delete an item from ListView and also delete it on Firebase?
问题
以下是翻译好的内容:
问题在于,如果我尝试在我的ListView
上删除一个项目,所有数据都将从ListView
和我的Firebase实时数据库中删除。我想要的是,如果我长按一个项目,只有该项目会被删除,而不是ListView
上的所有项目。谢谢。任何帮助将不胜感激。
这是我声明的内容:
MatkulAdapter matkulAdapter = null;
Matkul matkul;
DatabaseReference root = FirebaseDatabase.getInstance().getReference();
DatabaseReference dbReference = root.child("MataKuliah");
这是删除项目的代码:
listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
Matkul item = matkulAdapter.getItem(position);
matkulAdapter.remove(item);
dbReference.removeValue();
matkulAdapter.notifyDataSetChanged();
return true;
}
});
这是我的数据库截图:
还有,我尝试了使用.child
的代码,但数据只在ListView
上删除。因此,当我重新打开ListView
时,数据会重新出现。
listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
Matkul item = matkulAdapter.getItem(position);
matkulAdapter.remove(item);
dbReference.child(item.getNamaMatkul()).removeValue();
matkulAdapter.notifyDataSetChanged();
return true;
}
});
英文:
My problem is that if I try to delete an item on my ListView
, all the data will be deleted to on both listview and my firebase realtime database. All I want is, if I long click an item, only that item will be removed, not all of them on the ListView
. Thank you. Any help is appreciated
This is what I declare:
MatkulAdapter matkulAdapter = null;
Matkul matkul;
DatabaseReference root = FirebaseDatabase.getInstance().getReference();
DatabaseReference dbReference = root.child("MataKuliah");
This is the code to delete an item:
listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
Matkul item = matkulAdapter.getItem(position);
matkulAdapter.remove(item);
dbReference.removeValue();
matkulAdapter.notifyDataSetChanged();
return true;
}
});
This is my database:
EDIT QUESTION
and also i tried this code with .child but the data only deleted on ListView. So the data comes back when i re-open the ListView.
listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
Matkul item = matkulAdapter.getItem(position);
matkulAdapter.remove(item);
dbReference.child(item.getNamaMatkul()).removeValue();
matkulAdapter.notifyDataSetChanged();
答案1
得分: 0
当您使用以下代码行时:
dbReference.removeValue();
在MataKuliah
下方的所有内容都将被删除。如果只需要删除1595588052231
子项,则只需使用以下代码行:
dbReference.child("1595588052231").removeValue();
一个更好的方法是将1595588052231
存储为数据库中uid
属性的值,然后使用:
dbReference.child(item.getUid()).removeValue();
英文:
When you are using the following line of code:
dbReference.removeValue();
Everything beneath MataKuliah
will be deleted. If only need to remove the 1595588052231
child, then simply use the following line of code:
dbReference.child("1595588052231").removeValue();
A better approach might be to store the 1595588052231
as a value of a uid
property in your database and then use:
dbReference.child(item.getUid()).removeValue();
答案2
得分: 0
你需要在第二层子节点中传递你的 Firebase 项目 ID(15955880552231),就像这段代码一样:
Query query = rdbReference.orderByChild("namaMatkul").equalTo(item.namaMatkul);
query.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot itemSnapshot: dataSnapshot.getChildren()) {
appleSnapshot.getRef().removeValue();
matkulAdapter.remove(item);
matkulAdapter.notifyDataSetChanged();
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
Log.e(TAG, "onCancelled", databaseError.toException());
}
});
英文:
you need pass your firebase item id (15955880552231) in second child, like this code:
Query query = rdbReference.orderByChild("namaMatkul").equalTo(item.namaMatkul);
query.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot itemSnapshot: dataSnapshot.getChildren()) {
appleSnapshot.getRef().removeValue();
matkulAdapter.remove(item);
matkulAdapter.notifyDataSetChanged();
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
Log.e(TAG, "onCancelled", databaseError.toException());
}});
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论