Button stopped generating View

33 views Asked by At

I'm on a working on a project on Android Studio. Then I would like to implement a button that generate a view above him. And store it's text data in an ArrayList.

This is the java code:

addClassBtn.setOnClickListener((v)->{
        View cricketerView = getLayoutInflater().inflate(R.layout.check_box,categoryList,false);
        EditText checkBox_title = (EditText)cricketerView.findViewById(R.id.checkBox_title);
        switch (appPreference.getInt("lang",0)){
            case 0:checkBox_title.setHint(getResources().getString(R.string.en_en_editBox_Text));break;
            case 1:checkBox_title.setHint(getResources().getString(R.string.ch_tw_editBox_Text));break;
            default:checkBox_title.setHint("Error");
        }
        temporaryClass.add(checkBox_title.getText().toString());
        checkBox_title.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
                temporaryClass.remove(checkBox_title.getText().toString());
            }

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

            }

            @Override
            public void afterTextChanged(Editable s) {
                temporaryClass.add(checkBox_title.getText().toString());
            }
        });
        CheckBox checkBox_check = (CheckBox)cricketerView.findViewById(R.id.checkBox_check);
        checkBox_check.setOnClickListener((w)->{
        });
        Button checkBox_delete = (Button)cricketerView.findViewById(R.id.checkBox_delete);
        checkBox_delete.setOnClickListener((w)->{
            categoryList.removeView(w);
        });
        categoryList.addView(cricketerView);
    });

And this is the XML Layout code of the view that I would like to add above the button:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">
<EditText
    android:id="@+id/checkBox_title"
    android:layout_width="295dp"
    android:layout_height="40dp"
    android:layout_marginStart="13dp"
    android:layout_marginTop="7dp"
    android:layout_marginEnd="5dp"
    android:autofillHints="@string/ch_tw_editBox_Text"
    android:background="@drawable/rounded_edittext"
    android:hint="@string/ch_tw_editBox_Text"
    android:inputType="text"
    android:paddingStart="13dp"
    android:paddingEnd="13dp"
    android:selectAllOnFocus="false"
    android:textSize="22sp" />
<CheckBox
    android:id="@+id/checkBox_check"
    android:layout_width="30dp"
    android:layout_height="40dp"
    android:layout_marginStart="5dp"
    android:layout_marginTop="5dp"
    android:layout_marginEnd="5dp"
    android:text=""
    android:textColorLink="#000000" />
<Button
    android:id="@+id/checkBox_delete"
    android:layout_width="40dp"
    android:layout_height="40dp"
    android:background="@drawable/ic_delete"
    android:layout_marginStart="5dp"
    android:layout_marginEnd="13dp"
    android:layout_marginTop="5dp"/>
</LinearLayout>

Then when I try it, it only generate the view one time and I can't generate more view. I can't even delete the view that was generated.

TestCase:

enter image description here

I already used the code above on an other activity, and I work really good.

Thank you

0

There are 0 answers