I’ve got a problem, and I know where the problem lays but what I don’t understand is why it sometimes works and other times not. This is what I would like to do.
I have an Android application that displays a Google MapView and on top of this MapView there is an autocomplete textview. I get the value from Google (in json-format) and I can display them in this autocomplete textview. This works all of the times. Now when I press on one of the options that I get from Google, I would like to zoom to that location. This works most of the times.
So when I press in the address Elfde, I get 5 options/predictions back from Google. After that I Display those options. One of the options is Elfde-Liniestraat, Belgium. The Google MapView zooms to that location. If I type Bremen, and select Bremen, Germany it zooms to that location. But when I type in Peer and select Peer, Belgium I get a indexOutOfBounds exception in my ArrayAdapter.
Now the code:
When the textchanges in my autocomplete TextView i use this code:
public void onTextChanged(CharSequence s, int start, int before,
int count) {
if (count % 3 == 1){
// Get the places and pass the argument in the TextView to the task.
GetPlaces task = new GetPlaces();
task.execute(_txtFindLocation.getText().toString());
}
}
When I click on an item i use this code:
_txtFindLocation.setOnItemClickListener(new OnItemClickListener(){
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// Hide the keyboard.
InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
inputMethodManager.hideSoftInputFromWindow(_txtFindLocation.getWindowToken(), 0);
Log.v("Take a look on variables", "item: " + String.valueOf(arg2) + ", AdapterView: " + arg0.getCount() + ", _strArrayAdapter: " + _strArrayAdapter.getCount());
// Move to specified location.
moveToLocation(arg0.getItemAtPosition(arg2).toString());
// Clear the adapter, to decrease the size.
_strArrayAdapter.clear();
}
});
And here is where the indexOutOfBoundsException occurs.
For the input and selection of Elfde-Liniestraat, Belgium: arg2 = 0, arg0.getCount() = 5, _strArrayAdapter (that is linked with arg0 as source) = 5
For the input and selection of Bremen, Germany: arg2 = 3, arg0.getCount() = 5, _strArrayAdapter = 5
For the input and selection of Peer, Belgium (where the error is thrown): arg2 = 0, arg0.getCount() = 0, _strArrayAdapter = 0
So defenitely here is where the error is thrown. The _strArrayAdapter gets filled in this function:
private class GetPlaces extends AsyncTask<String, Void, ArrayList<String>>{
@Override
protected ArrayList<String> doInBackground(String... params) {
ArrayList<String> myPredictions = new ArrayList<String>();
Log.d("Search places", "params: " + params[0].toString());
try{
URL myURL = new URL("https://maps.googleapis.com/maps/api/place/autocomplete/json?input="
+ URLEncoder.encode(params[0], "UTF-8")
+ "&types=geocode&language=en&sensor=true&key=API_KEY");
URLConnection myURLConnection = myURL.openConnection();
BufferedReader myBufferReader = new BufferedReader(new InputStreamReader(myURLConnection.getInputStream()));
String strLine;
StringBuffer strBuffer = new StringBuffer();
// Take Google's legible JSON and turn it into on big String.
while((strLine = myBufferReader.readLine()) != null){
strBuffer.append(strLine);
}
// Turn String into a JSON Object.
JSONObject jsonPredictionsObject = new JSONObject(strBuffer.toString());
// Get a JSON Array that is inside the JSON Object.
JSONArray jsonPredictionsArray = new JSONArray(jsonPredictionsObject.getString("predictions"));
for (int i = 0; i < jsonPredictionsArray.length(); i++){
jsonPredictionsObject = (JSONObject) jsonPredictionsArray.get(i);
myPredictions.add(jsonPredictionsObject.getString("description"));
}
}
catch (IOException e){
Log.e("Search places", "GetPlaces: doInBackGround", e);
}
catch (JSONException e){
Log.e("Search places", "GetPlaces: doInBackGround", e);
}
return myPredictions;
}
protected void onPostExecute(ArrayList<String> result){
Log.d("YourApp", "onPostExecute: " + result.size());
// Update the adapter.
_strArrayAdapter = new ArrayAdapter<String>(getBaseContext(), R.layout.activity_list_item);
_strArrayAdapter.setNotifyOnChange(true);
// Attach the adapter to the TextView.
_txtFindLocation.setAdapter(_strArrayAdapter);
for (String text : result){
Log.d("Search places", "onPostExecute: result = " + text);
_strArrayAdapter.add(text);
}
_strArrayAdapter.clear();
Log.d("Search places", "onPostExecute: autoCompleteAdapter " + _strArrayAdapter.getCount());
}
For all three testcases the last Log.d(“Search places”, “onPostExecute: autoCompleteAdapter ” + _strArrayAdapter.getCount()); give a value of 5!
So the _strArrayAdapter gets filled in all cases, but when I select an Item in some cases the value of the _strArrayAdapter is set to 0, so its not filled.
Why is this happening?
With kind regard,
Martijn Haex
EDIT Thanks to the replys, I’ve revisited my code (above) a little bit and now it works like a charm.
The problem seems to be in following snippet in your
onTextChangedfunction. BTW, why are you clearing the array whenlengthOfModifiedText % 3 == 1?:When you enter
"Peer", your array will be cleared (no elements).Whenever you enter text of length
L, where L % 3 == 1, yourstrArrayAdapterwill be cleared as per your code.