我有一个像这样的节点
在这种情况下,我搜索id节点01、02 ... 当我找到它时,将其发送到另一个活动,在该活动中,我会搜索匹配的ID号,例如这样
当ID匹配时,我在回收者视图中显示它们,现在我想获取我要搜索的节点的父级,在这种情况下,餐厅-> 01 但是每当我调用我的database.getKey时,它返回的餐厅不是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);
}
}
答案 0 :(得分:0)
要获取密钥,您需要在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) {
}
});
答案 1 :(得分:0)
这是一场斗争,但我明白了,哈哈,谢谢所有尝试过的人。 我使用了我的项单击侦听器来获取对视图位置的引用,然后将适配器作为引用附加到该位置,然后可以从那里使用getKey。
类似的东西:)
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", adapter.getRef(position).getKey());
startActivity(fragmentintent);