Skip to content Skip to sidebar Skip to footer

Recyclerview - Sorting Not Working After Searching An 'arraylist'

I am working in an app with 'recyclerview', and I successfully implemented Search and sorting in it. Whenever I sort the list before searching something through it, Sorting works.

Solution 1:

It seems to me that the problem is this:

When you filter the booklist you create a temporary one and assign it to mAdapter.booklist:

this.booklist = booklist;
notifyDataSetChanged();

Then, when you want to sort the list you are sorting the previous instance of the list, not current one (the one that mAdapter owns).

I suspect that mAdapter.booklist = booksList; before mAdapter.notifyDataSetChanged in sortArrayList and reverseSortArrayList should do the trick.

Solution 2:

Create one copy of booksList before set filter in booksListMain for filtering function.

  booksListMain=newArrayList();
  booksListMain.addAll(booksList);

Update the filter function

voidfilter(String text) {
        booksList.clear();
        if(text.length()==0)
        {
        booksList.addAll(booksListMain);
        }else
        {
            for (BookPojo d : booksListMain) {
                if (d.getBookname().toLowerCase().contains(text)) {
                    booksList.add(d);
                }
            }
        }

        mAdapter.notifyDataSetChanged();
}

Solution 3:

I sorted the problem using below code. Both @Seigmeyer and @Om Infowave Developers answers helped me to fix out the issue. @Jaspreet Kaur answer is awesome for smooth scrolling ! Thanks for all of you guys !

voidfilter(String text) {
        if (text.length() == 0) {
            mAdapter.filterList(booklist);
        } else {
            ArrayList<BookPojo > temp = newArrayList();
            for (BookPojo d : booklist) {

                if (d.getContactName().toLowerCase().contains(text)) {
                    temp.add(d);
                }
            }
            mAdapter.filterList(temp);
        }
    }

Confused to whom the bounty should go !

Solution 4:

Use the Following code to display Full List, when search text is empty and create the new list when any text in the search.

if (myCustomerInfoArrayList != null && myCustomerInfoArrayList.size() > 0) {
                if (searchText.isEmpty()) {
                    Collections.sort(myArrayList, newCustomerNameSort());

                    SpeedyLinearLayoutManagerlinearLayoutManager=newSpeedyLinearLayoutManager(CustomerActivity.this);
                    linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);

                    linearLayoutManager.scrollToPosition(myPosition);

                    myRecyclerViewCustomer.setLayoutManager(linearLayoutManager);
                    myCustomerAdapter = newCustomerAdapter(CustomerActivity.this, 
                    myCustomerInfoArrayList, myDataBaseHandler);

                    myRecyclerViewCustomer.setAdapter(myCustomerAdapter);

                    myCustomerAdapter.notifyDataSetChanged();
                } else {
                    searchList = newArrayList<CustomerInfo>();
                    for (inti=0; i < myCustomerInfoArrayList.size(); i++) {
                        Stringname= myCustomerInfoArrayList.get(i).getMyFullName();
                        if (name.toLowerCase().contains(searchText)) {
                            CustomerProspectInfoinfo= myCustomerInfoArrayList.get(i);
                            info.setMyFullName(myCustomerInfoArrayList.get(i).getMyFullName());
                            searchList.add(info);

                        }
                    }
                    Collections.sort(searchList, newCustomerNameSort());

                    SpeedyLinearLayoutManagerlinearLayoutManager=newSpeedyLinearLayoutManager(CustomerActivity.this);
                    linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);

                    myRecyclerViewCustomer.setLayoutManager(linearLayoutManager);
                    myCustomerAdapter = newCustomerAdapter(CustomerActivity.this, 
                    searchList, myDataBaseHandler);

                    myRecyclerViewCustomer.setAdapter(myCustomerAdapter);
                    myCustomerAdapter.notifyDataSetChanged();
                }
            }



privateclassCustomerNameSortimplementsjava.util.Comparator<CustomerProspectInfo> {
        @Overridepublicintcompare(CustomerInfo lhs, CustomerInfo rhs) {
            String name1, name2;
            name1 = lhs.getMyFullName().toLowerCase().trim();
            name2 = rhs.getMyFullName().toLowerCase().trim();
            return name1.compareTo(name2);
        }
}

This help to increase the speed of recycler view scrolling and you can directly jump to required position.

SpeedyLinearLayoutManagerlinearLayoutManager=newSpeedyLinearLayoutManager(MainActivity.this);
linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
linearLayoutManager.scrollToPosition(myPosition);   // Position to scroll recycler view.      
myRecyclerView.setLayoutManager(linearLayoutManager);


SpeedyLinearLayoutManager.classpublicclassSpeedyLinearLayoutManagerextendsLinearLayoutManager {

        privatestaticfinalfloatMILLISECONDS_PER_INCH=2f; //default is 25f (bigger = slower)publicSpeedyLinearLayoutManager(Context context) {
            super(context);
        }

        publicSpeedyLinearLayoutManager(Context context, int orientation, boolean reverseLayout) {
            super(context, orientation, reverseLayout);
        }

        publicSpeedyLinearLayoutManager(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
            super(context, attrs, defStyleAttr, defStyleRes);
        }

        @OverridepublicvoidsmoothScrollToPosition(RecyclerView recyclerView, RecyclerView.State state, int position) {

            finalLinearSmoothScrollerlinearSmoothScroller=newLinearSmoothScroller(recyclerView.getContext()) {

                @Overridepublic PointF computeScrollVectorForPosition(int targetPosition) {
                    return SpeedyLinearLayoutManager.this.computeScrollVectorForPosition(targetPosition);
                }

                @OverrideprotectedfloatcalculateSpeedPerPixel(DisplayMetrics displayMetrics) {
                    return MILLISECONDS_PER_INCH / displayMetrics.densityDpi;
                }
            };

            linearSmoothScroller.setTargetPosition(position);
            startSmoothScroll(linearSmoothScroller);
        }
    }

Post a Comment for "Recyclerview - Sorting Not Working After Searching An 'arraylist'"