gkiar
gkiar

Reputation: 478

How to replace a textChangedListener in an activity

For my android application, I need to change the textChangedListener each time a RadioButton switches states. There are 4 possible buttons, and a corresponding EditText box for each. I currently only have 2 of the 4 buttons implemented, and when I switch from an implemented button (i.e. button 1) to an unimplemented button, there is no problem. However, when I try to select another implemented button, (i.e. button 2) the application crashes.

Bellow is some code of mine showing the implementation of my textChangedListeners.

public TextWatcher last;
public TextWatcher _list1;
public TextWatcher _list2;

public EditText active;
public RadioButton checked;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    final EditText t1 = (EditText) findViewById(R.id.1text);
    final EditText t2 = (EditText) findViewById(R.id.2text);
    final EditText t3 = (EditText) findViewById(R.id.3text);
    final EditText t4 = (EditText) findViewById(R.id.4text);

    final RadioGroup g1 = (RadioGroup) findViewById(R.id.radioGroup1);
    g1.setOnCheckedChangeListener(new OnCheckedChangeListener() {
        public void onCheckedChanged(RadioGroup group, int checkedId) {
            checked = (RadioButton)g1.findViewById(g1.getCheckedRadioButtonId());

            if (((String) checked.getText()).equalsIgnoreCase("But1"))
            {
                active.removeTextChangedListener(last);
                active = t1;
                active.setText("");
                active.addTextChangedListener(_list1);
                last = _list1;
            }
            else if (((String) checked.getText()).equalsIgnoreCase("But2"))
            {
                active.removeTextChangedListener(last);
                active = t2;
                active.setText("");
                active.addTextChangedListener(_list2);
                last = _list2;
            }
            else if (((String) checked.getText()).equalsIgnoreCase("But3"))
            {
                active = t3;
            }
            else if (((String) checked.getText()).equalsIgnoreCase("But4"))
            {
                active = t4;
            }
        }
    });

    checked = (RadioButton)g1.findViewById(g1.getCheckedRadioButtonId());

    active = t1;
    active.setText("");
    _list1 = (new TextWatcher(){
        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)
        {
            %Useful stuff
        }
    });
    _list2 = (new TextWatcher(){
        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)
        {
            %Different useful stuff
        }
    });
    active.addTextChangedListener(_list1);
    last = _list1;
}

Again, my problem is that when I try to switch from one TextChangedListener to another, my application crashes regardless of what is implemented within each listener. I feel like there is something very obvious which I am overlooking for the characteristics and implementation of the TextChangeListeners. Any help would be much appreciated!

Thanks!

My Error Log: (Note: the activity name is "Number")

01-31 08:29:17.673: INFO/ActivityManager(59): Displayed activity com.ikiar.engtools/.Number: 659 ms (total 659 ms)
01-31 08:29:24.083: DEBUG/AndroidRuntime(276): Shutting down VM
01-31 08:29:24.083: WARN/dalvikvm(276): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
01-31 08:29:24.103: ERROR/AndroidRuntime(276): FATAL EXCEPTION: main
01-31 08:29:24.103: ERROR/AndroidRuntime(276): java.lang.ClassCastException: android.text.SpannableStringBuilder
01-31 08:29:24.103: ERROR/AndroidRuntime(276):     at com.ikiar.engtools.Number$3.onTextChanged(Number.java:122)
01-31 08:29:24.103: ERROR/AndroidRuntime(276):     at android.widget.TextView.sendOnTextChanged(TextView.java:6131)
01-31 08:29:24.103: ERROR/AndroidRuntime(276):     at android.widget.TextView.setText(TextView.java:2691)
01-31 08:29:24.103: ERROR/AndroidRuntime(276):     at android.widget.TextView.setText(TextView.java:2556)
01-31 08:29:24.103: ERROR/AndroidRuntime(276):     at android.widget.EditText.setText(EditText.java:75)
01-31 08:29:24.103: ERROR/AndroidRuntime(276):     at android.widget.TextView.setText(TextView.java:2531)
01-31 08:29:24.103: ERROR/AndroidRuntime(276):     at com.ikiar.engtools.Number$1.onCheckedChanged(Number.java:71)
01-31 08:29:24.103: ERROR/AndroidRuntime(276):     at android.widget.RadioGroup.setCheckedId(RadioGroup.java:172)
01-31 08:29:24.103: ERROR/AndroidRuntime(276):     at android.widget.RadioGroup.access$600(RadioGroup.java:52)
01-31 08:29:24.103: ERROR/AndroidRuntime(276):     at android.widget.RadioGroup$CheckedStateTracker.onCheckedChanged(RadioGroup.java:342)
01-31 08:29:24.103: ERROR/AndroidRuntime(276):     at android.widget.CompoundButton.setChecked(CompoundButton.java:127)
01-31 08:29:24.103: ERROR/AndroidRuntime(276):     at android.widget.CompoundButton.toggle(CompoundButton.java:86)
01-31 08:29:24.103: ERROR/AndroidRuntime(276):     at android.widget.RadioButton.toggle(RadioButton.java:69)
01-31 08:29:24.103: ERROR/AndroidRuntime(276):     at android.widget.CompoundButton.performClick(CompoundButton.java:98)
01-31 08:29:24.103: ERROR/AndroidRuntime(276):     at android.view.View$PerformClick.run(View.java:8816)
01-31 08:29:24.103: ERROR/AndroidRuntime(276):     at android.os.Handler.handleCallback(Handler.java:587)
01-31 08:29:24.103: ERROR/AndroidRuntime(276):     at android.os.Handler.dispatchMessage(Handler.java:92)
01-31 08:29:24.103: ERROR/AndroidRuntime(276):     at android.os.Looper.loop(Looper.java:123)
01-31 08:29:24.103: ERROR/AndroidRuntime(276):     at android.app.ActivityThread.main(ActivityThread.java:4627)
01-31 08:29:24.103: ERROR/AndroidRuntime(276):     at java.lang.reflect.Method.invokeNative(Native Method)
01-31 08:29:24.103: ERROR/AndroidRuntime(276):     at java.lang.reflect.Method.invoke(Method.java:521)
01-31 08:29:24.103: ERROR/AndroidRuntime(276):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
01-31 08:29:24.103: ERROR/AndroidRuntime(276):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
01-31 08:29:24.103: ERROR/AndroidRuntime(276):     at dalvik.system.NativeStart.main(Native Method)
01-31 08:29:24.113: WARN/ActivityManager(59):   Force finishing activity com.ikiar.engtools/.Number
01-31 08:29:24.523: INFO/ARMAssembler(59): generated scanline__00000077:03515104_00000000_00000000 [ 33 ipp] (47 ins) at [0x3659b0:0x365a6c] in 365498 ns
01-31 08:29:24.653: WARN/ActivityManager(59): Activity pause timeout for HistoryRecord{450232b0 com.ikiar.engtools/.Number}

