1

Hello I am trying to use a viewswitcher to go back and forth between and TextView and an EditText. My view switches to an edittext from a textview. But does not switch back. Here is my xml:

<ViewSwitcher xmlns:android="http://schemas.android.com/apk/res/android"
            android:id="@+id/noteItemVS"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true">

            <TextView
                android:id="@+id/noteItemTextView"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:clickable="true"
                android:text="textview"
                android:textAlignment="center" />

            <EditText
                android:id="@+id/noteItemEditText"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:hint="edittext"
                android:textAlignment="center" />

        </ViewSwitcher>

My code for switching is:

protected void switchViews(NoteItem note) {
        ViewSwitcher switcher = (ViewSwitcher)      context.findViewById(R.id.noteItemVS);
        View switchCheck = switcher.getCurrentView();
        if (switchCheck instanceof EditText) {
            EditText l = (EditText)switchCheck ;
            String x = l.getText().toString();
            Log.e("tree",x);
            if (!x.isEmpty()) {
                switcher.showPrevious();
                TextView myTV = (TextView) switcher.findViewById(R.id.noteItemTextView);
                myTV.setText(x);
            }
        }
        ; if (switchCheck instanceof TextView) {
            TextView l = (TextView) switchCheck;
            String x = l.getText().toString();
            switcher.showNext();
            EditText myEditText = (EditText) switcher.findViewById(R.id.noteItemEditText);
            myEditText.setText(x);
        }
    }
Yagami Light
  • 1,756
  • 4
  • 19
  • 39
Dreamers Org
  • 1,151
  • 3
  • 12
  • 30

2 Answers2

0

By getting rid of the view switcher i was able to solve the problem. Simply by using View.Gone and View.Visibile

protected void switchViews(NoteItem note) {
        if (context.findViewById(R.id.noteItemTextView).getVisibility()==View.GONE) {
            EditText editText = (EditText) context.findViewById(R.id.noteItemEditText);
            String x  = editText.getText().toString();
            editText.setVisibility(View.GONE);
            TextView textView = (TextView) context.findViewById(R.id.noteItemTextView);
            textView.setVisibility(View.VISIBLE);
        } else {
            TextView textView = (TextView) context.findViewById(R.id.noteItemTextView);
            String x  = textView.getText().toString();
            textView.setVisibility(View.GONE);
            EditText editText = (EditText) context.findViewById(R.id.noteItemEditText);
            editText.setText(x);
            editText.setVisibility(View.VISIBLE);
        }
    }
Dreamers Org
  • 1,151
  • 3
  • 12
  • 30
  • you can still use view switcher if you really want, just try to find out where the problem lies. see the answer – Maniya Joe Jul 24 '16 at 17:28
0

This happens because interpreter is going through both the if statements and by default latter one is overriding the value of the forment one. Try below code it will work for sure.

protected void switchViews() {
    ViewSwitcher switcher = (ViewSwitcher)findViewById(R.id.noteItemVS);
    View switchCheck = switcher.getCurrentView();
    if (switchCheck instanceof EditText) {
        EditText l = (EditText)switchCheck;
        String x = l.getText().toString();
        Log.e("tree",x);
        if (!x.isEmpty()) {
            switcher.showPrevious();
            TextView myTV = (TextView) switcher.findViewById(R.id.noteItemTextView);
            myTV.setText(x);
        }
    }else{
        TextView l = (TextView) switchCheck;
        String x = l.getText().toString();
        switcher.showNext();
        EditText myEditText = (EditText) switcher.findViewById(R.id.noteItemEditText);
        myEditText.setText(x);
    }
}
Maniya Joe
  • 761
  • 6
  • 24