List knots from firebase database

0

I want to list from the firebase database 2 knots: I have a knot that I call Restaurant and another Categories. In Categories I have two different knots where I put 2 knots children who in turn have children. I want to list in a recycleview the elements of each restaurant and I do not know how to do it. I want to list it by its id of the restaurant that I also have it in my database Category.

This is my code

AdapterCategory

package com.example.joserayo.myrestaurantev3.View;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.joserayo.myrestaurantev3.Model.Categorias;
import com.example.joserayo.myrestaurantev3.R;
import com.squareup.picasso.Picasso;

import java.util.List;


public class AdapterCategorias extends RecyclerView.Adapter<AdapterCategorias.categorias> {
    private List<Categorias>cate;

    private Context context1;
    private RecyclerView recyclerView;

    public  AdapterCategorias(Context context,List<Categorias> categoria){
        context1= context;
        cate=categoria;

    }

    @NonNull
    @Override
    public categorias onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view= LayoutInflater.from(parent.getContext()).inflate(R.layout.item_categoria,parent,false);
        categorias categorias=new categorias(view);

        return categorias;

    }

    @Override
    public void onBindViewHolder(@NonNull categorias holder, int position) {
        Categorias categ=cate.get(position);
        holder.nombre.setText(categ.getNombre());
        holder.descripio.setText(categ.getDescripcion());
        holder.precio.setText(categ.getPrecio());
        Picasso.with(context1).load(categ.getUrl()).fit()
                .into(holder.foto);

    }

    @Override
    public int getItemCount() {
        return cate.size();
    }

    public static  class categorias extends RecyclerView.ViewHolder{

        TextView nombre, descripio,precio;
        ImageView foto;
        public categorias(View itemView) {

            super(itemView);

            nombre=(TextView)itemView.findViewById(R.id.nom);
            descripio=(TextView)itemView.findViewById(R.id.descr);
            precio=(TextView)itemView.findViewById(R.id.pre);
            foto=(ImageView)itemView.findViewById(R.id.imagenlistar);

        }
    }



}

This is my activity to list

package com.example.joserayo.myrestaurantev3.View;

import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.View;
import android.widget.Toast;

import com.example.joserayo.myrestaurantev3.Model.Categorias;
import com.example.joserayo.myrestaurantev3.Model.CategoriasHolder;
import com.example.joserayo.myrestaurantev3.R;
import com.firebase.ui.database.FirebaseRecyclerAdapter;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.Query;
import com.google.firebase.database.ValueEventListener;

import java.util.ArrayList;
import java.util.List;

public class ListarCategorias extends AppCompatActivity  {
private RecyclerView recyclerView;
    String foodid="";
  RecyclerView.LayoutManager manager;
List<Categorias>list;
    DatabaseReference reference;
    AdapterCategorias adapter;
    private Query productQuery;

FirebaseRecyclerAdapter<Categorias,CategoriasHolder>  recyclerAdapter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_listar_categorias);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
reference=FirebaseDatabase.getInstance().getReference();
        recyclerView = (RecyclerView) findViewById(R.id.myreclicleview);
         recyclerView.setHasFixedSize(true);
         manager=new LinearLayoutManager(this);
         recyclerView.setLayoutManager(manager);


        Bundle bundle = this.getIntent().getExtras();
        String idres = bundle.getString("idres");
        Log.d("Idrestau", "" + idres);
            listar(idres);
list=new ArrayList<>();



    }
    private void listar(String idres) {
        productQuery = reference.child("Categorias/bebidas")
                .orderByChild("idRestaurante").equalTo(idres);


        productQuery.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                for (DataSnapshot data:dataSnapshot.getChildren()){
                Categorias categorias= data.getValue(Categorias.class);
                    list.add(categorias);
                }
                adapter=new AdapterCategorias(ListarCategorias.this,list);
                recyclerView.setAdapter(adapter);

            }

            @Override
            public void onCancelled(DatabaseError databaseError) {
                Toast.makeText(ListarCategorias.this,databaseError.getMessage(),Toast.LENGTH_LONG);
            }
        });

                Log.d("encontrado",""+idres);

    }

firebase database knot category

database of my restaurant:

    
asked by Deyvis Garcia Cercado 16.06.2018 в 00:01
source

1 answer

0

Remember that Firebase is a NoSql database, so you should not think about the structure of it in a relational way.

To solve your problem you should restructure your table so that inside restaurants you create another node with the category of it, that way you would stay

-Restaurante Fusion
      +--Categoria

This way you can access the restaurant data and the data of the category where it belongs, without having to relational two knots and make more references

    
answered by 16.06.2018 в 18:15