从 FirebaseListAdapter 中的 onItemClick 检索 Firebase 密钥 [英] Retrieving the Firebase key from onItemClick in FirebaseListAdapter

查看:27
本文介绍了从 FirebaseListAdapter 中的 onItemClick 检索 Firebase 密钥的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 Firebase 数据库,我正在使用 push() 方法向数据库添加条目,从而生成随机密钥作为每个条目的参考.

I have a Firebase DB which I'm using the push() method to add entries to DB, thus generating the random key as a reference for each entry.

在应用程序的一部分中,我使用 FirebaseListAdapter 将一些数据库条目填充到列表视图中.然后我在我的列表中实现 setOnItemClickListener.我想在这里做的是返回我点击的每个条目的引用.

In part of the application I'm using a FirebaseListAdapter to populate some DB entries into a list view. Then I'm implementing setOnItemClickListener on my list. What I want to do here is to return the reference for each entry I click on.

    //Firebase DB setup
    mDatabaseStolen =       FirebaseDatabase.getInstance().getReference().child(testing);

    myListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {

            DatabaseReference itemRef = mDatabaseStolen.getRef();

            Toast toast = Toast.makeText(getActivity().getApplicationContext(), itemRef.getKey(), Toast.LENGTH_SHORT);
            toast.show();

        }
    });

这里正在处理的推送:

 mDatabase.child("testing").push().setValue(newBike);

Firebase 数据:

Firebase data:

"testing" : {  
 "-KTA7xijNrK1SK98rZns" : {     
  "color" : "Rjfu",      
  "frameSize" : 0,         
  "make" : "Ggg",     
  "other" : "Xhfhf",     
  "stolen" : false
},

"-KTABiJuH2-RXwmp0Rcu" : {   
"color" : "Red",     
"frameSize" : 0,      
"make" : "Test ",      
"other" : "No",      
"stolen" : false
}

因此,当我单击列表中的项目时,我希望返回引用 KTA7xijNrK1SK98rZns 或任何可能的引用.在第一个代码片段的 toast 中,我返回的只是每次单击父引用中的 testing 字符串.

So when I click on the item in the list, I wish to return the reference KTA7xijNrK1SK98rZns or whatever it may be. In the toast in the first code snippet, all I'm returning is the testing string in each click the parent reference.

感谢任何帮助.

推荐答案

你说你有一个 FirebaseListAdapter.如果是这种情况,您可以使用 adapter.getRef() 在某个位置获取对特定项目的引用.

You say you have a FirebaseListAdapter. If that is the case, you can get a reference to a specific item at a position with adapter.getRef().

myListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
        DatabaseReference itemRef = adapter.getRef(position);

        Toast toast = Toast.makeText(getActivity().getApplicationContext(), itemRef.getKey(), Toast.LENGTH_SHORT);
        toast.show();
    }
});

这篇关于从 FirebaseListAdapter 中的 onItemClick 检索 Firebase 密钥的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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