会议室数据库删除项目后显示错误数据

时间:2020-04-07 16:44:18

标签: android android-recyclerview android-room android-viewmodel

我正在使用我的应用程序中的会议室数据库。该应用程序显示“所有图书”,“家庭图书”和“图书馆图书”三类图书。该应用程序根据用户选择显示图书数据。一切正常,该应用程序显示正确的类别图书,但是当用户从任何类别中删除一本书时,应用程序和列表都会刷新,然后有时应用程序显示错误的数据,这意味着如果用户正在查看“家庭图书”类别下的书籍,而他删除了一本书,则该应用程序将向他显示随机书籍,而不是显示其选择的书籍家庭书。 //这就是我加载数据的方式:

 public void loadBooks(){
        if(booksType==1){
                    mBookViewModel.getAllBooks().observe(this, new Observer<List<Books>>() {
                        @Override
                        public void onChanged(@Nullable final List<Books>books) {
                                                 adapter.setBooks(books);
                        }
                    });
                }
     if(booksType==2){
                    mBookViewModel.getHomeBooks().observe(this, new Observer<List<Books>>() {
                        @Override
                        public void onChanged(@Nullable final List<Books>books) {
                            // Update the cached copy of the words in the adapter.
                            adapter.setBooks(books);
                        }
                    });
                }
     if(booksType==3){
                    mBookViewModel.getLibrarayBooks().observe(this, new Observer<List<Books>>() {
                        @Override
                        public void onChanged(@Nullable final List<Books>books) {
                            // Update the cached copy of the words in the adapter.
                            adapter.setBooks(books);
                        }
                    });
                }
    calculateSum();// calculating sum of values of a column.
    }

    public void calculateSum(){
    double total=mBookViewModel.getBookSum();
    }

  //  This how I am deleting a book:
    public void delete(Books book){


                   int rowsDeleted = mBookViewModel.deleteBook(book);


           loadBooks(); //have to call this method as I am doing other stuff also.Like calculating the sum of other integer fields.
    // suppose user is viewing Home Books(means bookType==2) after this call instead of showing user Home Books the app shows him any random list of books.It does not happen always but after user deletes five to six books continuously and fast.


    }

  //  My View Model:
      LiveData<List<Books>> getAllBooks() {return mRepository.getAllBooks(); }
     LiveData<List<Books>> getHomeBooks() {return mRepository.getHomeBooks(); }
     LiveData<List<Books>> getLibraryBooks() {return mRepository.getLibraryBooks(); }

     double getBookSum() {return mRepository.getBookSum(); }

  //  Repository:
      LiveData<List<Books>> getallBooks() {
            try {
                return new getAllBooksAsyncTask(mBooksDao).execute().get();
            } catch (ExecutionException e) {
                e.printStackTrace();
                return null;
            } catch (InterruptedException e) {
                e.printStackTrace();
                return null;
            }
        }

      private static class getAllBooksAsyncTask extends AsyncTask<Void, Void, LiveData<List<Books>>> {
            private BooksDao mAsyncTaskDao;


            getAllBooksAscAsyncTask(BooksDao dao) {
                mAsyncTaskDao = dao;
            }

            @Override
            protected LiveData<List<Books>> doInBackground(Void... voids) {
                return mAsyncTaskDao.getAllBooks();
            }
        }

     LiveData<List<Books>> getallBooks() {
            try {
                return new getAllBooksAsyncTask(mBooksDao).execute().get();
            } catch (ExecutionException e) {
                e.printStackTrace();
                return null;
            } catch (InterruptedException e) {
                e.printStackTrace();
                return null;
            }
        }

      private static class getHomeBooksAsyncTask extends AsyncTask<Void, Void, LiveData<List<Books>>> {
            private BooksDao mAsyncTaskDao;


            getHomeBooksAscAsyncTask(BooksDao dao) {
                mAsyncTaskDao = dao;
            }

            @Override
            protected LiveData<List<Books>> doInBackground(Void... voids) {
                return mAsyncTaskDao.getHomeBooks();
            }
        }

     LiveData<List<Books>> getallBooks() {
            try {
                return new getAllBooksAsyncTask(mBooksDao).execute().get();
            } catch (ExecutionException e) {
                e.printStackTrace();
                return null;
            } catch (InterruptedException e) {
                e.printStackTrace();
                return null;
            }
        }

      private static class getLibraryBooksAsyncTask extends AsyncTask<Void, Void, LiveData<List<Books>>> {
            private BooksDao mAsyncTaskDao;


            getLibraryBooksAscAsyncTask(BooksDao dao) {
                mAsyncTaskDao = dao;
            }

            @Override
            protected LiveData<List<Books>> doInBackground(Void... voids) {
                return mAsyncTaskDao.getLibraryBooks();
            }
        }

      public double getBookSum() {
            try {
                return new getBookSumAsyncTask(mBookDao).execute().get();
            } catch (ExecutionException e) {
                e.printStackTrace();
                return 0;
            } catch (InterruptedException e) {
                e.printStackTrace();
                return 0;
            }
        }

      private static class getBookSumAsyncTask extends AsyncTask<Void, Void, Double> {

            private BookDao mAsyncTaskDao;

            getBookSumAsyncTask(BookDao dao) {
                mAsyncTaskDao = dao;
            }

            @Override
            protected Double doInBackground(Void... voids) {
                return mAsyncTaskDao.getBookSum();

            }
        }

 //   Dao:
       @Query("SELECT * FROM books WHERE type  = 1 ")
        LiveData<List<Books>> getAllBooks();

      @Query("SELECT * FROM books WHERE type  = 2 ")
        LiveData<List<Books>> getHomeBooks();

      @Query("SELECT * FROM books WHERE type  = 3 ")
        LiveData<List<Books>> getLibraryBooks();

      @Query("SELECT SUM(pages) FROM books ")
        double getBookSum();


 //   Recycler View:
      @Override
        public void onBindViewHolder(@NonNull booksRecyclerAdapter.BookViewHolder holder, int position) {
            if (mBookss != null) {
              Books current = mBooks.get(position);


                String name = current.getName();


                holder.nameTextView.setText(name);

            }
        }
     void setBooks(List<Books> book){
            mBooks = book;
            notifyDataSetChanged();
        }
       @Override
        public int getItemCount() {
            if (mBooks != null)
                return mBooks.size();
            else return 0;
        }
        public Books getBookAtPosition (int position) {
            return mBooks.get(position);
        }

