4

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

4

1 回答 1

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);
    }
};
于 2016-06-01T04:07:27.147 回答