سؤال

Let's assume I've got 3 activities. They all present data fetched from Volley because data is coming from external API, in each activity data is stored in a ListView, and data is provided with my custom BaseAdapter (data differs between activities but they implement common interface called Listeable so generally it doesn't matter)

I read a bit about creating a SearchActivity, as far as I understand I'm just passing a query and I need to perform searching in SearchActivity on my own.

My question is can I pass list with data from my activity that runs a search to a SearchActivity that I don't need to fetch that data once again from external API, and then in my SearchActivity just filter that list for matching my query?

I want to archive behaviour, that no matter which activity it is I'm just passing that data to searchActivity and it filters it for me and present that data in a new list.

I hope that everybody can understand my question.

Maybe someone has done this kind of thing before and can share an example code?

هل كانت مفيدة؟

المحلول

I've found a solution

First off I implemented Parcelable in my List items (following How can I make my custom objects Parcelable?)

Than I've overwritten startActivity (thats the only way to pass data to Search Widget)

@Override
public void startActivity(Intent intent) {
    // check if search intent
    if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
        intent.putExtra("elementsToSearch", keywordsData);
    }

    super.startActivity(intent);
}

Make sure that keywordsData is an array of primitives or <? extends Parcelable>

Than in my Search Results activity I've simply called

getIntent().getExtras().getParcelableArrayList("elementsToSearch");
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top