How to query a specific child value in multiple no

2019-06-28 01:18发布

I want to query a specific child value in multiple nodes, to populate them in a list view by FirebaseListAdapter. I'm passing a firebase reference to FirebaseListAdapter method to listen to, which is in red rectangle in the picture below, And I want to populate in the list view the values of the "category" key only. which are in green rectangle in the picture below.

enter image description here

My code is 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);

And the layout for the listView containing a TextView to display the category name.

1条回答
我命由我不由天
2楼-- · 2019-06-28 02:16

You'll need to override FirebaseListAdapter.parseDataSnapshot() to extract the right value from the 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);
    }
};
查看更多
登录 后发表回答