0

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.

  1. it's Response is slow
  2. 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.

Zar E Ahmer
  • 33,936
  • 20
  • 234
  • 300
  • What is the log you get for "response is"? – FreewheelNat Jul 25 '14 at 08:19
  • sendRPCDone(frameElement, 'waka waka', new Array( 'waka waka africa' , 'waka waka fifa world cup song' , 'waka waka' , 'waka waka shakira' , 'waka waka fifa' , 'waka waka esto es africa' , 'waka waka song' , 'waka waka mp4' , 'waka waka dj remix' , 'waka waka world cup' , 'waka waka dj mix' ), new Array(), new Array()); This is the response i get @FreewheelNat – Zar E Ahmer Jul 25 '14 at 09:15
  • Why not ask for a standard format for your data from the server? JSON / XML would be much easier to parse than the string you posted. What does the adapter contain after the asynctask for "waka waka"? Is it empty? I suspect the problem is elsewhere, not in the format of the query. – hbot Jul 25 '14 at 09:24
  • This might be a good resource to read: https://developers.google.com/places/training/autocomplete-android – hbot Jul 25 '14 at 09:31
  • I have debug . I am getting data in array but can't set in it adapter i think – Zar E Ahmer Jul 25 '14 at 10:10

0 Answers0