1

below is my textwatcher class I am trying to change the textview content of my list based on the changes from the edittext of the list.

   public class MtQtyWatcher implements TextWatcher {
            View v;
            public MtQtyWatcher(View v){
                this.v = v;         
            }

            public void afterTextChanged(Editable s) {
                String qty = s.toString().trim();
                int quantity = qty.equals("")?0:Integer.parseInt(qty);
                EditText edtQty = (EditText)v.findViewById(R.id.qty);
                Item item = (Item)edtQty.getTag();

                if(item.getQty()!=quantity){
                    edtQty.setText(qty);
                    item.setQty(quantity);
                    float newRate = quantity * (item.getRate());
                    item.setAmt(newRate);
                    TextView txtAmt = (TextView) v.findViewById(R.id.amt);  
                    txtAmt.setText(""+item.getAmt());
                }
            }

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

            public void onTextChanged(CharSequence s, int start, int before,int count) {}

        }

This is the following stacktrace

04-10 15:00:13.179: E/AndroidRuntime(24454): FATAL EXCEPTION: main 
04-10 15:00:13.179: E/AndroidRuntime(24454): java.lang.StackOverflowError 
04-10 15:00:13.179: E/AndroidRuntime(24454):    at android.text.SpannableStringBuilder.getSpans(SpannableStringBuilder.java:783) 
04-10 15:00:13.179: E/AndroidRuntime(24454):    at android.text.method.ReplacementTransformationMethod$SpannedReplacementCharSequence.getSpans(ReplacementTransformationMethod.java:184) 
04-10 15:00:13.179: E/AndroidRuntime(24454):    at android.text.Styled.drawUniformRun(Styled.java:67) 
04-10 15:00:13.179: E/AndroidRuntime(24454):    at android.text.Styled.drawDirectionalRun(Styled.java:322) 
04-10 15:00:13.179: E/AndroidRuntime(24454):    at android.text.Styled.measureText(Styled.java:454) 
04-10 15:00:13.179: E/AndroidRuntime(24454):    at android.text.Layout.measureText(Layout.java:1971) 
04-10 15:00:13.179: E/AndroidRuntime(24454):    at android.text.Layout.getLineMax(Layout.java:912) 
04-10 15:00:13.179: E/AndroidRuntime(24454):    at android.text.Layout.getLineMax(Layout.java:886) 
04-10 15:00:13.179: E/AndroidRuntime(24454):    at android.text.Layout.getHorizontal(Layout.java:742) 
04-10 15:00:13.179: E/AndroidRuntime(24454):    at android.text.Layout.getHorizontal(Layout.java:703) 
04-10 15:00:13.179: E/AndroidRuntime(24454):    at android.text.Layout.getPrimaryHorizontal(Layout.java:688) 
04-10 15:00:13.179: E/AndroidRuntime(24454):    at android.widget.TextView.getFocusedRect(TextView.java:4522) 
04-10 15:00:13.179: E/AndroidRuntime(24454):    at android.view.FocusFinder.findNextFocus(FocusFinder.java:72)
04-10 15:00:13.179: E/AndroidRuntime(24454):    at android.view.ViewGroup.focusSearch(ViewGroup.java:476) 
04-10 15:00:13.179: E/AndroidRuntime(24454):    at android.view.ViewGroup.focusSearch(ViewGroup.java:478) 
04-10 15:00:13.179: E/AndroidRuntime(24454):    at android.view.ViewGroup.focusSearch(ViewGroup.java:478) 
04-10 15:00:13.179: E/AndroidRuntime(24454):    at android.view.ViewGroup.focusSearch(ViewGroup.java:478) 
04-10 15:00:13.179: E/AndroidRuntime(24454):    at android.view.ViewGroup.focusSearch(ViewGroup.java:478) 
04-10 15:00:13.179: E/AndroidRuntime(24454):    at android.view.ViewGroup.focusSearch(ViewGroup.java:478) 
04-10 15:00:13.179: E/AndroidRuntime(24454):    at android.view.ViewGroup.focusSearch(ViewGroup.java:478) 
04-10 15:00:13.179: E/AndroidRuntime(24454):    at android.view.View.focusSearch(View.java:3494) 
04-10 15:00:13.179: E/AndroidRuntime(24454):    at android.widget.TextView.onCreateInputConnection(TextView.java:4922) 
04-10 15:00:13.179: E/AndroidRuntime(24454):    at android.view.inputmethod.InputMethodManager.startInputInner(InputMethodManager.java:945) 
04-10 15:00:13.179: E/AndroidRuntime(24454):    at android.view.inputmethod.InputMethodManager.restartInput(InputMethodManager.java:900) 
04-10 15:00:13.179: E/AndroidRuntime(24454):    at android.widget.TextView.setText(TextView.java:2734) 
04-10 15:00:13.179: E/AndroidRuntime(24454):    at android.widget.TextView.setText(TextView.java:2676) 
04-10 15:00:13.179: E/AndroidRuntime(24454):    at android.widget.EditText.setText(EditText.java:178) 
04-10 15:00:13.179: E/AndroidRuntime(24454):    at android.widget.TextView.setText(TextView.java:2651) 
04-10 15:00:13.179: E/AndroidRuntime(24454):    at com.eshopping.u.ItemList$MtQtyWatcher.afterTextChanged(ItemList.java:144) 
04-10 15:00:13.179: E/AndroidRuntime(24454):    at android.widget.TextView.sendAfterTextChanged(TextView.java:6724) 
04-10 15:00:13.179: E/AndroidRuntime(24454):    at android.widget.TextView.setText(TextView.java:2822) 
04-10 15:00:13.179: E/AndroidRuntime(24454):    at android.widget.TextView.setText(TextView.java:2676) 
04-10 15:00:13.179: E/AndroidRuntime(24454):    at android.widget.EditText.setText(EditText.java:178) 
04-10 15:00:13.179: E/AndroidRuntime(24454):    at android.widget.TextView.setText(TextView.java:2651) 
04-10 15:00:13.179: E/AndroidRuntime(24454):    at com.eshopping.u.ItemList$MtQtyWatcher.afterTextChanged(ItemList.java:144) 
04-10 15:00:13.179: E/AndroidRuntime(24454):    at android.widget.TextView.sendAfterTextChanged(TextView.java:6724) 
04-10 15:00:13.179: E/AndroidRuntime(24454):    at android.widget.TextView.setText(TextView.java:2822) 
04-10 15:00:13.179: E/AndroidRuntime(24454):    at android.widget.TextView.setText(TextView.java:2676) 
04-10 15:00:13.179: E/AndroidRuntime(24454):    at android.widget.EditText.setText(EditText.java:178) 
04-10 15:00:13.179: E/AndroidRuntime(24454):    at android.widget.TextView.setText(TextView.java:2651) 
04-10 15:00:13.179: E/AndroidRuntime(24454):    at com.eshopping.u.ItemList$MtQtyWatcher.afterTextChanged(ItemList.java:144) 
04-10 15:00:13.179: E/AndroidRuntime(24454):    at android.widget.TextView.sendAfterTextChanged(TextView.java:6724) 
04-10 15:00:13.179: E/AndroidRuntime(24454):    at android.widget.TextView.setText(TextView.java:2822) 
04-10 15:00:13.179: E/AndroidRuntime(24454):    at android.widget.TextView.setText(TextView.java:2676) 
04-10 15:00:13.179: E/AndroidRuntime(24454):    at android.widget.EditText.setText(EditText.java:178) 
04-10 15:00:13.179: E/AndroidRuntime(24454):    at android.widget.TextView.setText(TextView.java:2651) 
04-10 15:00:13.179: E/AndroidRuntime(24454):    at com.eshopping.u.ItemList$MtQtyWatcher.afterTextChanged(ItemList.java:144) 
04-10 15:00:13.179: E/AndroidRuntime(24454):    at android.widget.TextView.sendAfterTextChanged(TextView.java:6724) 
04-10 15:00:13.179: E/AndroidRuntime(24454):    at android.widget.TextView.setText(TextView.java:2822) 
04-10 15:00:13.179: E/AndroidRuntime(24454):    at android.widget.TextView.setText(TextView.java:2676) 
04-10 15:00:13.179: E/AndroidRuntime(24454):    at android.widget.EditText.setText(EditText.java:178) 
04-10 15:00:13.179: E/AndroidRuntime(24454):    at android.widget.TextView.setText(TextView.java:2651) 
04-10 15:00:13.179: E/AndroidRuntime(24454):    at com.eshopping.u.ItemList$MtQtyWatcher.afterTextChanged(ItemList.java:144) 
04-10 15:00:13.179: E/AndroidRuntime(24454):    at android.widget.TextView.sendAfterTextChanged(TextView.java:6724) 
04-10 15:00:13.179: E/AndroidRuntime(24454):    at android.widget.TextView.setText(TextView.java:2822) 
04-10 15:00:13.179: E/AndroidRuntime(24454):    at android.widget.TextView.setText(TextView.java:2676) 
04-10 15:00:13.179: E/AndroidRuntime(24454):    at android.widget.EditText.setText(EditText.java:178) 
04-10 15:00:13.179: E/AndroidRuntime(24454):    at android.widget.TextView.setText(TextView.java:2651) 
04-10 15:00:13.179: E/AndroidRuntime(24454):    at com.eshopping.u.ItemList$MtQtyWatcher.afterTextChanged(ItemList.java:144) 
04-10 15:00:13.179: E/AndroidRuntime(24454):    at android.widget.TextView.sendAfterTextChanged(TextView.java:6724) 
04-10 15:00:13.179: E/AndroidRuntime(24454):    at android.widget.TextView.setText(TextView.java:2822) 
04-10 15:00:13.179: E/AndroidRuntime(24454):    at android.widget.TextView.setText(TextView.java:2676) 
04-10 15:00:13.179: E/AndroidRuntime(24454):    at android.widget.EditText.setText(EditText.java:178) 
04-10 15:00:13.179: E/AndroidRuntime(24454):    at android.widget.TextView.setText(TextView.java:2651) 
04-10 15:00:13.179: E/AndroidRuntime(24454):    a
4

