0

i have SearchView in my RecyclerView, user can delete the objects of this RecyclerView but when user searches for anything and deletes a object from the results this is where am having problem , when the search is not active am using a DefaultArrayList of Objects but when the searchBar is Active my result Objects are shown in another ArrayList named as FilteredArrayList and when i delete an item from array list the object is got deleted from the FilteredArrayList and from DataBase but not from the DefaultArrayList so when the search ends the same deleted file still remains at the DefaultArrayList

so any idea how can i notify the DefaultArrayList that the Object is not exists anymore (which is still cached the DefaultArrayList ) ???

well i tried this :

myAdapterClass.notifyDataSetChanged();

but it's not doing the job

remy boys
  • 2,928
  • 5
  • 36
  • 65
  • You need to delete it from `DefaultArrayList` by calling `DefaultArrayList.remove(index)` and then if you notify the adapter with `myAdapterClass.notifyDataSetChanged()` it should work. – TychoTheTaco May 27 '16 at 20:06
  • @TychoTheTaco but bro the point is this i can't , because right now DeafultArray is not the incharge right now View is using FilteredArray and the index of filterArray will be different then Deafultarray's Index – remy boys May 27 '16 at 20:13
  • Then just get a reference to `DefaultArrayList` and then get the object from `FilterredArrayList` and call `DefaultArrayList.remove(object)` – TychoTheTaco May 27 '16 at 22:34

2 Answers2

1

myAdapterClass.notifyDataSetChanged(); is not the problem here. You need to delete the item from both FilteredArrayList and DefaultArrayList.

Another solution for your problem is to use only one ArrayList. and inside your Adapter implement the Filterable interface. Refer to this Filterable example for more information.

Community
  • 1
  • 1
Abdallah Alaraby
  • 2,222
  • 2
  • 18
  • 30
1

What about passing that DefaultArrayList as data source to FilteredArrayList instead of copying+filtering? In pseudocode:

View {

   defaultList: DefaultArrayList;

   showSearchBar() {
      filteredList = new FilteredArrayList(this.defaultList);
      ...
      this.searchBar.show(filteredList);
   }

}


FilteredArrayList {

   FilteredArrayList(dataSource) {
     this.dataSource = dataSource;
   }

   enumerator() {
      ...
      // filtered items enumeration from dataSource
      ...
   }

   delete(item) {
      this.dataSource.delete(item);
      // also delete from filtered cache if needed
   }

}

Also it can be implemented with delegates or observers:

ListActionsDelegate {

   onDelete(Item item);

}

View ... implements ListActionsDelegate {

   defaultList: DefaultArrayList;

   onDelete(Item item) {
      this.defaultList.delete(item);
   }

   showSearchBar() {
      filteredList = new FilteredArrayList(this.defaultList);
      filteredList.delegate = this;
      ...
      this.searchBar.show(filteredList);
   }
}

FilteredArrayList {

   delegate: ListActionsDelegate;

   FilteredArrayList(items) {
     this.raw = items;
   }

   delete(item) {
      super.delete(item);

      if (this.delegate)
         this.delegate.onDelete(item); // notify list actions delegate
   }

}
ankhzet
  • 2,517
  • 1
  • 24
  • 31