title: Using SearchView With Room Database (LiveData and ViewModel) in java
body: <p>how are you, I wish everyone well
  - I have developed an application to save data using the room, and it has already succeeded.
  - I used the search interface on the menu, and I found that it works well.
  - But when you delete a letter or all letters, the list does not return as it was before.
  Please help it is important.
  This is my code</p>
  <pre><code>    ProductEntry
  
      @Entity(tableName = &quot;product&quot;)
  public class ProductEntry {
  
      @PrimaryKey(autoGenerate = true)
      private int id;
  
      private final String productName, productBarcode;
  
      @Ignore
      public ProductEntry(String productName, String productBarcode) {
          this.productName = productName;
          this.productBarcode = productBarcode;
      }
  
      public ProductEntry(int id, String productName, String productBarcode) {
          this.id = id;
          this.productName = productName;
          this.productBarcode = productBarcode;
      }
  
      public int getId() {
          return id;
      }
  
      public void setId(int id) {
          this.id = id;
      }
  
      public String getProductName() {
          return productName;
      }
  
      public String getProductBarcode() {
          return productBarcode;
      }
  
  
  }
  
  ProductDao
  
      @Dao
  public interface ProductDao {
  
      @Query(&quot;SELECT * FROM product ORDER BY id&quot;)
      LiveData&lt;List&lt;ProductEntry&gt;&gt; loadAllTasks();
  
      @Query(&quot;SELECT * FROM product WHERE id = :id&quot;)
      LiveData&lt;ProductEntry&gt; loadTaskById(int id);
  
      @Insert
      void insertTask(ProductEntry productEntry);
  
      @Update(onConflict = OnConflictStrategy.REPLACE)
      void updateTask(ProductEntry productEntry);
  
      @Delete
      void deleteTask(ProductEntry productEntry);
  
  }
  
  ProductAdapter
  
      public class ProductAdapter extends RecyclerView.Adapter&lt;ProductAdapter.ProductViewHolder&gt; implements Filterable {
  
      final private ItemClickListener itemClickListener;
  
      private List&lt;ProductEntry&gt; productEntries;
  
      private final Context context;
  
      private AppDatabase mDb;
  
  //    private List&lt;ProductEntry&gt; productEntriesAll;
  
      public ProductAdapter(Context context, ItemClickListener listener, AppDatabase mDb) {
          this.context = context;
          itemClickListener = listener;
          this.mDb = mDb;
  //        this.productEntriesAll = new ArrayList&lt;&gt;(productEntries);
      }
  
      @NonNull
      @Override
      public ProductViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
          View view = LayoutInflater.from(context).inflate(R.layout.product_list, parent, false);
          return new ProductViewHolder(view);
      }
  
  //    public Filter getFilter() {
  //        return filter;
  //    }
  //
  //    private final Filter filter = new Filter() {
  //        @Override
  //        protected FilterResults performFiltering(CharSequence constraint) {
  //            List&lt;ProductEntry&gt; filteredList = new ArrayList&lt;&gt;();
  //            if (constraint == null || constraint.length() == 0) {
  //                filteredList.addAll(productEntries);
  //            } else {
  //                String filterPattern = constraint.toString().toLowerCase().trim();
  //                for (ProductEntry item : productEntries) {
  //                    if (item.getProductName().toLowerCase().contains(filterPattern)) {
  //                        filteredList.add(item);
  //                    }
  //                }
  //            }
  //            FilterResults results = new FilterResults();
  //            results.values = filteredList;
  //
  //            return results;
  //        }
  //
  //        @SuppressLint(&quot;NotifyDataSetChanged&quot;)
  //        @SuppressWarnings(&quot;unchecked&quot;)
  //        @Override
  //        protected void publishResults(CharSequence constraint, FilterResults results) {
  //
  //            productEntries.clear();
  //            productEntries.addAll((List) results.values);
  //            notifyDataSetChanged();
  //        }
  //
  //    };
  
      @Override
      public void onBindViewHolder(ProductViewHolder holder, int position) {
  
          ProductEntry productEntry = productEntries.get(position);
  
          String productName = productEntry.getProductName();
          holder.productNameView.setText(productName);
  
          String productBarcode = productEntry.getProductBarcode();
          holder.productBarcodeView.setText(productBarcode);
  
          mDb = AppDatabase.getInstance(context);
  
          holder.deleteProduct.setOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(View v) {
                  AppExecutors.getInstance().diskIO().execute(new Runnable() {
                      @Override
                      public void run() {
                          int position = holder.getAdapterPosition();
                          List&lt;ProductEntry&gt; tasks = getProductEntries();
                          mDb.productDao().deleteTask(tasks.get(position));
                      }
                  });
              }
          });
  
      }
  
      @Override
      public int getItemCount() {
          if (productEntries == null) {
              return 0;
          }
          return productEntries.size();
      }
  
      public List&lt;ProductEntry&gt; getProductEntries() {
          return productEntries;
      }
  
      @SuppressLint(&quot;NotifyDataSetChanged&quot;)
      public void setProductEntries(List&lt;ProductEntry&gt; productEntries1) {
          productEntries = productEntries1;
          notifyDataSetChanged();
      }
  
      @Override
      public Filter getFilter() {
          return filter;
      }
  
      Filter filter = new Filter() {
          @Override
          protected FilterResults performFiltering(CharSequence constraint) {
  
              List&lt;ProductEntry&gt; filteredList = new ArrayList&lt;&gt;();
              if (constraint.toString().isEmpty()) {
                  filteredList.addAll(productEntries);
              } else {
                  for (ProductEntry productEntry: productEntries) {
                      if (productEntry.getProductName().toLowerCase()
                      .contains(constraint.toString().toLowerCase())) {
                          filteredList.add(productEntry);
                      }
                  }
              }
  
              FilterResults results = new FilterResults();
              results.values = filteredList;
  
              return results;
  
          }
  
          @SuppressWarnings(&quot;unchecked&quot;)
          @SuppressLint(&quot;NotifyDataSetChanged&quot;)
          @Override
          protected void publishResults(CharSequence constraint, FilterResults results) {
              productEntries.clear();
              productEntries.addAll((Collection&lt;? extends ProductEntry&gt;) results.values);
              notifyDataSetChanged();
          }
  
      };
  
      public interface ItemClickListener {
          void onItemClickListener(int itemId);
      }
  
      class ProductViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
  
          private final TextView productNameView, productBarcodeView;
  
          private final ImageView deleteProduct;
  
          public ProductViewHolder(View itemView) {
              super(itemView);
  
              productNameView = itemView.findViewById(R.id.listProductName);
              productBarcodeView = itemView.findViewById(R.id.listProductBarcode);
  
              deleteProduct = itemView.findViewById(R.id.listDeleteProduct);
  
              itemView.setOnClickListener(this);
  
          }
  
          @Override
          public void onClick(View view) {
              int elementId = productEntries.get(getAdapterPosition()).getId();
              itemClickListener.onItemClickListener(elementId);
          }
      }
  }
  
  ProductsActivity
  
      public class ProductsActivity extends AppCompatActivity implements ProductAdapter.ItemClickListener {
  
      private FloatingActionButton fabAddProduct;
  
      private RecyclerView recyclerView;
  
      private ProductAdapter productAdapter;
  
      private AppDatabase mDb;
  
      private SearchView searchView;
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_products);
  
          fabAddProduct = findViewById(R.id.fabAddProducts);
          fabAddProduct.setOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(View view) {
                  Intent addProductIntent = new Intent(ProductsActivity.this, AddProductActivity.class);
                  startActivity(addProductIntent);
              }
          });
  
          recyclerView = findViewById(R.id.recyclerViewProducts);
  
          recyclerView.setLayoutManager(new LinearLayoutManager(this));
  
          productAdapter = new ProductAdapter(this, this, mDb);
          recyclerView.setAdapter(productAdapter);
  
          DividerItemDecoration decoration = new DividerItemDecoration(getApplicationContext()
                  , LinearLayoutManager.VERTICAL);
          recyclerView.addItemDecoration(decoration);
  //
  //        new ItemTouchHelper(new ItemTouchHelper.SimpleCallback(0
  //                , ItemTouchHelper.LEFT | ItemTouchHelper.RIGHT) {
  //            @Override
  //            public boolean onMove(@NonNull RecyclerView recyclerView
  //                    , @NonNull RecyclerView.ViewHolder viewHolder
  //                    , @NonNull RecyclerView.ViewHolder target) {
  //                return false;
  //            }
  //
  //            @Override
  //            public void onSwiped(@NonNull final RecyclerView.ViewHolder viewHolder, int swipeDir) {
  //                AppExecutors.getInstance().diskIO().execute(new Runnable() {
  //                    @Override
  //                    public void run() {
  //                        int position = viewHolder.getAdapterPosition();
  //                        List&lt;ProductEntry&gt; tasks = productAdapter.getProductEntries();
  //                        mDb.productDao().deleteTask(tasks.get(position));
  //                    }
  //                });
  //            }
  //        }).attachToRecyclerView(recyclerView);
  
          mDb = AppDatabase.getInstance(getApplicationContext());
  
          setupViewModel();
      }
  
     private void setupViewModel() {
          MainViewModel viewModel = ViewModelProviders.of(this).get(MainViewModel.class);
  
          viewModel.getProducts().observe(this, new Observer&lt;List&lt;ProductEntry&gt;&gt;() {
              @Override
              public void onChanged(@Nullable List&lt;ProductEntry&gt; productEntries) {
                  productAdapter.setProductEntries(productEntries);
              }
          });
      }
  
      @Override
      public void onItemClickListener(int itemId) {
  
          Intent intent = new Intent(ProductsActivity.this, AddProductActivity.class);
          intent.putExtra(AddProductActivity.EXTRA_PRODUCT_ID, itemId);
          startActivity(intent);
      }
  
      @Override
      public boolean onCreateOptionsMenu(@NonNull Menu menu) {
  
          MenuInflater inflater = getMenuInflater();
          inflater.inflate(R.menu.search_menu, menu);
  
          MenuItem searchProductItem = menu.findItem(R.id.actionSearchProduct);
  
          searchView = (SearchView) searchProductItem.getActionView();
          searchView.setQueryHint(getString(R.string.product_search));
          searchView.setImeOptions(EditorInfo.IME_ACTION_DONE);
  
          searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
              @Override
              public boolean onQueryTextSubmit(String query) {
                  return false;
              }
              @Override
              public boolean onQueryTextChange(String newText) {
                  productAdapter.getFilter().filter(newText);
                  return false;
              }
          });
  
          return true;
  
      }
  
  }
  
  [enter image description here][1]
  
  
  [enter image description here][2]
  
  [enter image description here][3]
  
  
    [1]: https://i.stack.imgur.com/6aj5t.jpg
    [2]: https://i.stack.imgur.com/4KpaH.jpg
    [3]: https://i.stack.imgur.com/hwbkn.jpg
  </code></pre>