-1

how to get the data of selected multiple radio button values in a radio group and store in database..I have list View which is generating multiple Radio Groups .

1.I have a Custom ListView and a custom layout for it..which has a single Radio Group with multiple Radio Buttons.

2.So, when the app run it generates List of multiple Radio Groups according to data it is fetching from the database.

3.So, how to get data from this multiple radio Group. if its a single Radio Group it ok.. to get it by using

radioGroup = (RadioGroup) findViewById(R.id.radio);

int selectedId = radioGroup.getCheckedRadioButtonId();

Swtich(selectedId){ .................. ............ }

I have attached the UI..below Here is the screenshot of the UI

Here is the Code: atten.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">



    <ListView
        android:id="@+id/listView"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />



    <android.support.design.widget.BottomNavigationView
        android:id="@+id/bottom_navigation"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        app:itemBackground="@color/colorPrimary"
        app:itemIconTint="@color/white"
        app:itemTextColor="@color/white"
        app:menu="@menu/bott_menu" />
</RelativeLayout>

-----listCustomView.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@color/divider_color"
    android:paddingBottom="0dp">



        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginBottom="0dp"
            android:background="@color/cpb_blue">


            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical"
                android:padding="10dp"
                tools:background="@color/cpb_red">

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:orientation="horizontal">

                    <ImageView
                        android:id="@+id/imageView"
                        android:layout_width="92dp"
                        android:layout_height="68dp"
                        android:src="@drawable/userprofile" />

                    <TextView
                        android:id="@+id/textViewrollno"
                        android:layout_width="0dp"
                        android:layout_height="wrap_content"
                        android:layout_weight="1"
                        android:padding="10dp"
                        android:text="07"
                        android:textColor="@color/color_1" />

                    <TextView
                        android:id="@+id/textViewname"
                        android:layout_width="0dp"
                        android:layout_height="wrap_content"
                        android:layout_gravity="right"
                        android:layout_weight="1"
                        android:gravity="right"
                        android:padding="10dp"
                        android:text=""
                        android:textSize="10dp" />


                </LinearLayout>

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:orientation="horizontal">

                    <RadioGroup
                        android:layout_width="match_parent"
                        android:layout_height="50dp"
                        android:orientation="horizontal">

                        <CheckedTextView
                            android:id="@+id/checkedTextView"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_weight="1"
                            android:text="CheckedTextView" />

                        <RadioButton
                            android:id="@+id/leave"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_alignParentLeft="true"
                            android:layout_alignParentStart="true"
                            android:layout_alignParentTop="true"
                            android:text="Leave" />


                        <RadioButton
                            android:id="@+id/late"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_alignParentTop="true"
                            android:text="Late" />

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

                        <RadioButtonl
                            android:id="@+id/present"
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:text="present" />

                    </RadioGroup>
                </LinearLayout>


            </LinearLayout>

        </RelativeLayout>


</RelativeLayout>
egom
  • 21
  • 5

1 Answers1

0
radioGroup = (RadioGroup) findViewById(R.id.radio);

int selectedId = radioGroup.getCheckedRadioButtonId();

// find the radiobutton by returned id
radioButton = (RadioButton) findViewById(selectedId);

Toast.makeText(MyAndroidAppActivity.this,
    radioButton.getText(), Toast.LENGTH_SHORT).show();

radioButton.getText() --- Store this value in database as a String.

It's your choice how you want to store.

moondaisy
  • 4,303
  • 6
  • 41
  • 70
yash786
  • 1,151
  • 8
  • 18
  • thanks...but what you have provide is ok if i have a single Radio Group. – egom Jun 19 '17 at 19:00
  • as per your screen shoot you are having 3 radio group as you can initialize 3 radiogroup using findViewById and get selectedId of each radio group. radioGroup = (RadioGroup) findViewById(R.id.radio); – yash786 Jun 19 '17 at 19:36