How to add suggested places in Google maps?
-
24-12-2019 - |
Question
I did everything according to the tutorial with Google: https://developers.google.com/places/training/autocomplete-android
and still have errors in the project
I have a problem in this piece of code:
private class PlacesAutoCompleteAdapter extends ArrayAdapter<String> implements Filterable {
private ArrayList<String> resultList;
public PlacesAutoCompleteAdapter(Context context, int textViewResourceId) {
super(context, textViewResourceId);
}
@Override
public int getCount() {
return resultList.size();
}
@Override
public String getItem(int index) {
return resultList.get(index);
}
@Override
public Filter getFilter() {
Filter filter = new Filter() {
@Override
protected FilterResults performFiltering(CharSequence constraint) {
FilterResults filterResults = new FilterResults();
if (constraint != null) {
// Retrieve the autocomplete results.
resultList = autocomplete(constraint.toString());
// Assign the data to the FilterResults
filterResults.values = resultList;
filterResults.count = resultList.size();
}
return filterResults;
}
@Override
protected void publishResults(CharSequence constraint, FilterResults results) {
if (results != null && results.count > 0) {
notifyDataSetChanged();
}
else {
notifyDataSetInvalidated();
}
}};
return filter;
}
}
and imports:
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.content.Context;
import android.location.Address;
import android.location.Geocoder;
import android.os.AsyncTask;
import android.os.Handler;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Filterable;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.ScrollView;
import android.widget.TextView;
import android.widget.Toast;
import com.ggimer.dodatki.Animacje;
import com.ggimer.wyznaczanietrasy.R;
import com.google.android.gms.drive.query.Filter;
import com.google.android.gms.maps.GoogleMap;
and errors:
Multiple markers at this line - The return type is incompatible with ArrayAdapter.getFilter() - overrides android.widget.ArrayAdapter.getFilter
Multiple markers at this line - FilterResults cannot be resolved to a type - The method performFiltering(CharSequence) of type new Filter(){} must override or implement a supertype method Multiple markers at this line - FilterResults cannot be resolved to a type - FilterResults cannot be resolved to a type
FilterResults cannot be resolved to a type
I added errors.
Why copying all of the documentation I still have errors?
Solution
Filter is an abstract class.
I think you are missing part of it in the anonymous class declaration. Did you check the docs?
Plus, your import: import com.google.android.gms.drive.query.Filter;
may be causing part of your problem. I don't think that the two types are compatible:
- ArrayAdapter.GetFilter is expected to return
android.widget.Filter
- your imports make it seem like you might be returning
com.google.android.gms.drive.query.Filter
So, double check that. Try removing import com.google.android.gms.drive.query.Filter;
and see if that fixes it.