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

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

问题描述

我有一个Firebase DB,使用 push()方法将条目添加到DB,从而生成随机密钥作为每个条目的引用。 / p>

在部分应用程序中,我使用FirebaseListAdapter将一些数据库条目填充到列表视图中。然后我在我的列表上实现 setOnItemClickListener

  // Firebase数据库设置$ b $我要在此处执行的操作是返回每个条目的参考信息。 b mDatabaseStolen = FirebaseDatabase.getInstance()。getReference()。child(testing); 

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

DatabaseReference itemRef = mDatabaseStolen.getRef();

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

}
});

推送在这里处理:

<$ p $ (测试)。push()。setValue(newBike);

Firebase数据:

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

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





$ b因此当我点击列表中的项目时,返回引用 KTA7xijNrK1SK98rZns 或者任何可能的参数。在第一个代码片段的吐司里,我要返回的是每个单击父引用的测试字符串。

>。如果是这样的话,你可以在 adapter.getRef()的位置获得对特定项目的引用。

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

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


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.

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();

        }
    });

Push being handled here:

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

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
}

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.

Any help appreciated.

解决方案

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.getItem(position);

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

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

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