5 回答 5

3

I found a solution by removing the code from the afterTextChanged and adding the same to the onTextChanged

public class MtQtyWatcher implements TextWatcher {
        View view ;
        public MtQtyWatcher(View view ){
            this.view = view;
        }
        public void afterTextChanged(Editable s) {}
        public void beforeTextChanged(CharSequence s, int start, int count,int after) {}
        public void onTextChanged(CharSequence s, int start, int before,int count) {            
            if(s.length()>0){
                try{
                    String strQty = s.toString().trim();
                    int quantity = Integer.parseInt(strQty);
                    EditText txtQty = (EditText) view.findViewById(R.id.qty);
                    Item item = (Item)txtQty.getTag();                  
                    if (item.getQty()!=quantity){
                        item.setQty(quantity);
                        TextView txtAmt = (TextView) view.findViewById(R.id.amt);
                        float amt = quantity * (item.getRate());
                        item.setAmt(amt);
                        txtAmt.setText(String.valueOf(amt));
                    }
                }catch(Exception e){
                    Log.e(Tag,"onTextChnaged "+e);
                    e.printStackTrace();
                }
            }
        }
    }
于 2013-04-12T10:49:11.070 回答
2

在我看来,观察者正在被其中一个 setTexts 递归调用。

解决这个问题的一种方法是添加一个布尔值来停止重新进入。

public class MtQtyWatcher implements TextWatcher {

    boolean running = false;
    View v;

    public MtQtyWatcher(View v){
        this.v = v;         
    }

    public void afterTextChanged(Editable s) {
        if(!running){
            running = true;
            // add your stuff here
            running = false;
        }
    }
}
于 2013-04-10T15:23:11.053 回答
0

您的 TextWatcher 是否附加到 EditText edtQty?如果是这样,您将获得无限递归,因为您正在修改 EditText 中的文本,该文本将再次调用 afterTextChanged。

于 2013-04-10T15:22:31.533 回答
0

TextWatcher 中的 setText 导致无限堆栈循环。

我建议改用 InputFilter。

于 2013-04-10T15:27:58.703 回答
0

您也可以在调用 setText 方法之前从 edittext 中删除 textwatcher;在 settext 之后,您需要再次添加 textwatcher。

于 2015-12-11T14:51:08.370 回答