简体   繁体   中英

How to show the last item from room database at top in recyclerview

i am pulling data from a room database when ever i add new objects to the database and try to display them using Recyclerview they appear at the bottom of recycler view i am not using add(data,Position) function of list in the recyclerview adapter as it duplicates the data in recyclerview for example if data is already present in the recyclerview and user tries to saerch the same data again using a search view it gets duplicated also i have tried to reverse the list using layout-manager but then it scrolls the list to last item in recyclerview i have also tried smoothscroll to position 0 but it is not working either. I am using a search view to first search the data from the database if data is not present in the database i fetch it from the server through the API insert it to the database and notify the recyclerview of changes all works fine except that data appears at the bottom

Main Activity code

 private SearchView.OnQueryTextListener onQueryTextListener =
        new SearchView.OnQueryTextListener() {
            @Override
            public boolean onQueryTextSubmit(String query) {
                if (query.length() >= 3) {
                    getDealsFromDb(query);
                }
                return true;
            }

            @Override
            public boolean onQueryTextChange(String newText) {
                if (newText.length() >= 3) {
                    getDealsFromDb(newText);
                }
                return true;
            }

            private void getDealsFromDb(String searchText) {
                searchText = "%" + searchText + "%";
                final String finalSearchText = searchText;
                model.getDevicenames(searchText).observe(MainActivity.this, new Observer<List<MobileDataBaseObjectClass>>() {
                    @Override
                    public void onChanged(@Nullable List<MobileDataBaseObjectClass> mobileDataBaseObjectClasses) {
                        if (mobileDataBaseObjectClasses != null) {
                            if (mobileDataBaseObjectClasses.size() == 0) {
                                Log.i("Test", "Main Activity not found in database fetching from api: ");
                                model.insertSingleObject(finalSearchText);
                            } else {

                                mAdapter.setWords(mobileDataBaseObjectClasses);
                            }
                        }
                    }
                });

            }
        };

You must be passing the list to the adapter. First you get the list from database which will be sorted in a way like the last item at last index. After fetching the list just call the method to reverse the list.

List<DailyConsumption> mList = myDataBase.dailyConsumptionDao().findByDate(date);
Collections.reverse(mList); // inverses the list and shows last item at first index
// now pass this list to the adapter

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM