Passing Search Data to Search Activity

I have a main activity that has 2 fragments. The main action has SearchView in the action bar. Both fragments have a list of a large number of rows, List<String> .

Flow:

The user enters the fragment i → Selects a line (say, Selection1) → Based on Selection1, the list of lines is filled in the second fragment → Here the user selects the second line ---> Processing based on these two lines.

Now, since both fragments contain a large number of rows, the user enters a query into SearchView , which filters the list and reduces it to a smaller list displayed in SearchableActivity .

Now the problem is how SearchableActivity can access these two List<String> to filter them based on the request and display the shortened list to the user.

Currently, what I did is overridden by onSearchRequested and passes the data as

  @Override public boolean onSearchRequested() { Bundle appData = new Bundle(); appData.putString(FRAGMENT_ID, "Fragment_A"); appData.putStringArrayList(SEARCH_LIST, searchList); startSearch(null, false, appData, false); return true; } 

Is there a better way or a standard way in which this problem can be handled, that is, an implementation that allows me to base the data based on my MainActivity before SearchableActivity ?

Edit: add code. Displays how data is set to Fragment . onDataReceived is called from the HttpManager , which receives the data.

 @Override public void onDataReceived(String type,final Object object) { switch(type) { case PopItConstants.UPDATE_LIST: getActivity().runOnUiThread(new Runnable() { @Override public void run() { updateCinemaList((List<String>) object); } }); break; } } public void updateDataList(List<String> data) { this.dataList = data; spinner.setVisibility(View.GONE); mAdapter.updateList(dataList); } 
+7
android android-activity searchview android-fragments android-search
source share
2 answers

Okay ... So that's how I did it.

In principle, the data obtained in two fragments was not just a List<String> , but they were models, namely. Cinema and region, which contained details other than names, including location, rating, etc.

So, firstly, I created the ISearchable interface

 public Interface ISearchable { // This contains the Search Text. An ISearchable item is included // in search results if query is contained in the String returned by this method public String getSearchText(); //This is meant to return the String that must be displayed if this item is in search results public String getDisplayText(); //This is meant to handle onClick of this searchableItem public void handleOnClick(); } 

Both Cinema and Region models are implemented by ISearchable .

After that, I used the singleton DataManager class, in which I supported the List<ISearchable> currentSearchList .

 public class DataManager { .....<singleton implementation>.... List<ISearchable> currentSearchList; public void setSearchList(List<ISearchable> searchList) { this.currentSearchList = searchList; } public List<ISearchable> getSearchList() { return this.currentSearchList; } } 

Therefore, whenever a fragment (Fragment_A or Fragment_B) is loaded, it updates this currentSearchList , so when SearchableActivity searches, all it needs to do is DataManager.getInstance().getSearchList() , and then use this list to filter list of related items.

This is how I dealt with the problem of having lists in Activity other than SearchableActivity with which to search.

I understand that this may not be the best solution, so I look forward to suggestions and criticisms and use this to come up with a better solution.

+1
source share

I just answered a similar question a few minutes ago on how I can send a list to another action in Android Studio .

I urge you to rethink your pattern of simply transferring data between actions and fragments. Try to create one or more data models (non-Android classes) for your application and make these models available for the Android classes (Activities, Fragments, etc.) that they need.

Remove all the data storage and processing code from your actions and fragments and place it in the model (s).

+1
source share

All Articles