2013-07-11 2 views
0

У меня есть радиогруппа radiogoup1, в шляпе которой два радиатора rbtn1, rbtn2. Я хочу хранить Male для rbtn1 и Female для rbtn2 в базе данных. Как это сделать? Я упоминаю .xml и .java файл.Как сохранить текстовое значение для радиогруппы в базе данных sqlite?

sqlliteexample.xml:

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

    <TextView 
     android:id="@+id/textName" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="@string/name" 
     android:paddingLeft="5dp" 
     android:textAppearance="?android:attr/textAppearanceSmall" /> 

    <EditText 
     android:id="@+id/editName" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_weight="1" 
     android:ems="10" 
     android:inputType="textPersonName" > 

     <requestFocus /> 
    </EditText> 
</LinearLayout> 

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

    <TextView 
     android:id="@+id/textAge" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:paddingLeft="5dp" 
     android:text="@string/age" > 
    </TextView> 

    <EditText 
     android:id="@+id/editAge" 
     android:layout_width="80dp" 
     android:layout_height="wrap_content" 
     android:ems="10" 
     android:inputType="number" /> 
</LinearLayout> 

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

    <TextView 
     android:id="@+id/textContcat" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:paddingLeft="5dp" 
     android:text="@string/contact" /> 

    <EditText 
     android:id="@+id/editContact" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_weight="1" 
     android:ems="10" 
     android:inputType="textMultiLine" /> 
</LinearLayout> 

<LinearLayout 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:layout_marginTop="0dp" 
    android:orientation="horizontal" > 

    <TextView 
     android:id="@+id/sLabel" 
     android:layout_width="wrap_content" 
     android:layout_height="match_parent" 
     android:gravity="left|center" 
     android:paddingLeft="5dp" 
     android:paddingRight="10dp" 
     android:text="@string/sx" /> 

    <RadioGroup 
     android:id="@+id/RadioGroup01" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:orientation="horizontal" > 

     <RadioButton 
      android:id="@+id/malebutton" 
      android:layout_width="wrap_content" 
      android:layout_height="17dp" 
      android:text="@string/mal" 
      android:textSize="15sp" /> 

     <RadioButton 
      android:id="@+id/femalebutton" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="@string/femal" 
      android:textSize="15sp" /> 
    </RadioGroup> 
</LinearLayout> 

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

    <Button 
     android:id="@+id/savebutton" 
     style="?android:attr/buttonStyleSmall" 
     android:layout_width="wrap_content" 
     android:layout_height="32dp" 
     android:gravity="left|center" 
     android:text="@string/save" 
     android:textSize="13sp" /> 

    <Button 
     android:id="@+id/viewbutton" 
     style="?android:attr/buttonStyleSmall" 
     android:layout_width="wrap_content" 
     android:layout_height="32dp" 
     android:layout_gravity="center" 
     android:gravity="center|left" 
     android:text="@string/view" 
     android:textSize="13sp" /> 
</LinearLayout> 

SqlLiteExample.java:

public class SqlLiteExample extends Activity implements OnClickListener, OnCheckedChangeListener { 
Button sqlUpdate, sqlView; 
EditText etName,etAge,etContact; 
RadioGroup rdgrp; 
RadioButton selectedRadioButton; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.sqlliteexample); 
    sqlUpdate = (Button) findViewById(R.id.savebutton); 
    etName = (EditText) findViewById(R.id.editName); 
    etAge = (EditText) findViewById(R.id.editAge); 
    etContact = (EditText) findViewById(R.id.editContact); 
    rdgrp.setOnCheckedChangeListener(this); 

    sqlView = (Button) findViewById(R.id.viewbutton); 
    sqlView.setOnClickListener(this); 
    sqlUpdate.setOnClickListener(this); 

} 

@Override 
public void onClick(View arg0) { 
    // TODO Auto-generated method stub 
    switch(arg0.getId()){ 
    case R.id.savebutton: 
     boolean didWork = true; 
     try{ 
     String name = etName.getText().toString(); 
     String age = etAge.getText().toString(); 
     String contact = etContact.getText().toString(); 

     MyDB entry = new MyDB(SqlLiteExample.this); 
     entry.open(); 
     entry.createEntry(name,age,contact); 
     entry.close(); 
     }catch(Exception e){ 
      didWork = false; 
      String error = e.toString(); 
      Dialog d = new Dialog(this); 
      d.setTitle("Error"); 
      TextView tv = new TextView(this); 
      tv.setText(error); 
      d.setContentView(tv); 
      d.show(); 
     }finally{ 
      if(didWork){ 
       Dialog d = new Dialog(this); 
       d.setTitle("Updated"); 
       TextView tv = new TextView(this); 
       tv.setText("Success"); 
       d.setContentView(tv); 
       d.show(); 
      } 

     } 
     break; 
    case R.id.viewbutton: 
     Intent i = new Intent("com.bysakiralam.mydatabase.DISPLAYRECORDS"); 
     startActivity(i); 
     break; 
    } 
} 

@Override 
public void onCheckedChanged(RadioGroup arg0, int arg1) { 
    // TODO Auto-generated method stub 
    switch(arg1){ 
    case R.id.malebutton: 
     break; 
    case R.id.femalebutton: 
     break; 
    } 
} } 

ответ

1

Там нет необходимости переопределить onCheckedChanged() использовать следующее в вашем на SaveButton мыши

rdgrp=(RadioGroup)findViewById(R.id.RadioGroup01); 
String radiovalue= (RadioButton)this.findViewById(rdgrp.getCheckedRadioButtonId())).getText().toString(); 

и теперь использовать radiovalue для хранения в базе данных

Edit: забыть (

rdgrp=(RadioGroup)findViewById(R.id.RadioGroup01); 
String radiovalue= ((RadioButton)this.findViewById(rdgrp.getCheckedRadioButtonId())).getText().toString(); 
+0

он показывает ошибку. не могли бы вы еще раз проверить .. –

+0

@SmitaS Я обновил ваш код, пожалуйста, попробуйте новый обновленный ответ! –

+0

где это ....? @ Tarsem –

0

попробовать этот

myRadioGrp=(RadioGroup)findViewById(R.id.RadioGroup01); 
String radiovalue= ((RadioButton)this.findViewById(myRadioGrp.getCheckedRadioButtonId())).getText().toString(); 
Смежные вопросы