长按上的ListView中的Firebase Android删除节点键 [英] Firebase Android delete node key in ListView on longpress

查看:60
本文介绍了长按上的ListView中的Firebase Android删除节点键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

试图进行大量搜索,但是两天后,我仍然对这个问题感到困惑. 我的Firebase中有以下数据库,该数据库填充在ListView中. 对于ListView中的每一行,都会显示data1和data2.

Tried to search a lot but after 2 days I'm still stucked on this problem. I have the following database in my Firebase which is populated in my ListView. For each row in the ListView is displayed data1 and data2.

 test-3db7e
  |
  +---users
       |
       +---W9KkXAidmHgyOpyeQPeT5YxgQI42
            |
            +---data
            |    |
            |    +----LIv-OeQdixT0q6NZ-jL
            |    |     |
            |    |     +---data1: "Data1"
            |    |     |
            |    |     +---data2: "Data2"
            |    |
            |    +----LIv-R3PRKaEHaAcMjWu
            |          |
            |          +---data1: "Data1"
            |          |
            |          +---data2: "Data2"
            |
            +---name: "The user's name"

直到现在一切顺利,我可以从我的应用上传数据,并且列表视图与Firebase控制台完全同步.

Until now it all seams ok, I can upload data from my app and the listview syncs perfectly with the Firebase console.

我使用以下代码制作了ListView:

I made the ListView using the folling code:

    //Updating the listview
    databaseReference.child("users").child(user).child("data").addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(@NonNull DataSnapshot dataSnapshot) {

            //Getting all the children at this level
            Iterable<DataSnapshot> children = dataSnapshot.getChildren();
            dataArrayAdapter.clear();

            for (DataSnapshot child : children) {

                Data data = child.getValue(Data.class);
                dataList.add(data);
                dataArrayAdapter.notifyDataSetChanged();

            }
        }

        @Override
        public void onCancelled(@NonNull DatabaseError databaseError) {

        }
    });

我还创建了Data.class来将先前代码中的值存储到列表视图中.

Also I created the Data.class to store the values from the previous code to the listview.

    package com.mypackge.firebase;

    public class Data {

        private String data1;
        private String data2;

        public String getData1() {
            return data1;
        }

        public void setData1(String data1) {
            this.data1 = data1;
        }

        public String getData2() {
            return data2;
        }

        public void setData2(String data2) {
            this.data2 = data2;
        }

        public String toString() {
            return data1 +"\n"+data2;
        }
    }

现在的问题是以下代码,长按要删除的行(如果在对话框中按是),长按要删除的行.

The problem now is with the following code, on long press on the list view I want to remove (if pressed yes on the dialog) the row that was pressed.

     /**
     *
     * Deleting data on long press
     *
     */

    //Creating a dialog interface
    final DialogInterface.OnClickListener dialogClickListner = new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialogInterface, int i) {

            if(i==DialogInterface.BUTTON_POSITIVE){

            //    WHAT TO DO HERE?????

            }else if(i== DialogInterface.BUTTON_NEGATIVE){


            }

        }
    };

    //Creating the Item on Click Listner
    lstViewData.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
        @Override
        public boolean onItemLongClick(AdapterView<?> adapterView, View view, int position, long l) {
            //Creating the alert dialog
            AlertDialog.Builder builder = new AlertDialog.Builder(adapterView.getContext());
            builder.setMessage("Do you want to delete?")
                    .setPositiveButton("Yes", dialogClickListner)
                    .setNegativeButton("No",dialogClickListner).show();

            //    WHAT TO DO HERE?????

            return false;
        }
    });

谢谢大家的时间!!!

Thank you all for your time!!!

推荐答案

解决此问题的关键是将在创建Data类的对象时生成的pushed key添加到数据库中.因此,在data1data2字段旁边,添加另一个名为key的字段(也是String类型).请同时为该新字段添加相应的setter和getter.

The key for solving this problem is to add the pushed key that is generated when you are creating an object of Data class to the database. So beside data1 and data2 fields, add another field named key (also of type String). Please also add the corresponding setter and getter for this new field.

现在,这是实际上需要在对象上创建的方式:

Now, this is how you need actually to create on object:

DatabaseReference dataRef = databaseReference.child("users").child(user).child("data");
String key = dataRef.push().getKey();
Data data = new Data();
data.setData1("data1");
data.setData2("data2");
data.setKey(key);
dataRef.child(key).setValue(data);

现在,您的Data对象具有key字段,该字段填充了数据库中的实际键.要显示数据并删除单击的特定对象,请使用以下代码:

Now your Data object has the key field populated with the actual key from the database. To display the data and to remove a particular object that was clicked, please use the following code:

ValueEventListener valueEventListener = new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        ArrayList<Data> list = new ArrayList<>();
        for(DataSnapshot ds : dataSnapshot.getChildren()) {
            Data data = ds.getValue(Data.class);
            list.add(data);
        }
        ListView listView = (ListView) findViewById(R.id.list_view);
        ArrayAdapter<Data> arrayAdapter = new ArrayAdapter<>(getApplicationContext(), android.R.layout.simple_list_item_1, list);
        listView.setAdapter(arrayAdapter);

        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                AlertDialog.Builder builder = new AlertDialog.Builder(getApplicationContext());
                builder.setMessage("Do you want to delete?");

                builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int i) {
                        String key = arrayAdapter.getItem(i).getKey();
                        dataRef.child(key).removeValue();
                    }
                });

                builder.setNegativeButton("No", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int i) {
                        dialog.dismiss();
                    }
                });

                AlertDialog dialog = builder.create();
                dialog.show();
            }
        });
    }

    @Override
    public void onCancelled(@NonNull DatabaseError databaseError) {}
};
dataRef.addListenerForSingleValueEvent(valueEventListener);

这篇关于长按上的ListView中的Firebase Android删除节点键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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