0

Below code is part of search box in my android app. LoadCatalog is a async task for the api call, problem is whenever it is being called the editText stops taking new character for a fraction of second(skips a character in middle).

for ex- if the user want to enter "The book of leaves"... it only sometimes take "The boo of " or "The bookof "

It skips the character, pls suggest what's wrong in my code.

private TextWatcher productEntered = new TextWatcher() {
    long lastChange = 0;

    public void beforeTextChanged(CharSequence s, int start, int count, int after) {
    }

    public void onTextChanged(CharSequence enteredSequence, int start, int before, int count) {
        searchbarActionClear.setVisibility(View.VISIBLE);
        enteredText = enteredSequence;



        if (CommonUtils.isConnectingToInternet(DashboardActivity.this)) {
            if (enteredText.length() > 3) {
                new Handler().postDelayed(new Runnable() {
                    public void run() {
                        if (System.currentTimeMillis() - lastChange >= 600) {
                            resetList();
                            toolbarSuggestionEditText.setTag(toolbarSuggestionEditText.getKeyListener());
                            toolbarSuggestionEditText.setKeyListener(null);
                            new LoadCatalog().execute(String.valueOf(enteredText));
                        }
                    }
                }, 600);
                lastChange = System.currentTimeMillis();
            }
        }
    }


    public void afterTextChanged(Editable s) {
    }
};



   private class LoadCatalog extends AsyncTask<String, Void, CustomResponse> {
    @Override
    protected CustomResponse doInBackground(String... params) {
        String url;
        if (categoryItem != null) {
            url = String.format(AppConstants.URLs.SEARCH_WITH_CATEGORY, params[0], categoryItem);
        } else {

            url = String.format(AppConstants.URLs.SEARCH, params[0]);
        }

        CustomResponse response = HttpRequest.GET_REQUEST(url, DashboardActivity.this);

        return response;
    }

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

    @Override
    protected void onPostExecute(CustomResponse result) {
        try {
            if (result.getResponseCode() == 200) {

                JSONArray jsonArray = null;
                jsonArray = new JSONArray(result.getResponseBody());
                Suggestion suggestion = null;
                if (jsonArray.length() > 0) {
                    suggestionList.clear();
                    suggestionList.add(new Suggestion(null, Suggestion.TYPE_SUGGESTION_HEADER));
                    for (int i = 0; i < jsonArray.length(); i++) {
                        JSONObject jsonObject = jsonArray.getJSONObject(i);

                        suggestion = new Suggestion(jsonObject.getString("name"),
                                jsonObject.getString("category"),
                                Suggestion.TYPE_SUGGESTION);
                        suggestionList.add(suggestion);
                        suggestionAdapter.notifyDataSetChanged();
                    }

                    toolbarSuggestionEditText.setKeyListener((KeyListener) toolbarSuggestionEditText.getTag());

                } else {

                    toolbarSuggestionEditText.setKeyListener((KeyListener) toolbarSuggestionEditText.getTag());
                    Toast.makeText(DashboardActivity.this, "No item match with your search", Toast.LENGTH_SHORT).show();
                    suggestionList.clear();

                }
            } else {

                toolbarSuggestionEditText.setKeyListener((KeyListener) toolbarSuggestionEditText.getTag());
            }
        } catch (JSONException e) {

            toolbarSuggestionEditText.setKeyListener((KeyListener) toolbarSuggestionEditText.getTag());
            e.printStackTrace();
        }
    }
}
ts178
  • 321
  • 1
  • 6
  • 20
  • Please post the code of `LoadCatalog`. It is likely blocking the UI thread somewhere. – Ovidiu Oct 10 '18 at 14:32
  • Try this : https://github.com/JakeWharton/RxBinding – Krzysztof Kubicki Oct 10 '18 at 14:33
  • @Ovidiu Any idea from the code now? – ts178 Oct 11 '18 at 04:42
  • I am using recyclerview for list, I am guessing here whenever the list is getting updated, it's blocking the UI thread...and suggestions – ts178 Oct 11 '18 at 07:20
  • @priyanka178 Simply updating the recyclerview would not block the UI thread, however you are calling `suggestionAdapter.notifyDataSetChanged();` within the for loop, which may block the UI thread depending on how large the array is. Move that line of code outside of the for loop (after the loop) and check if there's any improvement. – Ovidiu Oct 11 '18 at 08:21
  • @priyanka178 The only other potential issue with `LoadCatalog` is if the result is massive, in which case the UI thread might be blocked for a short while while the result is parsed to a `JSONArray`. To get around this issue, you could parse the result and recreate your `sugggestionList` within `doInBackground`, and only call `suggestionAdapter.notifyDataSetChanged();` within `onPostExecute`. – Ovidiu Oct 11 '18 at 08:23
  • @Ovidiu any idea on Rx java part.. how this can be implemented with that – ts178 Oct 12 '18 at 05:55
  • @priyanka178 So I guess that didn't help then? One other thing I would try in order to narrow it down is to comment out `suggestionAdapter.notifyDataSetChanged();` and see if that makes a difference. It is not a solution as your suggestions list will no longer be updated, but it can help with figuring out whether that's what's freezing the UI. I'm not using RxJava myself so I don't know how that would work. – Ovidiu Oct 12 '18 at 11:04
  • @Ovidiu thanks for all the suggestions.. I tried almost all... no good yet.. I have reduced the result count to 5, even then the problem persists. What I noticed here is whenever the recycler view gets updated, it's blocking the keyboard for a fraction of second. – ts178 Oct 13 '18 at 04:03
  • @priyanka178 That means the recycler adapter's `onBindViewHolder` is probably blocking the UI thread. Are you loading images or reading files or performing other intensive tasks in there? – Ovidiu Oct 14 '18 at 21:27

0 Answers0