sqlite database practice in android -
i have made application simple in android database practice,as have no idea sqlite database
i've gone through many links it,but of them complex,i have created 4 activities 1st (mainactivity) contains 3 buttons
"add","edit", , "view" in 2nd activity (addactivity) have made 3 edittexts
entered values should stored in database.so can please tell me easy steps doing same?
mainactivity.java
package com.example.db; import android.os.bundle; import android.app.activity; import android.content.intent; import android.view.menu; import android.view.view; import android.view.view.onclicklistener; import android.widget.button; public class mainactivity extends activity { @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); button add=(button)findviewbyid(r.id.button1); button edit=(button)findviewbyid(r.id.button2); button view=(button)findviewbyid(r.id.button3); add.setonclicklistener(new onclicklistener() { @override public void onclick(view arg0) { // todo auto-generated method stub] intent i=new intent(mainactivity.this,addactivity.class); startactivity(i); } }); edit.setonclicklistener(new onclicklistener() { @override public void onclick(view v) { // todo auto-generated method stub intent i=new intent(mainactivity.this,editactivity.class); startactivity(i); } }); view.setonclicklistener(new onclicklistener() { @override public void onclick(view v) { // todo auto-generated method stub intent i=new intent(mainactivity.this,viewactivity.class); startactivity(i); } }); } }
addactivity.java
package com.example.db; import android.os.bundle; import android.app.activity; import android.content.intent; import android.database.cursor; import android.database.sqlite.sqlitedatabase; import android.view.menu; import android.view.view; import android.view.view.onclicklistener; import android.widget.button; import android.widget.edittext; public class addactivity extends activity { edittext name,addres,phon; button ad,cn; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_add); name = (edittext)findviewbyid(r.id.name); addres=(edittext)findviewbyid(r.id.address); phon = (edittext)findviewbyid(r.id.phone); ad =(button)findviewbyid(r.id.add); cn=(button)findviewbyid(r.id.cancel); final sqlitedatabase db = openorcreatedatabase("mydb",mode_private, null); db.execsql("create table if not exists simple(name varchar,address varchar,phone varchar"); ad.setonclicklistener(new onclicklistener() { string n=name.gettext().tostring(); string a=addres.gettext().tostring(); string p= phon.gettext().tostring(); @override public void onclick(view v) { db.execsql("insert simple values('n','a','p')"); cursor c =db.rawquery("select * simple",null); c.movetofirst(); } }); cn.setonclicklistener(new onclicklistener() { @override public void onclick(view v) { // todo auto-generated method stub intent =new intent(addactivity.this,mainactivity.class); startactivity(i); } }); } }
main.xml
<relativelayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".mainactivity" > <button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignparentleft="true" android:layout_alignparenttop="true" android:layout_marginleft="100dp" android:layout_margintop="92dp" android:text="add" /> <button android:id="@+id/button2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignright="@+id/button1" android:layout_below="@+id/button1" android:layout_margintop="28dp" android:text="edit" /> <button android:id="@+id/button3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignright="@+id/button2" android:layout_below="@+id/button2" android:layout_margintop="37dp" android:text="view" /> </relativelayout>
add.xml
<relativelayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".addactivity" > <textview android:id="@+id/textview3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignparentleft="true" android:layout_below="@+id/textview2" android:layout_margintop="60dp" android:text="phone" android:textappearance="?android:attr/textappearancemedium" /> <textview android:id="@+id/textview1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_above="@+id/textview3" android:layout_alignparentleft="true" android:layout_alignparenttop="true" android:layout_marginleft="17dp" android:layout_margintop="14dp" android:text="name" android:textappearance="?android:attr/textappearancemedium" /> <edittext android:id="@+id/name" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignbaseline="@+id/textview1" android:layout_alignbottom="@+id/textview1" android:layout_alignparentright="true" android:ems="10" > <requestfocus /> </edittext> <textview android:id="@+id/textview2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignparenttop="true" android:layout_margintop="80dp" android:layout_toleftof="@+id/edittext2" android:text="address" android:textappearance="?android:attr/textappearancemedium" /> <edittext android:id="@+id/address" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignparentright="true" android:layout_aligntop="@+id/textview2" android:ems="10" android:inputtype="textpostaladdress" /> <edittext android:id="@+id/phone" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignbottom="@+id/textview3" android:layout_alignparentright="true" android:ems="10" android:inputtype="phone" /> <button android:id="@+id/add" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/phone" android:layout_margintop="62dp" android:layout_torightof="@+id/textview1" android:text="add" /> <button android:id="@+id/cancel" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignbottom="@+id/add" android:layout_marginleft="36dp" android:layout_torightof="@+id/add" android:text="cancel" /> </relativelayout>
ok think want add value of edit text db
package com.example.databasesample; import java.util.arraylist; import android.app.activity; import android.content.contentvalues; import android.database.cursor; import android.database.sqlite.sqlitedatabase; import android.os.bundle; import android.view.menu; import android.view.view; import android.view.view.onclicklistener; import android.widget.adapterview; import android.widget.adapterview.onitemclicklistener; import android.widget.button; import android.widget.edittext; import android.widget.listview; public class mainactivity extends activity implements onclicklistener { static edittext edtadd; button btnadd, btnshow; listview listname; static databasesqllitehelper mbasesqllitehelper; dbmodel mdbmodel; arraylist<dbmodel> marraylist; listadapter madapter; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); initialize(); marraylist = new arraylist<dbmodel>(); mbasesqllitehelper = new databasesqllitehelper(mainactivity.this); mbasesqllitehelper.getreadabledatabase(); mbasesqllitehelper.getwritabledatabase(); } public void initialize() { edtadd = (edittext) findviewbyid(r.id.edtentername); btnadd = (button) findviewbyid(r.id.btnadd); btnadd.setonclicklistener(this); btnshow = (button) findviewbyid(r.id.btnshow); btnshow.setonclicklistener(this); listname = (listview) findviewbyid(r.id.listname); listname.setonitemclicklistener(new onitemclicklistener() { @override public void onitemclick(adapterview<?> arg0, view arg1, int arg2, long arg3) { // todo auto-generated method stub deleteitem(marraylist.get(arg2).getid()); } }); } @override public boolean oncreateoptionsmenu(menu menu) { // inflate menu; adds items action bar if present. getmenuinflater().inflate(r.menu.main, menu); return true; } @override public void onclick(view v) { // todo auto-generated method stub switch (v.getid()) { case r.id.btnadd: addname(edtadd.gettext().tostring()); break; case r.id.btnshow: shownames(); break; default: break; } } // adding values public void addname(string name) { sqlitedatabase mopenhelper = mbasesqllitehelper.getwritabledatabase(); contentvalues mcontentvalues = new contentvalues(); mcontentvalues.put("name", name); mopenhelper.insert("names", null, mcontentvalues); mopenhelper.close(); } //showing values in list public void shownames() { string selectquery = "select * names"; sqlitedatabase mdatabase = mbasesqllitehelper.getwritabledatabase(); cursor mcursor = mdatabase.rawquery(selectquery, null); if (mcursor.movetofirst()) { { mdbmodel = new dbmodel(); mdbmodel.setid(mcursor.getstring(0)); mdbmodel.setname(mcursor.getstring(1)); marraylist.add(mdbmodel); } while (mcursor.movetonext()); } madapter = new listadapter(mainactivity.this, marraylist); listname.setadapter(madapter); } deleteing values public void deleteitem(string id) { sqlitedatabase mdatabase = mbasesqllitehelper.getwritabledatabase(); string delete = "delete names _id =" + id; mdatabase.execsql(delete); mdatabase.close(); madapter.notifydatasetchanged(); marraylist.remove(id); } //updating item public static void updateitem(string id) { sqlitedatabase mdatabase = mbasesqllitehelper.getwritabledatabase(); string update = "update names set name=\"" + edtadd.gettext().tostring() + "\" _id=" + id; mdatabase.execsql(update); mdatabase.close(); }
}
package com.example.databasesample; public class databaseconstants { public static final string create_table_profile_query = "create table names(" + " _id integer primary key autoincrement," + " name varchar" + ")";
}
package com.example.databasesample; import android.content.context; android.database.sqlite.sqlitedatabase; import android.database.sqlite.sqliteopenhelper; public class databasesqllitehelper extends sqliteopenhelper { private static final string database_name = "my sample database"; private static final int database_version = 1; public databasesqllitehelper(context context) { super(context, database_name, null, database_version); } @override public void oncreate(sqlitedatabase db) { db.execsql(databaseconstants.create_table_profile_query); } @override public void onupgrade(sqlitedatabase db, int oldversion, int newversion) { }
}
package com.example.databasesample; public class dbmodel { string id; string name; public string getid() { return id; } public void setid(string id) { this.id = id; } public string getname() { return name; } public void setname(string name) { this.name = name; }
}
package com.example.databasesample; import java.util.arraylist; import android.content.context; import android.view.layoutinflater; import android.view.view; import android.view.view.onclicklistener; import android.view.viewgroup; import android.widget.baseadapter; import android.widget.button; import android.widget.textview;
public class listadapter extends baseadapter {
context mcontext; arraylist<dbmodel> marraylist; public listadapter(context mcontext, arraylist<dbmodel> models) { // todo auto-generated constructor stub this.marraylist = models; this.mcontext = mcontext; } @override public int getcount() { // todo auto-generated method stub return marraylist.size(); } @override public object getitem(int arg0) { // todo auto-generated method stub return arg0; } @override public long getitemid(int arg0) { // todo auto-generated method stub return arg0; } @override public view getview(final int position, view convertview, viewgroup parent) { // todo auto-generated method stub if (convertview == null) { layoutinflater mlayoutinflater = (layoutinflater) mcontext .getsystemservice(context.layout_inflater_service); convertview = mlayoutinflater.inflate(r.layout.list_layout, parent, false); textview txtid = (textview) convertview.findviewbyid(r.id.txtid); txtid.settext(marraylist.get(position).getid()); textview txtname = (textview) convertview .findviewbyid(r.id.txtname); txtname.settext(marraylist.get(position).getname()); button btnupdate=(button)convertview.findviewbyid(r.id.btnupdate); btnupdate.setonclicklistener(new onclicklistener() { @override public void onclick(view v) { // todo auto-generated method stub mainactivity.updateitem(marraylist.get(position).getid()); } }); } return convertview; }
}
//activity_main.xml <relativelayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingbottom="@dimen/activity_vertical_margin" android:paddingleft="@dimen/activity_horizontal_margin" android:paddingright="@dimen/activity_horizontal_margin" android:paddingtop="@dimen/activity_vertical_margin" tools:context=".mainactivity" > <edittext android:id="@+id/edtentername" android:layout_width="fill_parent" android:layout_height="wrap_content" android:hint="enter name" /> <button android:id="@+id/btnadd" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/edtentername" android:text="add database" /> <button android:id="@+id/btnshow" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/btnadd" android:text="show" /> <listview android:id="@+id/listname" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/btnshow" > </listview>
//listlayout.xml <?xml version="1.0" encoding="utf-8"?> <relativelayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" > <textview android:id="@+id/txtid" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <textview android:id="@+id/txtname" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginleft="20dp" android:layout_torightof="@+id/txtid" /> <button android:id="@+id/btnupdate" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/txtname" android:text="update" />
here make app in can add ,edit , update database.i use 2 main classes first databasesqllitehelper.java create databse , databaseconstants.java create table. delete item db click on list , update first enter value in edit text.comment on if need further help.
Comments
Post a Comment