7

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

The flow is:

User enters Fragment I --> Selects a string (lets say Selection1) --> Based on Selection1 a list of strings is populated in the second fragment --> Here the user selects a second String ---> Processing based on these two strings.

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

Now the problem is how does the SearchableActivity get access to these two List<String> to filter them based on the query and display a reduced list to the user.

Currently what I have done is overridden onSearchRequested and pass 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 standard way by which this problem can be handled i.e. an implementation that allows data to be based from my MainActivity to SearchableActivity?

Edit: Adding code. Showing how data is set in the 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);
}
unrealsoul007
  • 3,809
  • 1
  • 17
  • 33
  • May this link could help you – prat Oct 03 '15 at 21:39
  • @Heyyou Data is basically `List` which is contained in lets say Fragment A. But SearchableActivity doesn't have access to this list. I don't know what code do I need to paste here to elucidate this further. – unrealsoul007 Oct 03 '15 at 22:20
  • @Heyyou When the fragment loads my `HttpManager` downloads the List from the server and passes it too Fragment (using interface). Then fragment passes this list to adapter to display. – unrealsoul007 Oct 03 '15 at 22:23
  • @unrealsoul007 , have you pass string to actvity to fragmat for search? – Ravi Vaghela Oct 10 '15 at 05:31

2 Answers2

1

I just answered a similar question a few minutes ago, at how can I send a List into another activity in Android Studio

I encourage you to rethink your pattern of simply passing data around among Activities and Fragments. Consider creating one or more data models (non-Android classes) for your application, and making these models available to the Android classes (Activities, Fragments, etc.) that need them.

Remove all of the data storage and manipulation code from your Activities and Fragments, and put it into the model(s).

Community
  • 1
  • 1
GreyBeardedGeek
  • 29,460
  • 2
  • 47
  • 67
  • All my data storage code is in Data Models and not in Activities or Fragments. I just mentioned `List` in Fragment for the ease of understanding. Thanks for your answer but I had already been using Singleton pattern and was looking for something better. – unrealsoul007 Oct 10 '15 at 09:24
1

Okay... So this is how I did it.

Basically, the data received in the two fragments was not simply List<String> but they were models viz. Cinema and Region which contained details other than names including location, rating etc.

So, firstly, I made an interface ISearchable

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 the Cinema and Region models implemented ISearchable.

After this, I used a singleton class DataManager in which I maintained a 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;
    }

}

So whenever a fragment (either Fragment_A or Fragment_B) is loaded, it updates this currentSearchList, so that when the SearchableActivity performs search all it has to do is DataManager.getInstance().getSearchList() and then use this list for filtering out a list of matching items.

This is how I handled the problem of having Lists in Activity other than the SearchableActivity using which search needs to be performed.

I understand this might not be the best solution, so, I look forward to suggestions and criticisms, and using that to be arrive at a better solution.

unrealsoul007
  • 3,809
  • 1
  • 17
  • 33