简体   繁体   中英

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

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.

在此输入图像描述

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM