I am creating a Google
search like app for songs . while user entering a song name , AutotextView
suggest you the songs Name . For each word change I have to call an AsycTask
to get data from web service
and it is perfectly working. I have two problems in it.
- it's Response is slow
- AsyncTask does not set adapter when i call a song with space like "Waka Waka"
I have tried other way round to solve it . I replace the space with %20
but still adapter for space is not setting the adapter listView for AutotexView
Autotextview
textView.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence cs, int arg1, int arg2,
int arg3) {
seq = cs;
}
@Override
public void beforeTextChanged(CharSequence s, int arg1, int arg2,
int arg3)
{
}
@Override
public void afterTextChanged(Editable arg0)
{
if(mAsync!=null) {
mAsync.cancel(true);
}
//new SearchTask().execute(seq.toString().trim());
mAsync =(SearchTask) new SearchTask().execute(seq.toString());
}
});
SearchTask
private class SearchTask extends AsyncTask<CharSequence, Void, String>
{
ArrayAdapter<String> adapter;
String[] array;
@Override
protected String doInBackground(CharSequence... params)
{
try {
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(pak_url + params[0]);
HttpResponse responce = httpclient.execute(httppost);
HttpEntity httpEntity = responce.getEntity();
String response = EntityUtils.toString(httpEntity);
Log.d("response is", response);
if (response != null) {
String substr = response.split("\\(")[2].split("\\)")[0];
array = substr.split(",");
for (int i = 0; i < array.length; i++) {
array[i] = array[i].replace("'", "");
}
}
return response;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
if (array != null) {
adapter = new ArrayAdapter<String>(PlayListActivity.this,
android.R.layout.simple_list_item_1, array);
textView.setAdapter(adapter);
adapter.getFilter().filter(seq);
}
}
}
Anyone suggest me a fast way other than AsyncTask
. or May i doing something wrong while setting adapter or filtering. When i give space , The AsyncTask
do not get data.