Open rajiv256 opened 7 years ago
I have this problem.
I have List<Address>
.
But swapsuggestion
needs SearchSuggestion
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
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) {
}
}
`
Now pass an array list of Suggestion
objects containing the suggested strings as their fields to the swapSuggestions
function.
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>
@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));
}
}
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?