如何使用firebase android在多个节点中查询特定的子值 [英] How to query a specific child value in multiple nodes with firebase android

查看:160
本文介绍了如何使用firebase android在多个节点中查询特定的子值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想查询多个节点中的特定子值,以通过FirebaseListAdapter将其填充到列表视图中。
我传递一个firebase参考FirebaseListAdapter方法来听,这是在下面的图片中的红色矩形,
我想填充列表视图的值只有类别键。它们在下图中的绿色矩形中。





我的代码在这里:

  catgoryList =(ListView)findViewById(R.id.catList); 
rootRef = FirebaseDatabase.getInstance()。getReference();
restaurantMenuRef = rootRef.child(menu)。child(restaurantID);

FirebaseListAdapter< String> listAdapter = new FirebaseListAdapter< String>
(this,String.class,R.layout.menu_list_item,restaurantMenuRef){
@Override
protected void populateView(View v,String s,int position){
TextView menuName =(TextView)v.findViewById(R.id.menuName);
menuName.setText(s);
}
};
catgoryList.setAdapter(listAdapter);



$ b您需要重写 FirebaseListAdapter.parseDataSnapshot(),以从中提取正确的值。 $ b

解决方案 DataSnapshot

  FirebaseListAdapter< String> listAdapter = new FirebaseListAdapter< String> 
(this,String.class,R.layout.menu_list_item,restaurantMenuRef){

@Override
protected String parseSnapshot(DataSnapshot snapshot){
return snapshot.child ( 类别)的getValue(String.class);

$ b @Override
protected void populateView(View v,String s,int position){
TextView menuName =(TextView)v.findViewById(R.id .menuName);
menuName.setText(s);
}
};


I want to query a specific child value in multiple nodes, to populate them in a list view by FirebaseListAdapter. I'm passing a firebase reference to FirebaseListAdapter method to listen to, which is in red rectangle in the picture below, And I want to populate in the list view the values of the "category" key only. which are in green rectangle in the picture below.

My code is here:

    catgoryList = (ListView) findViewById(R.id.catList);
    rootRef = FirebaseDatabase.getInstance().getReference();
    restaurantMenuRef = rootRef.child("menu").child(restaurantID);

    FirebaseListAdapter<String> listAdapter = new FirebaseListAdapter<String>
            (this, String.class, R.layout.menu_list_item, restaurantMenuRef ) {
        @Override
        protected void populateView(View v, String s, int position) {
            TextView menuName = (TextView)v.findViewById(R.id.menuName);
            menuName.setText(s);
        }
    };
    catgoryList.setAdapter(listAdapter);

And the layout for the listView containing a TextView to display the category name.

解决方案

You'll need to override FirebaseListAdapter.parseDataSnapshot() to extract the right value from the DataSnapshot:

FirebaseListAdapter<String> listAdapter = new FirebaseListAdapter<String>
        (this, String.class, R.layout.menu_list_item, restaurantMenuRef ) {

    @Override
    protected String parseSnapshot(DataSnapshot snapshot) {
        return snapshot.child("category").getValue(String.class);
    }

    @Override
    protected void populateView(View v, String s, int position) {
        TextView menuName = (TextView)v.findViewById(R.id.menuName);
        menuName.setText(s);
    }
};

这篇关于如何使用firebase android在多个节点中查询特定的子值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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