0

Firebase database contains data for different keys. I need to query and show this data with recyclerview. but since the same data are saved many times in the database, the same results appear many times in recyclerview. I want to prevent this. Even though the queried value is dozens of times in the database, I want it to be displayed only once in recyclerview. How can I do that. I will be happy if you help.

InfiniteFirebaseArray infiniteFirebaseArray = new InfiniteFirebaseArray();
infiniteFirebaseArray.getSearch(areaCode + phoneNumber);


mAdapter = new InfiniteFirebaseRecyclerAdapter<ContactList, NameViewHolder>(ContactList.class, R.layout.layout_search_contact_list_item,
        NameViewHolder.class, mUserDatabase, mPageLimit) {

    @Override
    protected void populateViewHolder(NameViewHolder viewHolder, ContactList model, int position) {
        Logger.enter();
        if (model != null) {

            String string = model.getDisplay_name();
            String firstletter = string.substring(0, 1);

            resultcount++;

            viewHolder.name.setText(model.getDisplay_name());
            viewHolder.phone.setText(model.getPhone_number());
            viewHolder.userImageText.setText(firstletter);

            ContactResult contactResult = new ContactResult();
            contactResult.setName(model.getDisplay_name());
            contactResult.setPhone(model.getPhone_number());
            contactLists.add(contactResult);

            Log.d("testcontacts",String.valueOf(contactLists.get(position).getName()));

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

                    String phoneNumber = contactLists.get(position).getPhone();
                    callPhone(phoneNumber);
                }
            });

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

                    String contactName = contactLists.get(position).getName();
                    String contactPhoneNumber = contactLists.get(position).getPhone();
                    addContact(contactName, contactPhoneNumber);

                }
            });
        }
        
        Logger.exit();
    }
};

Search_Contact_List.setAdapter(mAdapter);
Search_Contact_List.addOnScrollListener(new EndlessRecyclerViewScrollListener(gridLayoutManager) {
    @Override
    public void onLoadMore(int page, int totalItemsCount, RecyclerView view) {

        Logger.enter();
        mAdapter.more();
        Logger.exit();
    }
});

Database result: enter image description here

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
İsa C.
  • 329
  • 5
  • 16
  • 3
    Rather than simply `contactLists.add(contactResult);`, you could check if the same object exists in that list – OneCricketeer Oct 05 '20 at 19:17
  • The best approach would be to not have those duplicate items in your database. The workaround would be to hide the items you don't want users to see: https://stackoverflow.com/questions/41223413/how-to-hide-an-item-from-recycler-view-on-a-particular-condition – Frank van Puffelen Oct 05 '20 at 19:43
  • Thank you but neither of these are the answers for me. I am using a populate viewholder. Even if I do not add an arraylist, these data are created in 4 copies. I need to both prevent this and hide the same data. It is not possible to check it while writing to the database because there are millions of data and the data written is thousands. – İsa C. Oct 05 '20 at 20:08
  • I changed the adapter class I used and checked the data I listed on the model. Thank you for your answers. – İsa C. Oct 05 '20 at 21:52

0 Answers0