Функция AutoCompleteTextView afterTextChanged вызывает каждый раз - PullRequest
0 голосов
/ 29 октября 2018

У меня есть один autoCompleteTextView в моем приложении, в котором, когда пользователь заканчивает вводить текст, он проверяет свой метод afterTextChanged и перенаправляет пользователя на следующую страницу подробностей, основываясь на кодах, введенных пользователем, но проблема заключается в том, что когда пользователь щелкает обратно со страницы сведений, он снова переключается детализировать страницу. Метод afterTextChanged вызывается каждый раз. Я назвал это в методе onCreatedView. Как я могу предотвратить это. Пожалуйста, помогите.

Код:

         actSearchCode.addTextChangedListener(new TextWatcher() {

        @Override
        public void afterTextChanged(Editable s) {
            //do nothing


            String product_code = actSearchCode.getText().toString();

            Log.e("TAG", "afterTextChanged: " );

            if(getCodeType.equals("art")) {

                if (actSearchCode.getText().length() != 0 && actSearchCode.getText().length() >= 5) {

                    singleProductDetailList = databaseHelper.getProductByArtCode(product_code);

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

                        Fragment fragment = new ProductDetailFragment();
                        FragmentManager fragmentManager = getFragmentManager(); // this is basically context of the class
                        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
                        Bundle bundle = new Bundle();
                        bundle.putString("product_art", product_code);
                        bundle.putString("source_type", "ean");

                        fragment.setArguments(bundle);
                        fragmentTransaction.replace(R.id.frame, fragment);
                        fragmentTransaction.addToBackStack(null);
                        fragmentTransaction.commit();

                    } else {

                        singleUnrecognisedCheck = new ArrayList<>();
                        singleUnrecognisedCheck = databaseHelper.getUnrecognisedByCode(product_code);

                        Vibrator vibrator = (Vibrator) getActivity().getSystemService(getActivity().VIBRATOR_SERVICE);
                        vibrator.vibrate(1000);

                        MediaPlayer mp = MediaPlayer.create(getActivity(), R.raw.beep);
                        mp.start();

                        if (singleUnrecognisedCheck.size() == 0) {
                            getDialog();
                        } else {
                            Toast.makeText(getActivity(), R.string.unrecognised_code_has_been_added, Toast.LENGTH_SHORT).show();
                        }

                    }

                }

            }
                else{

                if (actSearchCode.getText().length() != 0 && actSearchCode.getText().length() >= 13) {

                    singleProductDetailList = databaseHelper.getProductByArtCode(product_code);

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

                        Fragment fragment = new ProductDetailFragment();
                        FragmentManager fragmentManager = getFragmentManager(); // this is basically context of the class
                        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
                        Bundle bundle = new Bundle();
                        bundle.putString("product_art", product_code);
                        bundle.putString("source_type", "ean");

                        fragment.setArguments(bundle);
                        fragmentTransaction.replace(R.id.frame, fragment);
                        fragmentTransaction.addToBackStack(null);
                        fragmentTransaction.commit();

                    } else {

                        singleUnrecognisedCheck = new ArrayList<>();
                        singleUnrecognisedCheck = databaseHelper.getUnrecognisedByCode(product_code);

                        Vibrator vibrator = (Vibrator) getActivity().getSystemService(getActivity().VIBRATOR_SERVICE);
                        vibrator.vibrate(1000);

                        MediaPlayer mp = MediaPlayer.create(getActivity(), R.raw.beep);
                        mp.start();

                        if (singleUnrecognisedCheck.size() == 0) {

                            codeValue = 1;

                            getDialog();
                        } else {
                            Toast.makeText(getActivity(), R.string.unrecognised_code_has_been_added, Toast.LENGTH_SHORT).show();
                        }


                    }

                }

                }
        }

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            //do nothing
        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            if (s.length() != 0) {
                imgClose.setVisibility(View.VISIBLE);
                imgSourceClose.setVisibility(View.GONE);

            } else {
                imgClose.setVisibility(View.GONE);
                imgSourceClose.setVisibility(View.VISIBLE);

            }
        }
    });


    // article code start

    modelProductCodeList.clear();
    productCodeList.clear();


    if (getCodeType.equals("art")) {
        modelProductCodeList = databaseHelper.getProductsArticleCode();

        for (int k = 0; k < modelProductCodeList.size(); k++) {

            productCodeList.add( modelProductCodeList.get(k).getArticle_code());


        }
        actSearchCode.setAdapter(null);
        adapter = new ArrayAdapter<String>(getActivity(),
                android.R.layout.simple_list_item_1, android.R.id.text1, productCodeList);
        actSearchCode.setAdapter(adapter);

    }


    rgGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(RadioGroup radioGroup, int i) {


            if (i == R.id.rdbArticleCode) {


                actSearchCode.setText("");
                getCodeType = "art";

                modelProductCodeList = databaseHelper.getProductsArticleCode();

                databaseHelper.close();
                productCodeList.clear();

                for (int k = 0; k < modelProductCodeList.size(); k++) {
                    productCodeList.add(modelProductCodeList.get(k).getArticle_code());

                }

                actSearchCode.setAdapter(null);
                adapter = new ArrayAdapter<String>(getActivity(),
                        android.R.layout.simple_list_item_1, android.R.id.text1, productCodeList);
                actSearchCode.setAdapter(adapter);


            } else if (i == R.id.rdbEANCode) {

                actSearchCode.setText("871128");

                getCodeType = "ean";
                modelProductCodeList = databaseHelper.getProductsEANCode();

                databaseHelper.close();
                productCodeList.clear();
                for (int k = 0; k < modelProductCodeList.size(); k++) {
                    productCodeList.add(modelProductCodeList.get(k).getEan_code());

                    Log.e("TAG", "onCheckedChanged: "+modelProductCodeList.get(k).getEan_code() );

                }

                actSearchCode.setAdapter(null);
                adapter = new ArrayAdapter<String>(getActivity(),
                        android.R.layout.simple_list_item_1, android.R.id.text1, productCodeList);
                actSearchCode.setAdapter(adapter);

            }

        }
    });


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

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

            Fragment fragment = new ProductDetailFragment();
            FragmentManager fragmentManager = getFragmentManager(); // this is basically context of the class
            FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
            Bundle bundle = new Bundle();
            bundle.putString("product_art", (String) adapterView.getItemAtPosition(i) );
            bundle.putString("source_type", getCodeType);

            fragment.setArguments(bundle);
            fragmentTransaction.replace(R.id.frame, fragment);
            fragmentTransaction.addToBackStack(null);
            fragmentTransaction.commit();

        }
    });

1 Ответ

0 голосов
/ 29 октября 2018
        final static boolean flag=false;//define this in the class before on create/on createView


        ----------


        actSearchCode.addTextChangedListener(new TextWatcher() {


                @Override
                public void afterTextChanged(Editable s) {
                    //do nothing

         if(!flag)
        {


                    Log.e("TAG", "afterTextChanged: " );

                    if(getCodeType.equals("art")) {

                        if (actSearchCode.getText().length() != 0 && actSearchCode.getText().length() >= 5) {

                            singleProductDetailList = databaseHelper.getProductByArtCode(product_code);

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

                           flag=true;

                                Fragment fragment = new ProductDetailFragment();
                                FragmentManager fragmentManager = getFragmentManager(); // this is basically context of the class
                                FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
                                Bundle bundle = new Bundle();
                                bundle.putString("product_art", product_code);
                                bundle.putString("source_type", "ean");

                                fragment.setArguments(bundle);
                                fragmentTransaction.replace(R.id.frame, fragment);
                                fragmentTransaction.addToBackStack(null);
                                fragmentTransaction.commit();


                            } 
  }else{
        flag=false;
        }
...