I am trying to make a autocomplete using AsyncTask
with a server. I shared my code here.
This is the code for my AutocompleteTextwatcher
:
AutoCompleteTextView textView = (AutoCompleteTextView) findViewById(R.id.mainautocomplete);
textView.addTextChangedListener(new TextWatcher() {
@Override
public void afterTextChanged(Editable editable) {
}
@Override
public void beforeTextChanged(CharSequence charSequence, int arg1, int arg2, int arg3) {
}
@Override
public void onTextChanged(CharSequence charSequence, int start, int before, int count) {
String text=charSequence.toString();
if (text.length() > 3) {
MyAsyncTask myTask = new MyAsyncTask();
try {
ArrayList<String> adapterList = new ArrayList<String>();
adapterList=myTask.execute(url).get();/*My Web service url*/
if(!adapterList.isEmpty()){
Log.v("ADPTER LIST",adapterList.toString());
adapter = new ArrayAdapter<String>(getApplicationContext(), R.layout.list_item, adapterList);
textView.setAdapter(adapter);
}else{
Log.v("ADPTER LIST","No VALUES");
}
} catch (InterruptedException e) {
Log.v("catch",e.getMessage());
} catch (ExecutionException e) {
Log.v("catch2",e.getMessage());
}
}
}
});
And my AsyncTask
code is,
private class MyAsyncTask extends AsyncTask<String, Void, ArrayList<String>>{
@Override
protected void onPreExecute(){
pd = ProgressDialog.show(HomeLayoutActivity.this,"", "Please Wait!");
}
@Override
protected ArrayList<String> doInBackground(String... params) {
try {
String myQuery=params[0];
HttpClient httpClient = new DefaultHttpClient();
HttpResponse response = null;
InputStream is = null;
String result = null;
StringBuilder sb = null;
response = httpClient.execute(new HttpGet(myQuery));
is = response.getEntity().getContent();
BufferedReader reader = new BufferedReader(new InputStreamReader(is, "UTF-8"));
sb = new StringBuilder();
sb.append(reader.readLine() + "\n");
String line = "0";
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
is.close();
result = sb.toString();
JSONArray jArray = new JSONArray(result);
JSONObject jData = null;
tags.clear();
for (int i = 0; i < jArray.length(); i++) {
jData = jArray.getJSONObject(i);
tags.add(jData.getString("tagname"));
}
Log.v("JSON",""+tags.toString());
} catch(Exception e){
Log.v("MUGBUG4",e.getMessage());
}
return tags;
}
@Override
protected void onProgressUpdate(Void...strings ){
}
@Override
protected void onPostExecute(ArrayList<String> result){
Log.v("OPE",result.toString());
pd.dismiss();
}
}
My problem is,
ArrayList
from AsyncTask
(I logged and checked). But the values are not showing in the autocomplete dropdown.What i tried is,
onPostExecute
of AsyncTask
. The same output is showing.Is any problem with android 4 and Asynctask
doInBackground()
?
I have the same situation. I don't know will it help, but I recommend you to add these lines of code after adding your adapter.
if (!textView.isPopupShowing()) {
textView.showDropDown();
}
It helped me. To be true I don't know exact reason of this behavior, but in earlier implementations of AutoCompleteTextView they used ListView to display dropdown. In 4.0 (and maybe earlier) they start using ListPopupWindow. May be this is the reason. Cheers
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With