Upvotes: 1

Views: 1134

Answers (3)

Vivek
Vivek

Reputation: 13238

I think there is some problem in your layout file.

I have edited the code and created main.xml and changed EditText ids from 1text,2text,3text,4text to text1,text2,text3,text4

The code given bellow is working.

Following is the code for activity

    public TextWatcher last;
    public TextWatcher _list1;
    public TextWatcher _list2;

    public EditText active;
    public RadioButton checked;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        final EditText t1 = (EditText) findViewById(R.id.text1);
        final EditText t2 = (EditText) findViewById(R.id.text2);
        final EditText t3 = (EditText) findViewById(R.id.text3);
        final EditText t4 = (EditText) findViewById(R.id.text4);
        final Context c = getApplicationContext();

        final RadioGroup g1 = (RadioGroup) findViewById(R.id.radioGroup1);
        g1.setOnCheckedChangeListener(new OnCheckedChangeListener() {
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                checked = (RadioButton) g1.findViewById(g1.getCheckedRadioButtonId());

                if (((String) checked.getText()).equalsIgnoreCase("But1")) {
                    active.removeTextChangedListener(last);
                    active = t1;
                    active.setText("");
                    active.addTextChangedListener(_list1);
                    last = _list1;
                } else if (((String) checked.getText()).equalsIgnoreCase("But2")) {
                    active.removeTextChangedListener(last);
                    active = t2;
                    active.setText("");
                    active.addTextChangedListener(_list2);
                    last = _list2;
                } else if (((String) checked.getText()).equalsIgnoreCase("But3")) {
                    active = t3;
                } else if (((String) checked.getText()).equalsIgnoreCase("But4")) {
                    active = t4;
                }
            }
        });

        checked = (RadioButton) g1.findViewById(g1.getCheckedRadioButtonId());

        active = t1;
        active.setText("");
        _list1 = (new TextWatcher() {
            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) {
                Toast.makeText(c, "onTextChanged called(_list1 watcher)", Toast.LENGTH_SHORT).show();
            }
        });
        _list2 = (new TextWatcher() {
            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) {
                Toast.makeText(c, "onTextChanged called(_list2 watcher)", Toast.LENGTH_SHORT).show();
            }
        });
        active.addTextChangedListener(_list1);
        last = _list1;
    }

}


Following is the code for main.xml file

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <RadioGroup
        android:id="@+id/radioGroup1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <RadioButton
            android:id="@+id/But1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="But1" />

        <EditText
            android:id="@+id/text1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:inputType="text" />

        <RadioButton
            android:id="@+id/But2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="But2" />

        <EditText
            android:id="@+id/text2"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:inputType="text" />

        <RadioButton
            android:id="@+id/But3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="But3" />

        <EditText
            android:id="@+id/text3"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:inputType="text" />

        <RadioButton
            android:id="@+id/But4"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="But4" />

        <EditText
            android:id="@+id/text4"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:inputType="text" >
        </EditText>
    </RadioGroup>

</LinearLayout>

Try this. Its working.

Upvotes: 2

ngesh
ngesh

Reputation: 13501

Seems like your EditText is not having any listeners when the method setText("") is called, since you are removing it just before call this method.. So how about doing this,

if (((String) checked.getText()).equalsIgnoreCase("But1"))
            {
                active.setText("");
                active.removeTextChangedListener(last);
                active = t1;

                active.addTextChangedListener(_list1);
                last = _list1;
            }
            else if (((String) checked.getText()).equalsIgnoreCase("But2"))
            {
                active.setText("");

               active.removeTextChangedListener(last);
                active = t2;
                                active.addTextChangedListener(_list2);
                last = _list2;
            }

Upvotes: 0

Intathep
Intathep

Reputation: 3388

hope this help

if but 1

    t1.addTextChangedListener(new TextWatcher(){

        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) {
            // TODO Auto-generated method stub
                            do stuff
        }});

else if but 1

    t1.addTextChangedListener(new TextWatcher(){

        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) {
            // TODO Auto-generated method stub
                           another stuff
        }});

Upvotes: 0

Related Questions