How to generate random Firebase items within a FirebaseRecyclerAdapter - Android Studio

0

I am having difficulty generating random items received by Firebase, I have read the documentation but I was confused.

I want to randomize items within FirebaseRecyclerAdapter according to the number of children entered.

Ex: if I have 3 items entered, it will randomize the 3's. But if I enter 10 items in Firebase, the random limit extends to 10. What is the best way to implement?

privatestaticContextmContext;DatabaseReferencereferencia;RecyclerViewlistaEmpresa;privatestaticStringempresa;//SearchViewsearchView;ImageViewbaner1;MainActivitymain=newMainActivity();DatabaseReferencedatabaseReferencia=FirebaseDatabase.getInstance().getReference();DatabaseReferenceemergenciaRef=databaseReferencia.child("Categorias").child("Emergencia"); //ok
DatabaseReference drogariaRef = databaseReferencia.child("Categorias").child("Drogaria");//ok
DatabaseReference hospitalRef = databaseReferencia.child("Categorias").child("Hospital");//ok
DatabaseReference alimentacaoRef = databaseReferencia.child("Categorias").child("Alimentacao");//ok
DatabaseReference supermercadoRef = databaseReferencia.child("Categorias").child("Supermercado"); //ok
DatabaseReference barRef = databaseReferencia.child("Categorias").child("Bar");//ok
DatabaseReference taxiRef = databaseReferencia.child("Categorias").child("Taxi"); //ok
DatabaseReference servicosRef = databaseReferencia.child("Categorias").child("Servicos");//ok
DatabaseReference postoDeGasolinaRef = databaseReferencia.child("Categorias").child("PostoDeGasolina");//ok
DatabaseReference saudeBelezaRef = databaseReferencia.child("Categorias").child("SaudeBeleza");//ok
DatabaseReference modaRef = databaseReferencia.child("Categorias").child("Moda");//ok
DatabaseReference comercioRef = databaseReferencia.child("Categorias").child("Comercio");//ok
DatabaseReference escolaRef = databaseReferencia.child("Categorias").child("Escola");//ok
DatabaseReference papelariaRef = databaseReferencia.child("Categorias").child("Papelaria");//ok
DatabaseReference prefeituraRef = databaseReferencia.child("Categorias").child("Prefeitura");





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

    baner1 = (ImageView)findViewById(R.id.baner);
    mContext = this;






    listaEmpresa = (RecyclerView)findViewById(R.id.recyclerv_view);
    listaEmpresa.setHasFixedSize(true);
    listaEmpresa.setLayoutManager(new LinearLayoutManager(this));


    //pega categoria do mainActivity
    Bundle bundle = getIntent().getExtras();
    if(bundle != null) {

        int categoriaEscolhida = bundle.getInt("categoria");


        switch (categoriaEscolhida){


            case 0 :{


              //  Query randomData = emergenciaRef.orderByChild("id").startAt(gerarRandom()).limitToFirst();

                referencia = emergenciaRef;


            }
            break;

            case 1:{

                referencia = drogariaRef;


            }
            break;

            case 2:{

                referencia = hospitalRef;

            }
            break;


            case 3:{

                referencia = alimentacaoRef;

            }
            break;

            case 4:{

                referencia = supermercadoRef;

            }
            break;

            case 5:{

                referencia = barRef;

            }
            break;

            case 6:{

                referencia = taxiRef;

            }
            break;

            case 7:{

                referencia = servicosRef;

            }
            break;

            case 8:{

                referencia = postoDeGasolinaRef;

            }
            break;

            case 9:{

                referencia = saudeBelezaRef;

            }
            break;

            case 10:{

                referencia = modaRef;

            }
            break;


            case 11:{

                referencia = comercioRef;

            }
            break;


            case 12:{

                referencia = escolaRef;

            }
            break;


            case 13:{

                referencia = papelariaRef;

            }
            break;


            case 14:{

                referencia = prefeituraRef;

            }
            break;

        }


    }



}

private int gerarRandom(int i) {
    return 0;
}


@Override
protected void onStart() {
    super.onStart();

    FirebaseRecyclerAdapter<Empresas,FirebaseViewHolder> firebaseRecyclerAdapter = new FirebaseRecyclerAdapter<Empresas, FirebaseViewHolder>(

                Empresas.class,
                R.layout.layout_listaitem,
                FirebaseViewHolder.class,
                referencia){


            @Override
                    protected  void populateViewHolder(FirebaseViewHolder viewHolder, final Empresas emp, int position){

                    viewHolder.setImg(getApplicationContext(), emp.getFoto());
                    viewHolder.setNomeDaEmpresa(emp.getNomeDaEmpresa());
                    viewHolder.setEnd(emp.getEndereco());
                    viewHolder.setTel(emp.getTell());



                viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {

                        Intent intent = new Intent(mContext,EmpresaDescricao.class);

                        intent.putExtra("foto" ,emp.getFoto());
                        intent.putExtra("nomeDaEmpresa",emp.getNomeDaEmpresa());
                        intent.putExtra("endereco",emp.getEndereco());
                        intent.putExtra("tell",emp.getTell());
                        intent.putExtra("localizacao",emp.getLocal());
                        intent.putExtra("site",emp.getSite());
                        intent.putExtra("descricao",emp.getDesc());
                        intent.putExtra("foto1",emp.getFoto1());
                        intent.putExtra("foto2",emp.getFoto2());
                        intent.putExtra("foto3",emp.getFoto3());
                        intent.putExtra("foto4",emp.getFoto4());

                        mContext.startActivity(intent);

                    }
                });







            }
       };

    listaEmpresa.setAdapter(firebaseRecyclerAdapter);
}
public  static class FirebaseViewHolder extends RecyclerView.ViewHolder {
View mView;

public FirebaseViewHolder(View itemView) {
    super(itemView);
    mView = itemView;
}

     private void intent() {


        Intent intent = new Intent(mContext, EmpresaDescricao.class);

        mContext.startActivity(intent);

    }


    public void setNomeDaEmpresa(String nomeDaEmpresa) {
    TextView nomDaEmpresa = (TextView) mView.findViewById(R.id.nomeDaEmpresa);

    nomDaEmpresa.setText(nomeDaEmpresa);
}


public void setImg(Context ctx, String image) {

    ImageView foto = (ImageView) mView.findViewById(R.id.image);
    Picasso.with(ctx).load(image).into(foto);
}

public void setEnd(String endereco){
    TextView end =(TextView)mView.findViewById(R.id.endereco);

    end.setText(endereco);
}



public void setTel(String tell){
    TextView tel =(TextView)mView.findViewById(R.id.telefone);

    tel.setText(tell);
}}

public void banerClick(View view) {

    Intent intent = null, chooser = null;


    intent = new Intent(Intent.ACTION_VIEW);
    intent.setData(Uri.parse(main.questionario));
    chooser = Intent.createChooser(intent,"Launch browsers");
    startActivity(chooser);
}}
    
asked by anonymous 19.05.2018 / 16:54

0 answers