wie, um text zu speichern den Wert für eine radiogroup in sqlite-Datenbank?

habe ich eine radio-Gruppe radiogoup1, dass hat Sie zwei radiobutton-rbtn1, rbtn2. angenommen, ich möchte store für Männer rbtn1 und Weiblich für rbtn2 in der Datenbank. Wie es zu tun? Ich erwähne die .xml-und .java-Datei.

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;
    }
} }
InformationsquelleAutor Smita S | 2013-07-11
Schreibe einen Kommentar