0

I am trying to implement the search functionality in android the data is binding through the result recieved from webservice.

This is working but problem is its lagging a bit. For eg After first word typed it waits for results to come and then type next word.

Its because async task is running on first word click.But its annoying for users to wait.

Any suggestion or changes which can make things smooth.

    public MultiAutoCompleteTextView editText1;
    ArrayList<setFilterItems> searchFilter=new ArrayList<setFilterItems>();

editText1.addTextChangedListener(new TextWatcher() {

                @Override

                public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3) {
                    // When user changed the Text


                    String cs1= cs.toString();

                    if (cs1.length() == 1) {
                        System.out.println("Size : "+searchFilter.size());
                        //if(searchFilter.size()<1) {
                            try {
                                result = new getSearchTags(cs.toString().toLowerCase(Locale.US)).execute().get();
                                System.out.println("yeh hai mera result " + result);
                            } catch (InterruptedException e) {
                                e.printStackTrace();
                            } catch (ExecutionException e) {
                                e.printStackTrace();
                            }
                        //}else{
                        //     searchadapter.getFilter().filter(cs.toString().toLowerCase(Locale.US));
                        //}
                        //searchadapter.getFilter().filter(cs.toString().toLowerCase(Locale.US));
                    }
                    if (result.equals("1")) {
                                try {
                                    System.out.println("Pehle Baar");
                                    searchadapter.getFilter().filter(cs.toString().toLowerCase(Locale.US));
                                    }
                    catch(Exception e)
                                   {
                                    //System.out.println(e.getMessage().toString());
                                   }
                        }

                }

                @Override
                public void beforeTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
                    //TODO Auto-generated method stub
                }

                @Override
                public void afterTextChanged(Editable arg0) {
                    //TODO Auto-generated method stub


                }
            });

ASYNC TASK getSearchTags

public class getSearchTags extends AsyncTask<String, Void, String> {
        private final String empId;

        public getSearchTags(String empId) {
            this.empId = empId;
        }

        @Override
        protected void onPreExecute() {
            super.onPreExecute();

        }

        @Override
        protected String doInBackground(String... params) {
            String result= Utils.getSearchTags(connString + "/GetSearchAutoComplete", empId);
            System.out.println("result :" + result + "||");
            if (null == result || result.length() == 2) {
                System.out.println("No results");
                return "0";
            }
            else {
                searchFilter.clear();
                try {

                    JSONArray jsonArray=new JSONArray(result);
                    System.out.println("Length :"+jsonArray.length());
                    for (int i = 0; i < jsonArray.length(); i++) {
                        JSONObject objJson = jsonArray.getJSONObject(i);
                        setFilterItems objItem = new setFilterItems();
                        objItem.setID(objJson.getString("tag_id"));
                        objItem.setImage(objJson.getString("tag_type"));
                        objItem.setName(objJson.getString("tag_name"));

                        searchFilter.add(objItem);

                    }
                } catch (Exception e) {
                    e.printStackTrace();
                    System.out.println("Exception:" + e.getLocalizedMessage() + "||");

                }
                return "1";
            }
        }

        @Override
        protected void onPostExecute(String result) {
            super.onPostExecute(result);
            if(result=="1") {
                //setAdapterToListview();
                searchadapter = new searchRowAdapter(MainActivity_old.this, R.layout.searchrow, searchFilter);
                editText1.setAdapter(searchadapter);
                editText1.setTokenizer(new MultiAutoCompleteTextView.CommaTokenizer());
                searchadapter.notifyDataSetChanged();
                editText1.showDropDown();
                System.out.println("Success");
            }else{
                System.out.println("Error");
            }
        }


    }

Filter Interface

private class NameFilter extends Filter {

        @Override
        protected FilterResults performFiltering(CharSequence constraint) {
            FilterResults filterResults = new FilterResults();
            if (constraint == null || constraint.length() == 0) {
                filterResults.values = originalList;
                filterResults.count = originalList.size();
            } else {
                final String lastToken = constraint.toString().toLowerCase();
                final int count = originalList.size();
                final List<setFilterItems> list = new ArrayList<setFilterItems>();
                setFilterItems contact;

                for (int i = 0; i < count; i++) {
                    contact = originalList.get(i);
                    if (contact.getName().toLowerCase().contains(lastToken)
                            || contact.getName().toLowerCase().startsWith(lastToken)) {
                        list.add(contact);
                    }
                }

                filterResults.values = list;
                filterResults.count = list.size();
            }
            return filterResults;
        }

        @Override
        protected void publishResults(CharSequence constraint, FilterResults results) {
            list = (List<setFilterItems>) results.values;
            if (results.count > 0) {
                notifyDataSetChanged();
            } else {
                notifyDataSetInvalidated();
            }
        }

    }
Vaibs_Cool
  • 6,126
  • 5
  • 28
  • 61

2 Answers2

0

I also had a post related to a good search algorithm. After a lot of trial and errors and tests at home. I did something that worked ok. Didn't have time to finalise it but it is a good starting point.
My solution was to load all the data at once, like search the first letter, grab the data and store it in memory and once the user enter letter, take from the previous loaded list.It will be way faster than loading from server. The problem now appears once the user presses back.
What I did was like delete the entire cache and load from server, every time the user was pressing back.
You can take a look if you want:
https://github.com/toaderandrei/loadcart
Stak post:
Live Search in Android
Things can be simplified, meaning it can be done better, simplified a bit. For example the number of threads and the interaction with them. Hopefully it can help.

Community
  • 1
  • 1
Andrei T
  • 2,985
  • 3
  • 21
  • 28
0

I will suggest you do the following things:

  • Use the same AsyncTask, and don't forget to cancel previous one, since they might just be stacking up on each other, thus, slowing the whole thing
  • Inside of your AsyncTask do:

    protected void doInBackground(Void... args) {
        Thread.setPriority(MIN_PRIORITY)
        ...
    }
    
Iliiaz Akhmedov
  • 867
  • 7
  • 17
  • I am not sending webservice after every word typed.. Only after first word typed data is binded.. but till the time data is binded the user has to wait or phone is in unusable state. – Vaibs_Cool Oct 02 '15 at 09:35