Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8752525
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T13:15:11+00:00 2026-06-13T13:15:11+00:00

I’ve got a problem, and I know where the problem lays but what I

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T13:15:12+00:00Added an answer on June 13, 2026 at 1:15 pm

    The problem seems to be in following snippet in your onTextChanged function. BTW, why are you clearing the array when lengthOfModifiedText % 3 == 1 ?:

    if (count % 3 == 1){
                    _strArrayAdapter.clear();
    

    When you enter "Peer", your array will be cleared (no elements).
    Whenever you enter text of length L, where L % 3 == 1, your strArrayAdapter will be cleared as per your code.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I know there's a lot of other questions out there that deal with this
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
This could be a duplicate question, but I have no idea what search terms
Does anyone know how can I replace this 2 symbol below from the string
I want to count how many characters a certain string has in PHP, but
link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text
I am trying to understand how to use SyndicationItem to display feed which is
I've got a string that has curly quotes in it. I'd like to replace

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.