arimorty / floatingsearchview

A search view that implements a floating search bar also known as persistent search
https://github.com/arimorty/floatingsearchview/blob/master/README.md
Apache License 2.0
3.54k stars 667 forks source link

How to Swap SearchSuggestions #272

Open rajiv256 opened 7 years ago

rajiv256 commented 7 years ago

mSearchView.swapSuggestions(newSuggestions);

I have an arraylist of Strings with me to be suggested for a change in the query. What should be the input of the swapSuggestions function?

mrmohajeri commented 7 years ago

I have this problem. I have List<Address>. But swapsuggestion needs SearchSuggestion

rajiv256 commented 7 years ago

I found a way to do this. SearchSuggestion is an abstract interface with unimplemented functions. So, I created a new object Suggestion which contains the suggestion string as its field and implements the functions of SearchSuggestion. And then we can return an ArrayList<Suggestion> to swapSuggestion function.

Here is my code

` Code :

public class Suggestion implements SearchSuggestion{

public String suggestion ;

public Suggestion(String s){

    this.suggestion = s ;

}

public String getSuggestion(){

    return this.suggestion ;

}

@Override

public String getBody() {

    return this.suggestion ;

}

@Override

public int describeContents() {

    return 0;

}

@Override

public void writeToParcel(Parcel parcel, int i) {

}

}

Code

`

Now pass an array list of Suggestion objects containing the suggested strings as their fields to the swapSuggestions function.

mrmohajeri commented 7 years ago

Here is my code:

Geocoder geocoder = new Geocoder(this);
List<Address> addressList = null;
addressList = geocoder.getFromLocationName(searchQuery, 5);

But the method getFromLocationName returns List<Address> So I can not pass my addresslist variable to Suggestion Class that you mentioned.

Can I use the following class?

public class AddressModel extends android.location.Address implements SearchSuggestion {
    public AddressModel(Locale locale) {
        super(locale);
    }

    @Override
    public String getBody() {
        return null;
    }
}

Because when I use this, I have error. Because getFromLocationName needs a List<Address> not List<AddressModel>

minhluan96 commented 7 years ago

@nefrin79 you could use that AddressModel like a model for suggestion

public class Suggestion implements SearchSuggestion {
    private Address mQuery;

    public Suggestion(Address query) {
        this.mQuery = query;
    }

    public Address getmQuery() {
        return mQuery;
    }

    public void setmQuery(Address mQuery) {
        this.mQuery = mQuery;
    }

    @Override
    public String getBody() {
        return mQuery.getAdminArea() + ", " + mQuery.getCountryName();
    }

and then you create another list to hold your result

List<Suggestion> lstResult = new ArrayList<>();

Finally, you add those address item back to the list that you just created

List<Address> addressList = geocoder.getFromLocationName(queryLocation, 5);

            if (addressList != null) {
                for (Address address : addressList) {
                    lstResult.add(new Suggestion(address));
                }
            }