1

I have added AutoCompleteTextView in my app. In which I have coded on both methods i.e. afterTextChange() if a person writes complete word and do not click on suggestion and OnItemClickListener() if a user clicks on sugesstion but the problem is that these both methods are working together. If I click on suggestions using onItemClick then afterTextChange( ) also works and my code runs two times. What to do to prevent both methods not run same time.

OnAfterTextChange() Code

 actvShop_name.addTextChangedListener(new TextWatcher() {

        @Override
        public void afterTextChanged(Editable s) {

                   actvShop_name.setVisibility(View.VISIBLE);
            txtShop.setVisibility(View.GONE);
            //do nothing
            if ((actvShop_name.getText().length() != 0) && actvShop_name.getText().length() >= 3) {
                modelShopDetailsArrayList = databaseHelper.getShopDetail(actvShop_name.getText().toString(), language);

                Log.e("TAG", "onKeyDown:Add " + modelShopDetailsArrayList.size() + " " + actvShop_name.getText().toString() + language);

                if (modelShopDetailsArrayList.size() != 0) {

                    HashMap<String, String> shopDetails = session.getSelectedShopDetail();

                    Log.e("TAG", "onItemClick: " + shopDetails.get("shop_name_nl") + "  ");

                    if (shop_name != null) {

                        if (shop_name == actvShop_name.getText().toString()) {

                            InputMethodManager imm = (InputMethodManager) getActivity().getSystemService(
                                    Context.INPUT_METHOD_SERVICE);
                            imm.hideSoftInputFromWindow(actvShop_name.getApplicationWindowToken(), 0);

                            fillShopDetails(actvShop_name.getText().toString());

                        } else {


                            AlertDialog.Builder alertDialog = new AlertDialog.Builder(getActivity());
                            alertDialog.setMessage(R.string.delete_cart);
                            alertDialog.setPositiveButton(R.string.yes, new DialogInterface.OnClickListener() {
                                public void onClick(DialogInterface dialog, int which) {

                                    DatabaseHelper databaseHelper = new DatabaseHelper(getActivity());
                                    databaseHelper.deleteCart();

                                    InputMethodManager imm = (InputMethodManager) getActivity().getSystemService(
                                            Context.INPUT_METHOD_SERVICE);
                                    imm.hideSoftInputFromWindow(actvShop_name.getApplicationWindowToken(), 0);

                                    fillShopDetails(actvShop_name.getText().toString());


                                }
                            });
                            alertDialog.setNegativeButton(R.string.no
                                    , new DialogInterface.OnClickListener() {
                                        public void onClick(DialogInterface dialog, int which) {
                                            dialog.cancel();
                                        }
                                    });
                            alertDialog.show();


                        }

                    } else {
                        InputMethodManager imm = (InputMethodManager) getActivity().getSystemService(
                                Context.INPUT_METHOD_SERVICE);
                        imm.hideSoftInputFromWindow(actvShop_name.getApplicationWindowToken(), 0);

                        fillShopDetails(actvShop_name.getText().toString());
                    }

OnItemClickListener() Code

 actvShop_name.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {


          //  actvShop_name.removeTextChangedListener(textWatcher);

            shop_name_entered = adapterView.getItemAtPosition(i).toString();

            actvShop_name.setVisibility(View.GONE);
            txtShop.setVisibility(View.VISIBLE);
            txtShop.setText(shop_name_entered);


            HashMap<String, String> shopDetails = session.getSelectedShopDetail();

            Log.e("TAG", "onItemClick: " + shopDetails.get("shop_name_nl") + "  " + shop_name_entered);

            if (shop_name != null) {

                if (shop_name.equals(shop_name_entered)) {

                    InputMethodManager imm = (InputMethodManager) getActivity().getSystemService(
                            Context.INPUT_METHOD_SERVICE);
                    imm.hideSoftInputFromWindow(actvShop_name.getApplicationWindowToken(), 0);

                    fillShopDetails(shop_name_entered);

                } else {

                    AlertDialog.Builder alertDialog = new AlertDialog.Builder(getActivity());
                    alertDialog.setMessage(R.string.delete_cart);
                    alertDialog.setPositiveButton(R.string.yes, new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int which) {

                            DatabaseHelper databaseHelper = new DatabaseHelper(getActivity());
                            databaseHelper.deleteCart();

                            InputMethodManager imm = (InputMethodManager) getActivity().getSystemService(
                                    Context.INPUT_METHOD_SERVICE);
                            imm.hideSoftInputFromWindow(actvShop_name.getApplicationWindowToken(), 0);

                            fillShopDetails(shop_name_entered);


                        }
                    });
                    alertDialog.setNegativeButton(R.string.no
                            , new DialogInterface.OnClickListener() {
                                public void onClick(DialogInterface dialog, int which) {
                                    dialog.cancel();
                                }
                            });
                    alertDialog.show();
                }

            } else {
                InputMethodManager imm = (InputMethodManager) getActivity().getSystemService(
                        Context.INPUT_METHOD_SERVICE);
                imm.hideSoftInputFromWindow(actvShop_name.getApplicationWindowToken(), 0);

                fillShopDetails(shop_name_entered);
            }


        }
    });


    actvShop_name.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            actvShop_name.setVisibility(View.VISIBLE);
            txtShop.setVisibility(View.GONE);
        }
    });
    txtShop.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            actvShop_name.setVisibility(View.VISIBLE);
            txtShop.setVisibility(View.GONE);
        }
    });

Dialog opened two times as both methods are running together.

Dhaiyur
  • 498
  • 4
  • 14
mishti
  • 183
  • 7
  • 20

1 Answers1

0

I was stuck with the same problem, which you are facing now.

As we are using the addTextChangedListenerthan it will fire when any change will happen to your EditText either from the typing or setting the text programmatically (like selecting a value from the list and then set on Editext)

You can use the two approaches for it.

  1. Disable the addTextChangedListener listener from the AutoCompleteTextView. For this click here
  2. Play with view visibility (which I have used successfully). For this, you need to create a new view (you can create TextView) on your AutoCompleteTextView and set the visibility of the view to GONE by default than we will play the view visibility programmatically like inside the addTextChangedListener use below code

Code:

  @Override
    public void afterTextChanged(Editable s) {
        //do nothing
    YOUR_AUTO_COMPLTE_TEXTVIEW.setVisibility(View.VISIBLE);
    YOUR_TEXTVIEW.setVisibility(View.GONE);

    }

And inside the setOnItemClickListener again we need to play view visibility as follow and set the set text on the new Textview check below:-

actvShop_name.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {

                YOUR_AUTO_COMPLTE_TEXTVIEW.setVisibility(View.GONE);
                YOUR_TEXTVIEW.setVisibility(View.VISIBLE);

            }
}

And apply click on the YOUR_AUTO_COMPLTE_TEXTVIEW and the YOUR_TEXTVIEW to set the view visibility like below

YOUR_AUTO_COMPLTE_TEXTVIEW.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                YOUR_AUTO_COMPLTE_TEXTVIEW.setVisibility(View.VISIBLE);
                YOUR_TEXTVIEW.setVisibility(View.GONE);
            }
        });
        YOUR_TEXTVIEW.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                YOUR_AUTO_COMPLTE_TEXTVIEW.setVisibility(View.VISIBLE);
                YOUR_TEXTVIEW.setVisibility(View.GONE);
            }
        });
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Ravindra Kushwaha
  • 7,846
  • 14
  • 53
  • 103