I have an AutoCompleteTextView that fetch's names from a database, my problem seems to be with the Array-Adapter or dropdown that binds 50% of the time. For instance if someone types in John then then you might get Suggestion for Jo but not with hn . The dropdown displays randomly even when I can see that data has come through this is my activity
public class tester extends Activity
{
EditText POST;
AutoCompleteTextView autobox;
String[] Arrays;
ArrayAdapter<String> adapters;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// setContentView(R.layout.mypage);
// set up Array with some initial values
autobox= (AutoCompleteTextView) findViewById(R.id.autobox);
String[] Arrays={"John Smith","Sam Austin"};
// create the adapter inside the Oncreate method
adapters = new ArrayAdapter<>(tester.this,
android.R.layout.simple_dropdown_item_1line,Arrays);
autobox.setAdapter(adapters);
autobox.setThreshold(1);
autobox.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) {
if(start>=1)
autobox.getText().toString();
new AutoText().execute();
}
@Override
public void afterTextChanged(Editable s) {}
});
}
public class AutoText extends AsyncTask<String,String,String> {
public String LOGIN_URL = "http://10.0.2.2:9999/api/namedata";
HttpURLConnection connection;
URL url;
StringBuilder sb= new StringBuilder();
String line="",nameList="";
String names="";
DataOutputStream DO;
@Override
protected void onPreExecute() {
super.onPreExecute();
names= autobox.getText().toString();
}
@Override
protected String doInBackground(String[] jsonData) {
String query= "names="+ names;
try {
url = new URL(LOGIN_URL);
connection = (HttpURLConnection) url.openConnection();
connection.setDoOutput(true);
connection.setRequestMethod("POST");
connection.connect();
connection.setReadTimeout(10000);
connection.setConnectTimeout(15000);
DO = new DataOutputStream(connection.getOutputStream());
DO.writeBytes(query);
DO.flush();
DO.close();
InputStream in = new BufferedInputStream(connection.getInputStream());
BufferedReader reader = new BufferedReader(new InputStreamReader(in));
while ((line = reader.readLine()) != null) {
sb.append(line);
}
nameList= sb.toString();
}
catch (Exception e) {
e.printStackTrace();
System.err.println("Why I am getting errors:");
}
return nameList;
}
protected void onPostExecute(String results) {
try
{
JSONObject jsonn = new JSONObject(nameList);
JSONArray jArray = jsonn.getJSONArray("name_updates");
JSONObject jobject = null;
String fullnames="";
JSONArray JA = new JSONArray();
for (int i = 0; i < jArray.length(); i++) {
fullnames+= jobject.getString("fullnames")+"-";
JA.put(jobject);
}
Arrays= fullnames.split("-");
// check the output values
System.out.println("ee " + java.util.Arrays.toString(Arrays));
adapters.clear();
adapters.addAll(Arrays);
adapters.notifyDataSetChanged();
}
catch(Exception e)
{
}
}
}
}
If you notice I use the System.out.println("ee " + java.util.Arrays.toString(Arrays)); onPostexecute and can see that all the data is coming in correctly,however the dropdown issue does not show at times that is the main issue. When the dropdown menu does show up everything is updated as it should be. I have tried to put autobox.showDropDown(); after the notifyDataSetChanged() but it still disappears at random times when there is new data. I have tried Android - AutoCompleteTextView, showDropDown() doesn't always work but it did not work for me and it seems like a bad idea to make an adapter null and keep recreating it.