簡體   English   中英

刪除項目后房間數據庫顯示錯誤數據

[英]Room Database showing wrong data after deleting item

我在我的應用程序中使用房間數據庫。該應用程序在所有書籍、家庭書籍和圖書館書籍三個類別下顯示書籍。該應用程序根據用戶選擇顯示書籍數據。一切正常,該應用程序顯示正確的類別書籍,但當用戶刪除任何類別的書,然后應用程序和列表刷新,然后有時應用程序顯示錯誤的數據意味着如果用戶正在查看家庭書籍類別下的書籍並且他刪除了一本書,那么應用程序會向他顯示隨機書籍而不是顯示他選擇的家庭書籍。 // 這就是我加載數據的方式:

 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); 但它不起作用。另外,如果我的代碼有問題,那么為什么它不會總是發生,它有時通常在連續刪除五六個項目后發生。 非常感謝任何幫助。

經過多次嘗試,我發現問題出在適配器上。適配器正在加載先前選擇的書籍列表,例如,如果用戶首先選擇 homeBooks,然后選擇 allBooks,然后在刪除后對 allBooks 列表中的任何一本書執行刪除查詢操作而不是將用戶保留在 allBooks 列表中,適配器向他顯示 homeBooks 列表。 因為我對所有三種情況都使用了相同的適配器。我認為這可能是房間數據庫的問題。所以,當我對不同的情況使用不同的適配器時,一切都按預期工作。我不知道這是否是正確的方法它但現在它正在工作。所以代碼現在變成了

 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.
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM