0

I have an AutoCompleteTextView and I have added a Textwatcher to it. The textview gets data from the database in Json format via an AsyncTask(gets pairs of city, state data example Chicago,IL). The issue is that about 60% of the time my AutoCompleteTextview fails to create a list of options when I can see that the Json has been successfully transferred to the onPostexecute method. For example when I type Orl in the Textview and look at the AsyncTask onPostExecute I can see the following Json being returned

{"location_update":[{ "city":"Orla","state":"TX"},

{"city":"Orland",state":"CA"},

{"city":"Orland","state":"IN"}]}

On any given try the AutoCompleteTextview might show none or all 3 of those options. I do not know the reason for this inconsistency, here is my code what could cause this? any suggestions would be great as stated earlier I can see that everything is being passed correctly but on onPostExecute is inconsistent in binding to the Textview. Main.java // Main.Activity: This simply connects to the Textview and sends information to LocalFeed_Auto which has the AsyncTask Class

   AutoCompleteTextView  searchlocation= (AutoCompleteTextView) findViewById(R.id.search);
        searchlocation.setThreshold(2);
        searchlocation.addTextChangedListener(new TextWatcher() 
       {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                String Search_Location = searchlocation.getText().toString();
                new LocalFeed_Auto(Search_Location, LocalFeed.this, searchlocation).execute();
            }

            @Override
            public void afterTextChanged(Editable s) {


            }
        });

LocalFeed_Auto.java my AsyncTask I only show onPostexecute because the other parts are simply to make database calls and that works correctly because I can gather its data LocalFeed_auto.java

    // This is my AsyncTask were I get database data and send to  
    AutcompleteTextview
   //  at the very top the constructor that receives the inputs
 public LocalFeed_Auto(String searchlocati,Activity act, AutoCompleteTextView searchlocation)
    {
        this.searchlocati=searchlocati;
        this.act=act;
        this.searchlocation=searchlocation;
    }


   // the rest is network calls then onPostExecute gets the output
    protected void onPostExecute(String results) {
        try
        {
    //  String Total has all the information 
    //from the database that has been 
    //returned from doInBackground and I check that data in println()
         System.err.println(Total);

            JSONObject jsonn = new JSONObject(total);
            JSONArray jArray = jsonn.getJSONArray("location_update");
            JSONObject jobject = null;
            String location = "";
            JSONArray sss = new JSONArray();
      // All data returned is in Json so I read it here
            for (int i = 0; i < jArray.length(); i++) {
                jobject = jArray.getJSONObject(i);
         // location holds the information that 
         //AutoCompleteTextview should show and 
         //binds city state like city,state
         location+= jobject.getString("city") + "," + jobject.getString("state")+";";
                sss.put(jobject);
            }
     // I split the information by ";" city state pairs
            String[] Arrays = location.split(";");

      // I pass it back to the AutocompleteTextview here
ArrayAdapter<String> adapters = new ArrayAdapter<>
 (this.act, android.R.layout.simple_dropdown_item_1line,Arrays);
            searchlocation.setAdapter(adapters);


        }
        catch(Exception e)
        {

        }
    }

Since I can see all data is passing successfully in the System.err.println() then it must mean that the adapter is not bind 100% of the time.

Pablo prez
  • 139
  • 4
  • 16
  • `"I have an AutoCompleteTextView and I have added a Textwatcher to it."` wrong, `"The textview gets data from the database in Json format via an AsyncTask"` wrong. see http://stackoverflow.com/a/19860624/2252830 – pskink Aug 23 '15 at 05:23
  • and? is it better now? – pskink Aug 23 '15 at 18:18
  • 1
    It is much better, Thanks a lot that example was perfect. I simply took that example put it in the background thread and it worked perfectly. – Pablo prez Aug 23 '15 at 18:29
  • I tried giving you a point here but I do not have my 15 reputation points. – Pablo prez Aug 23 '15 at 18:32
  • no "background thread" needed: see what thread `runQuery` is called in: add `Log.d(TAG, "thread " + Thread.currentThread());` inside 'runQuery' and see the logcat – pskink Aug 23 '15 at 18:33
  • Oh ok that's perfect then everything is running, I was just confused because I thought that would occupy the 1 thread that an android activity has. My activity has other stuff going on besides that but I am new to all of this. – Pablo prez Aug 23 '15 at 20:12

0 Answers0