Víctor Martín
Víctor Martín

Reputation: 3450

Why I can't get the value of an added radiobuttons or edittexts?

I've done an app how add dynamically to a linearlayout some layout customized. My problem is when I want to retrieve the value of some component of this layout added.

I have three types of XML that I can add dynamically.

ONE

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="5dp"
    android:layout_margin="@dimen/tv_margin"
    android:padding="@dimen/padding_container"
    android:gravity="center">

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="$$$"
        android:id="@+id/tv_question"
        android:textColor="@color/ferro_palette_black"/>

    <RadioGroup xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/rg_answers"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:layout_marginTop="@dimen/ll_main_padding_top">

        <RadioButton
            android:id="@+id/rb_value_1"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:button="@null"
            android:drawableBottom="?android:attr/listChoiceIndicatorSingle"
            android:gravity="center_horizontal|bottom"
            android:text="1"
            android:textColor="@color/ferro_palette_black" />

        <RadioButton
            android:id="@+id/rb_value_2"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:button="@null"
            android:drawableBottom="?android:attr/listChoiceIndicatorSingle"
            android:gravity="center_horizontal|bottom"
            android:text="2"
            android:textColor="@color/ferro_palette_black"/>

        <RadioButton
            android:id="@+id/rb_value_3"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:button="@null"
            android:drawableBottom="?android:attr/listChoiceIndicatorSingle"
            android:gravity="center_horizontal|bottom"
            android:text="3"
            android:textColor="@color/ferro_palette_black"/>

        <RadioButton
            android:id="@+id/rb_value_4"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:button="@null"
            android:drawableBottom="?android:attr/listChoiceIndicatorSingle"
            android:gravity="center_horizontal|bottom"
            android:text="4"
            android:textColor="@color/ferro_palette_black"/>

        <RadioButton
            android:id="@+id/rb_value_5"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:button="@null"
            android:drawableBottom="?android:attr/listChoiceIndicatorSingle"
            android:gravity="center_horizontal|bottom"
            android:text="5"
            android:textColor="@color/ferro_palette_black"/>

    </RadioGroup>
</LinearLayout>

TWO

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="5dp"
    android:layout_margin="@dimen/tv_margin"
    android:padding="@dimen/padding_container"
    android:gravity="center">

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="$$$"
        android:id="@+id/tv_question"
        android:textColor="@color/ferro_palette_black"/>

    <RadioGroup xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/rg_answers"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:layout_marginTop="@dimen/ll_main_padding_top">

        <RadioButton
            android:id="@+id/rb_value_yes"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:button="@null"
            android:drawableBottom="?android:attr/listChoiceIndicatorSingle"
            android:gravity="center_horizontal|bottom"
            android:text="@string/yes"
            android:textColor="@color/ferro_palette_black"/>

        <RadioButton
            android:id="@+id/rb_value_no"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:button="@null"
            android:drawableBottom="?android:attr/listChoiceIndicatorSingle"
            android:gravity="center_horizontal|bottom"
            android:text="@string/no"
            android:textColor="@color/ferro_palette_black"/>


    </RadioGroup>
</LinearLayout>

THREE

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="5dp"
    android:layout_margin="@dimen/tv_margin"
    android:padding="@dimen/padding_container"
    android:gravity="center">

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="$$$"
        android:id="@+id/tv_question"
        android:textColor="@color/ferro_palette_black"/>

    <EditText
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:inputType="textMultiLine"
        android:ems="10"
        android:id="@+id/et_answer"
        android:textColor="@color/ferro_palette_black"/>

</LinearLayout>

I've done the next method to inflate and get values.

public void addQuestion(LinearLayout mRootLayout, Context _c, int quiz_type, String question, int order, int answer_id)
    {
        LayoutInflater inflater = (LayoutInflater) _c.getSystemService( Context.LAYOUT_INFLATER_SERVICE );
        int id_layout = -1;
        if(quiz_type == 1) {
            id_layout = R.layout.quiz_type_one;
        }else if(quiz_type == 2){
            id_layout = R.layout.quiz_type_two;
        }else if(quiz_type == 3){
            id_layout = R.layout.quiz_type_three;
        }

        View mChildView = inflater.inflate(id_layout, mRootLayout, false);
        ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        mRootLayout.addView(mChildView, params);

        TextView tv_question = (TextView) mChildView.findViewById(R.id.tv_question);
        tv_question.setText(question);

        if(quiz_type == 1) {
            RadioGroup rg_answers = (RadioGroup)mChildView.findViewById(R.id.rg_answers);
            int value = rg_answers.getCheckedRadioButtonId();
        }else if(quiz_type == 2){
            RadioGroup rg_answers = (RadioGroup)mChildView.findViewById(R.id.rg_answers);
            int value = rg_answers.getCheckedRadioButtonId();
        }else if(quiz_type == 3){
            EditText et_answer = (EditText)mChildView.findViewById(R.id.et_answer);
            String value = et_answer.getText().toString();
        }

    }

For some reason, everytime I'm getting -1 or "" in the variable value. Why??

Thanks for your help.

Upvotes: 1

Views: 85

Answers (1)

Techfist
Techfist

Reputation: 4344

by seeing your code, in this method addQuestion(), you are inflating you layouts fresh,

so in below part of method where you are trying to catch the values, it will always be -1 and "" as you are trying to read values out of freshly inflated views.

if you want to get values which user has entered, try implementing OnClick listeners, or pehaps a button pressing which you initiate value read.

Upvotes: 1

Related Questions