sunil_patidar
sunil_patidar

Reputation: 203

how to disable TextWatcher on setting data to edittext

I am using TextWatcher on EditText .when I input something in edittext it works perfectly but when I set data into edittext from database getting NullPointerException. I used onTouchListener to on this Edittext. my question is. could I check the condition that TextWatcher works only after touch event is true.

EditText det=(EditText) findViewById(R.id.det);
    det.setOnTouchListener(new OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            // TODO Auto-generated method stub
            System.out.println(v.getId());
            d_id=v.getId();
            if (event.getAction() == MotionEvent.ACTION_UP) {
                getPopUp(det);
            }
            return false;
        }
    });

when I call setText on det it goes to TextWatcher

det.setText(ft.get_det().toString());

TextWatcher

                    @Override
            public void afterTextChanged(Editable string) {
                try {

                    int rp=0;
                    det=(EditText)findViewById(d_id);
                    s_id=d_id-4;
                    sev=(EditText)findViewById(s_id);
                    o_id=d_id-2;
                    occ=(EditText)findViewById(o_id);
                    rp=d_id+1;
                    rpn=(TextView)findViewById(rp);
                    if (sev.getText().length() > 0
                            || occ.getText().length() > 0
                            || sev.getText().length() == 0) {
                        if (string.length() > 0) {
                            int s, o, d, r;
                            System.out.println(sev.getId());
                            System.out.println(det.getId());
                            s = Integer.parseInt(sev.getText().toString());
                            o = Integer.parseInt(occ.getText().toString());
                            d = Integer.parseInt(det.getText().toString());
                            r = s * o * d;
                            String m = r + "";
                            rpn.setText(m);                         
                        } else {
                            // det.setText("");
                            // rpn.setText("");
                             System.out.println("In else");
                        //  makeToast();
                        }
                    }

                } catch (Exception e) {
                    det.setText("");
                    rpn.setText("");
                    System.out.println("IN Exception");
                    makeToast();
                }
            }
        };

Upvotes: 8

Views: 15032

Answers (2)

sunil_patidar
sunil_patidar

Reputation: 203

i solved this by taking a global boolen variable.Set this variable true in onTouchListener and check condition in afterTextChanged method that if varible is true of false.

Upvotes: 6

Jitender Dev
Jitender Dev

Reputation: 6925

Create a TextWatcher like below

TextWatcher textWatcher=new TextWatcher() {

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            // TODO Auto-generated method stub

        }

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count,
                int after) {
            // TODO Auto-generated method stub

        }

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

        }
    };

and then to add this to your EditText call

   EditText.addTextChangedListener(textWatcher);

and to remove call

   EditText.removeTextChangedListener(textWatcher);

Upvotes: 29

Related Questions