0

In my app the user should be able to choose from 3 different categories (out of 8) which are randomly generated. For this I used the approach described here.

I don't know how to use this approach to use the retrieved documents to insert them into the TextViews in my Category Activity. I created a category class to store the retrieved documents from Firestore but I also don't know if that is even the right approach. I would very much appreciate every tip!

Here is my Category Activity where the 3 documents get retrieved and in the end should be displayed in the TextViews:

@Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.activity_category_selection, container, false);

        tvCategory1 = view.findViewById(R.id.text_view_category_1);
        tvCategory2 = view.findViewById(R.id.text_view_category_2);
        tvCategory3 = view.findViewById(R.id.text_view_category_3);
        textViewCategoryRound = view.findViewById(R.id.text_view_category_round);

        imageCategory1 = view.findViewById(R.id.image_category_1);
        imageCategory2 = view.findViewById(R.id.image_category_2);
        imageCategory3 = view.findViewById(R.id.image_category_3);

        categoryCollectionRef.get().addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
            @Override
            public void onComplete(@NonNull Task<QuerySnapshot> task) {
                if (task.isSuccessful()) {

                    List<Category> categoryList = new ArrayList<>();
                    for (DocumentSnapshot document : task.getResult()) {
                        Category category = document.toObject(Category.class);
                        categoryList.add(category);
                    }

                    int categoryListSize = categoryList.size();
                    List<Category> randomCategoryList = new ArrayList<>();
                    for (int i = 0; i < categoryListSize; i++) {
                        Category randomCategory = categoryList.get(new Random().nextInt(categoryListSize));
                        if (!randomCategoryList.contains(randomCategory)) {
                            randomCategoryList.add(randomCategory);
                            if (randomCategoryList.size() == 3) {

                                break;

                            }
                        } else {
                            Log.d(TAG, "Error getting documents: ", task.getException());
                        }
                    }

                }
            }
        });

        return view;
    }
}

Here is the category class where I hope I store the documents correctly:

public class Category {

    public String category_name;


    public String picture_id;

    public Category (){

    }


    public String getPicture_id() {
        return picture_id;
    }

    public void setPicture_id(String picture_id) {
        this.picture_id = picture_id;
    }

    public String getCategory_name() {
        return category_name;
    }

    public void setCategory_name(String category_name) {
        this.category_name = category_name;
    }

    public Category(String category_name){
        this.category_name = category_name;
    }
}

Here is the way I structured my Category collection (there are 8 documents/categories): enter image description here

Kaiser
  • 606
  • 8
  • 22
  • I don't understand which part of the problem you're stuck on. – Doug Stevenson Jan 06 '20 at 16:07
  • The problem was that I did not know if I used the both code in the right way to retrieve the documents. And I also did not know how to display the documents in the TextViews in the CategoryActivity but Numan Turkeri had the right way – Kaiser Jan 06 '20 at 16:38

2 Answers2

1

Hi i can suggest easy way.

 List<Category> categoryList =null;

   categoryCollectionRef.get().addOnCompleteListener(new 
   OnCompleteListener<QuerySnapshot>() {
        @Override
        public void onComplete(@NonNull Task<QuerySnapshot> task) {
            if (task.isSuccessful()) {

               categoryList = new ArrayList<>();
                for (DocumentSnapshot document : task.getResult()) {
                    Category category = document.toObject(Category.class);
                    categoryList.add(category);
                }



                }

            }
        }
    });
testButtonClickListener....{
Collections.shuffle(categoryList);
    showShuffledList();
}
public void showShuffledList(){
textView1.setText(categoryList.get(0).getCategoryName())
 textView2.setText(categoryList.get(1).getCategoryName())
 textView3.setText(categoryList.get(2).getCategoryName())
}

Of course you should add null check all steps and configure more way.

Numan Turkeri
  • 526
  • 4
  • 18
0

You can do 3 things:

1) when saving the docs name them with an incremental number at the end, then generate 3 random numbers in range and fetch the docs ending in those numbers.

2) add a field in all docs that holds a unique number and follow similar approach to the above but using a query on those fields.

3) create a special doc that has the names of all other docs in an array. Then fetch 3 doc names from the array based on the generated random numbers.

There might be other ways too, but those are the approaches that first come to my mind.

Waelmas
  • 1,894
  • 1
  • 9
  • 19