0

I stored data's in firebase Real Time database and want to retrieve them in shuffled. For example i need to give data's in shuffled for every user uses that application and every time when the user refresh the page it gives randomly shuffled values..

Because the data was more than 1000 + .. so every time giving a new data for user.

code

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

    DatabaseReference reference = FirebaseDatabase.getInstance().getReference().child("Vacancy");

    FirebaseRecyclerOptions<VacancyModel> options = new FirebaseRecyclerOptions.Builder<VacancyModel>()
            .setQuery(reference.orderByChild("companyname"), VacancyModel.class)
            .build();


    FirebaseRecyclerAdapter<VacancyModel, VacancyViewHolder> adapter =
            new FirebaseRecyclerAdapter<VacancyModel, VacancyViewHolder>(options) {

                @Override
                protected void onBindViewHolder(@NonNull VacancyViewHolder holder, int position, @NonNull final VacancyModel model) {




                    holder.vacompany_name.setText(model.getCompanyname());
                    holder.vajob_type.setText("Job Title: " + model.getJob_title());
                    holder.vatiming.setText(model.getTiming());
                    holder.vatotal_vacancy.setText("Total Vacancy: " +model.getTotal_vacancy());
                    holder.vasp.setText(model.getS_p());
                    holder.vaexpected_vasalary.setText("Expected Salary: " +model.getExpexted_salary());
                    holder.vaarea.setText("Area: " +model.getPincode());
                    holder.vaphone_number.setText("Contact Number: " +model.getContact_number());



                    holder.itemView.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {


                            Intent intent = new Intent(Home.this, VacancyViewActivity.class);
                            intent.putExtra("pid", model.getCompanyID());
                            startActivity(intent);

                        }

                    });
                }


                @NonNull
                @Override
                public VacancyViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
                    View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.vacancy_list_item, viewGroup, false);
                    VacancyViewHolder holder = new VacancyViewHolder(view);
                    return holder;
                }

            };

    recyclerView.setAdapter(adapter);
    adapter.startListening();


}

firebase Database

  • @AlexMamo: the [question you linked](https://stackoverflow.com/questions/46798981/firestore-how-to-get-random-documents-in-a-collection) is about Cloud Firestore, while this one is about Realtime Database. While the same mechanisms can be applied to both, it's not a 1:1 duplicate without at least some clarifying comments. – Frank van Puffelen Aug 15 '19 at 14:14
  • You'll typically need to include some random value into each node that you can then order and filter on. Dan's answer to this [question](https://stackoverflow.com/q/46798981) shows how to do that for Firestore, and the same can be applied to the Realtime Database. – Frank van Puffelen Aug 15 '19 at 14:18
  • @FrankvanPuffelen Oh my bad, sorry about that. I have copied and pasted the wrong url. Now should be better. This is the correct **[post](https://stackoverflow.com/questions/50413117/how-to-get-unique-random-product-in-node-firebase/50413208)**. Thanks! – Alex Mamo Aug 15 '19 at 14:18

1 Answers1

1

you are taking wrong RecyclerOptions for this

FirebaseRecyclerOptions use to observe the realtime database thus changes on realtime database is rendered in as sequenced in realtime database

why don't you use basic recyclerview setups like this.

databaseReference.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot snapshot) {

    for (DataSnapshot dataSnapshot : snapshot.getChildren()) {

        StudentDetails studentDetails dataSnapshot.getValue(StudentDetails.class);

        list.add(studentDetails);
    }

    //shuffle it after getting all the data

    Collections.shuffle(list);

    adapter = new RecyclerViewAdapter(ShowStudentDetailsActivity.this, list);

    recyclerView.setAdapter(adapter);

   //got all the data 
}

@Override
public void onCancelled(DatabaseError databaseError) {

    //cancel

}});
Black mamba
  • 462
  • 4
  • 15