gpt4 book ai didi

java - Firebase 数据库从子 android 访问节点

转载 作者:太空宇宙 更新时间:2023-11-04 10:18:08 25 4
gpt4 key购买 nike

我有一个这样的节点

enter image description here

在本例中我搜索 id 节点 01, 02,...当我找到它时,我将其发送到另一个 Activity ,在该 Activity 中我搜索匹配的 ID 号,如下所示 enter image description here

当 Id 匹配时,我将它们显示在回收器 View 中,现在我想获取我在本例中搜索的节点的父节点 Restaurant--> 01但每当我调用我的database.getKey时,它都会返回Restaurant而不是01

我的问题是如何获取父节点而不是原始节点或(父节点的父节点)?非常感谢

这是我的代码:

RecyclerView recyclerView;
RecyclerView.LayoutManager layoutManager;


FirebaseDatabase database;
DatabaseReference restaurantList;

String restaurantId;
FirebaseRecyclerAdapter<RestaurantModel, RestaurantViewHolder> adapter;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_restaurant_list);


//Firebase
database = FirebaseDatabase.getInstance();
restaurantList = database.getReference("Restaurant");

recyclerView = (RecyclerView) findViewById(R.id.recycler_restaurants);
layoutManager = new LinearLayoutManager(this);
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(layoutManager);

if(getIntent() !=null)
restaurantId = getIntent().getStringExtra("RestaurantId");
if(!restaurantId.isEmpty() && restaurantId != null)
{
loadListRestaurant(restaurantId);

}

}




private void loadListRestaurant(final String restaurantId) {
adapter = new FirebaseRecyclerAdapter<RestaurantModel, RestaurantViewHolder>(
RestaurantModel.class,
R.layout.restaurant_menu_item,
RestaurantViewHolder.class,
restaurantList.orderByChild("RestaurantId").equalTo(restaurantId)
) {
@Override
protected void populateViewHolder(RestaurantViewHolder viewHolder, RestaurantModel model, int position) {
viewHolder.restaurantName.setText(model.getName());
Picasso.get().load(model.getImage())
.into(viewHolder.restaurantImage);
viewHolder.restaurantRate.setRating(Float.parseFloat(model.getRating()));
viewHolder.restaurantCategory.setText(model.getDescription());
try {
if (Integer.parseInt(model.getPrice()) == 0)
viewHolder.priceRate.setRating(2);
else
viewHolder.priceRate.setRating(Integer.parseInt(model.getPrice()));
}
catch (Exception e){}




final RestaurantModel local = model;
viewHolder.setItemClickListener(new ItemClickListener() {
@Override
public void onClick(View view, int position, boolean isLongClick) {
Intent fragmentintent = new Intent(RestaurantList.this, main_intermediate.class);
fragmentintent.putExtra("RestaurantId", restaurantList.getKey());
Log.d(TAG, restaurantList.getKey() );
startActivity(fragmentintent);
}
});
}
};
recyclerView.setAdapter(adapter);

}

}

最佳答案

要获取 key ,您需要在 01 - 02 内部循环,以便在使用 getKey() 时能够检索它们,因此请尝试以下操作:

DatabaseReference reference = FirebaseDatabase.getInstance().getReference("Restaurant");

reference.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for(DataSnapshot datas: dataSnapshot.getChildren()){
String keys=datas.getKey();
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});

关于java - Firebase 数据库从子 android 访问节点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51457214/

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