如何在与FirebaseListAdapter一起使用的ListView中删除当前选定的项目? [英] How to remove currently selected item in ListView that works with FirebaseListAdapter?

查看:86
本文介绍了如何在与FirebaseListAdapter一起使用的ListView中删除当前选定的项目?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个Android应用,并且已经使用Firebase实现了身份验证.登录后,在我的NavigationDrawerActivity中,我有一个ListView,我已经设置了FirebaseListAdapater来获取用户特定的数据.

I am developing an Android app, and I have already implemented authentication with Firebase. After logging in, in my NavigationDrawerActivity I have a ListView that I had set a FirebaseListAdapater to that gets user specific data.

在我的onCreate()方法中,我有以下内容:

In my onCreate() method I have the following:

        firebaseAuth = FirebaseAuth.getInstance();
        FirebaseUser localUser = firebaseAuth.getCurrentUser();
        String localUserEmail = localUser.getEmail();
        String localUserEmailUniqueID = getEmailAddressUniqueID(localUserEmail);
        String dbUsersRoot = "https://DATABASENAME.firebaseio.com/Users/";
        String dbUniqueRefForCurrentUser = dbUsersRoot + localUserEmailUniqueID +"/parishioners";

        DatabaseReference databaseReferenceCurrentUser = FirebaseDatabase.getInstance().getReferenceFromUrl(dbUniqueRefForCurrentUser);

        final FirebaseListAdapter<String> firebaseListAdapter = new FirebaseListAdapter<String>(
                this,
                String.class,
                android.R.layout.simple_list_item_2,
                databaseReferenceCurrentUser
        ) {
            @Override
            protected void populateView(View v, String model, int position) {
                TextView textView = (TextView) v.findViewById(android.R.id.text1);
                    textView.setText(model);
                }
            };

        mListView.setAdapter(firebaseListAdapter);

现在,在我的onStart()方法中,我已经向按钮添加了setOnClickListener,该按钮希望将新项目添加到列表中,如下所示:

Now, in my onStart() method I have added an setOnClickListener to a button that wishes to add a new item into my list , like this :

@Override
    protected void onStart(){
        super.onStart();

        mButtonAdd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                firebaseAuth = FirebaseAuth.getInstance();
                FirebaseUser localUser = firebaseAuth.getCurrentUser();
                String localUserEmail = localUser.getEmail();
                String localUserEmailUniqueID = getEmailAddressUniqueID(localUserEmail);
                String dbUsersRoot = "https://DATABASENAME.firebaseio.com/Users/";
                String dbUniqueRefForCurrentUser = dbUsersRoot + localUserEmailUniqueID +"/parishioners";
                DatabaseReference databaseReferenceCurrentUser = FirebaseDatabase.getInstance().getReferenceFromUrl(dbUniqueRefForCurrentUser);

                databaseReferenceCurrentUser.push().setValue(editTextNewMember.getText().toString().trim());
            }
        });
}

单击添加"按钮后,我将在数据库中输入以下内容(假设EditText中包含字符串"capuccino"):

On clicking the Add Button, I'll have in my database something like this (supposing the EditText has in it the string 'capuccino'):

"luminittta" : {
      "parishioners" : {
        "-KX1GcEWPMy7oFWgt_3h" : "capuccino"

现在,我的列表将显示"capuccino"(因为它是实时数据库,因此我的列表将自动使用新添加的值进行更新).

Now, my List will show 'capuccino' (since it's a realtime database my list will automatically get updated with the newly added value).

我的问题是: 接下来,我如何实现删除功能,当用户长时间单击ListView中的某个项目时,该项目将被删除?

My question is: How can I implement next a delete functionality that when the user long clicks an item from the ListView it gets removed?

整个数据库如下:

{
  "Users" : {
    "davidivanmircea" : {
      "parishioners" : {
        "-KX1FNcgSXgsMNz6QRIS" : "Gus Crick",
        "-KX1FVIzzPM4nMrMvm9x" : "Florentino Williams",
        "-KX1FZ27M-KS5TQZQ76u" : "Marc Raff",
        "-KX1FbercKpPrl9r9tb4" : "Frederick Haddon",
        "-KX1FgguOlRRN4sZKS8Z" : "Cristobal Wolfe",
        "-KX1FkgwifQLIHplsZNx" : "Scott Nodal",
        "-KX1Fp64Sa2QD94GR9uK" : "Odis Nevers",
        "-KX1FrYK5PeuBucEiamY" : "Chauncey Mossman",
        "-KX1FvIBDSrvppC3e4Ip" : "Alfonso Ignacio",
        "-KX1Fy7dEHH8Z9JV-BRL" : "Douglas Hettinger",
        "-KX1WdtbV4PEiscJV6E_" : "Daniel Muresan",
        "-KX1WvYqekJZdWS6wGh6" : "Angelina Jolie"
      }
    },
    "luminittta" : {
      "parishioners" : {
        "-KX1GYuSsgmHyGk5LQLA" : "mocha",
        "-KX1G_Q130sWitNGzlAR" : "triplo",
        "-KX1GaPIhuIyhV4vmLGe" : "latte",
        "-KX1GcEWPMy7oFWgt_3h" : "capuccino",
        "-KX1Gf7TKY3N8IQETig-" : "machiatto",
        "-KX1Gh4KoYpcVpCDFI2x" : "cafe melange",
        "-KX1GoCutX3XO-CRWWB3" : "ristretto",
        "-KX1GpvdE5i9UR2ZaX0K" : "americano",
        "-KX1Grb8S5QZu4HT9cuW" : "espresso"
      }
    }
  }
}

非常感谢您的帮助!

PS:我已经在代码中用"DATABASENAME"修改了我的实际数据库名称.

PS: I have modified my actual database name with 'DATABASENAME' inside the code.

推荐答案

要删除LongClick上的特定项目,请在populateView中写入

To remove a specific item on LongClick write this in populateView

v.setOnLongClickListener(new View.OnLongClickListener() {
        @Override
        public boolean onLongClick(View view) {
            getRef(position).removeValue();
            notifyDataSetChanged();
            return true;
        }
    });

这篇关于如何在与FirebaseListAdapter一起使用的ListView中删除当前选定的项目?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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