2017-06-19 93 views
-1

如何獲取Radio組中選定的多個單選按鈕值的數據並存儲到數據庫中..我有列表查看哪個正在生成多個無線電組。如何從Android中的Radio組中選擇多個單選按鈕值並在數據庫中存儲數據

1.I有自定義的ListView和it..which一個自定義佈局有一個單一的廣電集團與多個單選按鈕。

2.因此,當應用程序運行時,它會根據從數據庫中提取的數據生成多個無線電組列表。

3.因此,如何從這個多個無線電組中獲取數據。如果它的單個無線電集團可以通過使用

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

int selectedId = radioGroup.getCheckedRadioButtonId();

SWTICH(selectedId){ .................. ............ }

我已附加在UI..below Here is the screenshot of the UI

這裏是代碼: 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> 

回答

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. 

這是您的選擇,你想如何存儲。

+0

謝謝...但是如果我有一個單一的無線電組,您提供的就可以了。 – egom

+0

根據你的屏幕拍攝你有3個電臺組,因爲你可以使用findViewById初始化3個radiogroup並獲得每個radio組的selectedId。 radioGroup =(RadioGroup)findViewById(R.id.radio); – yash786

+0

好吧,會嘗試讓它工作。 – egom

相關問題