i've radio group radiogoup1 hat 2 radiobutton rbtn1, rbtn2. want store male rbtn1 , female rbtn2 in database. how it? mentioning .xml , .java file.
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 = 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; } } }
there no need override oncheckedchanged() use following in on savebutton click
rdgrp=(radiogroup)findviewbyid(r.id.radiogroup01); string radiovalue= (radiobutton)this.findviewbyid(rdgrp.getcheckedradiobuttonid())).gettext().tostring();
and use radiovalue store in database
edit: forget (
rdgrp=(radiogroup)findviewbyid(r.id.radiogroup01); string radiovalue= ((radiobutton)this.findviewbyid(rdgrp.getcheckedradiobuttonid())).gettext().tostring();
Comments
Post a Comment