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

时间:2016-06-01 03:27:16

标签: android firebase firebase-realtime-database firebaseui

我想查询多个节点中的特定子值,以便通过FirebaseListAdapter在列表视图中填充它们。 我将firebase引用传递给FirebaseListAdapter方法进行监听,如下图所示为红色矩形, 我想在列表视图中填充"类别"的值。只有关键。在下图中为绿色矩形。

enter image description 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);

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

1 个答案:

答案 0 :(得分:3)

您需要覆盖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);
    }
};