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

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

我想查询多个节点中的特定子值,以通过 FirebaseListAdapter 在列表视图中填充它们。 我将一个 firebase 引用传递给 FirebaseListAdapter 方法来监听,它在下图中的红色矩形中, And 我只想在列表视图中填充 "category" 键的值。下图中的绿色矩形。

我的代码在这里:

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