我尝试添加adapter.notifyDataSetChanged()和adapter.notifyItemRemoved(position);但是,如果我的代码中有错误,那么为什么它不总是发生,通常在连续删除五到六项后有时会发生。 非常感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

经过大量尝试后,我发现问题出在适配器,适配器正在加载先前选择的书籍清单,例如如果用户首先选择homeBooks,然后选择allBooks并对allBooks列表中的任何一本书执行删除查询,则在删除操作之后,适配器不再向用户显示allBooks列表,而是将其保留在allBooks列表中。由于我在所有三种情况下都使用相同的适配器。我认为这可能是Room Database的问题。因此,当我在不同情况下使用不同的适配器时,一切都按预期工作。我不知道这是否是正确的方法它,但是现在它正在工作。所以代码现在变成

 public void loadBooks(){
        if(booksType==1){
   bookRecyclerAdapter adapter1=new bookRecyclerAdapter(this);
        recyclerView.setAdapter(adapter1);
                    mBookViewModel.getAllBooks().observe(this, new Observer<List<Books>>() {
                        @Override
                        public void onChanged(@Nullable final List<Books>books) {
                                                 adapter1.setBooks(books);
                        }
                    });
                }
     if(booksType==2){
  bookRecyclerAdapter adapter2=new bookRecyclerAdapter(this);
        recyclerView.setAdapter(adapter2);
                    mBookViewModel.getHomeBooks().observe(this, new Observer<List<Books>>() {
                        @Override
                        public void onChanged(@Nullable final List<Books>books) {
                            // Update the cached copy of the words in the adapter.
                            adapter2.setBooks(books);
                        }
                    });
                }
     if(booksType==3){
  bookRecyclerAdapter adapter3=new bookRecyclerAdapter(this);
        recyclerView.setAdapter(adapter3);
                    mBookViewModel.getLibrarayBooks().observe(this, new Observer<List<Books>>() {
                        @Override
                        public void onChanged(@Nullable final List<Books>books) {
                            // Update the cached copy of the words in the adapter.
                            adapter3.setBooks(books);
                        }
                    });
                }
    calculateSum();// calculating sum of values of a column.
    }