如何从ListView中删除项目,并在Firebase上同时删除?

huangapple go评论84阅读模式
英文:

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(&quot;MataKuliah&quot;);

This is the code to delete an item:

listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
                @Override
                public boolean onItemLongClick(AdapterView&lt;?&gt; 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:

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&lt;?&gt; 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(&quot;1595588052231&quot;).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(&quot;namaMatkul&quot;).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, &quot;onCancelled&quot;, databaseError.toException());
}});

huangapple
  • 本文由 发表于 2020年7月24日 19:15:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/63072461.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定