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.