【问题标题】:How to query a specific child value in multiple nodes with firebase android如何使用firebase android查询多个节点中的特定子值
【发布时间】:2016-09-30 05:54:04
【问题描述】:

我想查询多个节点中的特定子值,以便通过 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);

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

【问题讨论】:

    标签: android firebase firebase-realtime-database firebaseui


    【解决方案1】:

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

    【讨论】:

    • dataSnapshots 在哪里传递?
    • 是的,很抱歉。我更新了代码以匹配公共 API。
    • 它工作正常,SQL“WHERE category = 'drinks'”的替代方法是什么?
    猜你喜欢
    • 1970-01-01
    • 2017-09-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-31
    • 1970-01-01
    • 2016-08-04
    相关资源
    最近更新 更多