简体   繁体   中英

Firebase Database accessing nodes from child android

i have a node like this

在此处输入图片说明

I search for the id node in this case 01, 02,... when i find it i send it to another activity, in that activity i search for matching id numbers like this 在此处输入图片说明

when the Id matches i display them in a recycler view, now i want to get the parent of the node that i am searching for in this case Restaurant--> 01 but whenever i call my database.getKey it returns Restaurant not 01

My Question is how can i get the parent node instead of the origin node or(parent of parent node)? Many thanks Already

Heres my code:

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

}

}

To get the key, you need to loop inside the 01 - 02 to be able to retrieve them when you use getKey() , therefore try the following:

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

it was a struggle but i got it haha, thanks for everyone who tried. I used my item click listener to get reference to the position on the view and then attach my adapter to the position as a ref and from there i can just use getKey.

something like this works :)

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

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