繁体   English   中英

如何使用firebase android查询多个节点中的特定子值

[英]How to query a specific child value in multiple nodes with firebase android

我想查询多个节点中的特定子值,以便通过FirebaseListAdapter在列表视图中填充它们。 我正在通过firebaseListAdapter方法的firebase引用来监听,这是在下面的图片中的红色矩形, 并且我想在列表视图中填充“类别”键的值。 在下图中为绿色矩形。

在此输入图像描述

我的代码在这里:

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

并且包含TextView的listView的布局显示类别名称。

您需要覆盖FirebaseListAdapter.parseDataSnapshot()以从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);
    }
};

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM