单击listview时,将其删除并在firebase中删除与其关联的数据 [英] When click on listview , delete it and delete associated data with it in firebase

查看:89
本文介绍了单击listview时,将其删除并在firebase中删除与其关联的数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,感谢您的帮助 我在这里问过如何从Firebase轮询listview,并庆幸的是Krishna Kishan解决了我的问题,但是现在我想做的是单击listview项目时,该项目将被删除,好的,我做了这部分,但是我的问题是我想要从Firebase数据中删除或删除列表视图中与该特定项目相关的数据,这是我的代码

Hello and thanks for help I have asked here how to Polpulate listview from firebase and thankfully Krishna Kishan solved my problem but now what I like to do is when I click on listview items, that item will be removed, that is ok I done this part but my problem is I want to delete or remove associated data with that particular item in the list view from firebase data here is my code

  listi.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            items.remove(position);
            adapt.notifyDataSetChanged();
            myRef.getRoot().child("message").removeValue();
        }
    });

但是这行代码从firebase中删除了所有内容,但我只希望删除与listview项目相关的数据.

But this line of code remove everything from firebase, but I want only remove the associated data with listview item.

myRef.getRoot().child("message").removeValue();

我将说明更多.假设每次点击时,我都会向Firebase添加一个值,然后说1,然后再加上2,然后再加上3,所以我的listview项将分别为1,2和3 现在,当我在列表视图上单击2时,我也想从列表视图和firebase中删除2并保留1和2,但是我不想按名称调用它,因为如果我有很大的列表视图,可以说我想要一个用户20个项目单击任何项​​目,然后将它们从列表和Firebase中删除,因此下次他们重新启动该应用程序时,列表视图将不会显示,因为它已从Firebase中删除

I illustrate more. Let ssay on each click I add a value to a firebase let say 1, then I add 2 then 3, so my listview items will be 1,2 and 3 now when I click on 2 on listview I want to delete 2 from listview and from firebase as well and leave 1 and 2 , but I do not want to call it by name because if I have large listview let say 20 items I want a user click on any items then remove them from list and firebase so next time when they relaunch the app that list view will not show because it has removed from firebase

这是我的firebase数据库结构:

Here's my firebase database structure:

推荐答案

您必须修改一些您发布的代码段之外的代码.

You have to modify some code that is outside the snippet you posted.

我想您的items是从数据库中读取的.当您读取消息"节点时,必须保存键和值. 您可以通过多种方式实现这一目标.例如

I guess your items are read from the database. When you read your "message" node you have to save keys as well as values. You can achieve this in a number of ways. e.g.

final ArrayList<String> keyList = new ArrayList<>();
final ArrayList<String> items = new ArrayList<>();
myRef.getRoot().child("messages")
        .addListenerForSingleValueEvent(new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        for (DataSnapshot messages : dataSnapshot.getChildren()) {
            keyList.add(messages.getKey());
            items.add(messages.getValue(String.class));
        }
    }
    @Override
    public void onCancelled(DatabaseError databaseError) {
        /*handle errors*/
    }
});

现在您既有键又有值.以此方式修改您的OnClickListener:

Now you have both keys and values. Modify your OnClickListener this way:

listi.setOnItemClickListener(new AdapterView.OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        items.remove(position);
        adapt.notifyDataSetChanged();
        //new code below
        myRef.getRoot().child("message").child(keyList.get(position)).removeValue();
        keyList.remove(position);
    }
});

显然,当同时填充keyListitems时,应调用listi.setOnItemClickListener( ... ).请注意,ValueEventListener是异步的.

Obviously listi.setOnItemClickListener( ... ) should be called when both keyList and items are properly filled. Please notice that ValueEventListener is asynchronous.

这篇关于单击listview时,将其删除并在firebase中删除与其关联的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