gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:20:54 25 4
gpt4 key购买 nike

我想查询多个节点中的特定子值,以通过 FirebaseListAdapter 在 ListView 中填充它们。我将一个 firebase 引用传递给 FirebaseListAdapter 方法来监听,它在下图中的红色矩形中,并且 我只想在 ListView 中填充“类别”键的值。下图中的绿色矩形。

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 的布局。

最佳答案

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

关于android - 如何使用firebase android查询多个节点中的特定子值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37558909/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com