android - displaying contents of an sqlite database -
i have update page allows inserting, updating , displaying contents of database. xml layout file looks this:
<?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" android:background="@drawable/bckg1" android:gravity="top" android:orientation="vertical" > <textview android:id="@+id/gotomenu" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignparentbottom="true" android:layout_alignparentright="true" android:layout_marginbottom="46dp" android:layout_marginright="18dp" android:text="go main menu" android:textcolor="#221778" android:textsize="15sp" /> <edittext android:id="@+id/entryforroll" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignbottom="@+id/rollno" android:layout_alignparentright="true" android:layout_torightof="@+id/attendancepercent" android:ems="10" android:inputtype="textpersonname" android:hint="enter roll number" > <requestfocus /> </edittext> <textview android:id="@+id/rollno" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignparentleft="true" android:layout_alignparenttop="true" android:layout_margintop="58dp" android:text="roll number:" android:textappearance="?android:attr/textappearancelarge" android:textcolor="#221778" android:textstyle="bold" /> <button android:id="@+id/show" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_above="@+id/gotomenu" android:layout_toleftof="@+id/gotomenu" android:text="show" android:textcolor="#221778" android:textstyle="bold" android:onclick="getall" /> <button android:id="@+id/insert" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_above="@+id/show" android:layout_alignleft="@+id/name" android:layout_marginbottom="36dp" android:text="insert" android:textcolor="#221778" android:textstyle="bold" /> <button android:id="@+id/update" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignbaseline="@+id/insert" android:layout_alignbottom="@+id/insert" android:layout_alignright="@+id/gotomenu" android:text="update" android:textcolor="#221778" android:textstyle="bold" /> <textview android:id="@+id/attendancepercent" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_above="@+id/insert" android:layout_alignparentleft="true" android:layout_marginbottom="23dp" android:text="attendance %:" android:textappearance="?android:attr/textappearancelarge" android:textcolor="#221778" android:textstyle="bold" /> <textview android:id="@+id/name" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignparentleft="true" android:layout_below="@+id/rollno" android:layout_margintop="24dp" android:text="name:" android:textappearance="?android:attr/textappearancelarge" android:textcolor="#221778" android:textsize="22sp" android:textstyle="bold" /> <edittext android:id="@+id/entryforname" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignbottom="@+id/name" android:layout_alignleft="@+id/show" android:ems="10" android:inputtype="textpersonname" android:hint="enter name" /> <edittext android:id="@+id/entryforpercentage" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignbottom="@+id/attendancepercent" android:layout_torightof="@+id/show" android:ems="10" android:hint="enter %" />
now 3 buttons perform actions of insert, update , display.the java code given below :
package com.vidhi.grietportal; import android.app.activity; import android.content.intent; import android.database.cursor; import android.database.sqlite.sqlitedatabase; import android.graphics.color; import android.graphics.typeface; import android.os.bundle; import android.view.view; import android.view.view.onclicklistener; import android.widget.button; import android.widget.edittext; import android.widget.tablelayout; import android.widget.tablerow; import android.widget.textview; public class update extends activity { button b1,b2,b3; edittext e1,e2,e3; attendancedatabaseadapter adb; sqlitedatabase db; databasehelper dbh; textview tv1; @override protected void oncreate(bundle savedinstancestate) { // todo auto-generated method stub super.oncreate(savedinstancestate); setcontentview(r.layout.update); adb= new attendancedatabaseadapter(this); adb=adb.open(); b1=(button)findviewbyid(r.id.insert); b2=(button)findviewbyid(r.id.update); b3=(button)findviewbyid(r.id.show); e1=(edittext)findviewbyid(r.id.entryforroll); e2=(edittext)findviewbyid(r.id.entryforname); e3=(edittext)findviewbyid(r.id.entryforpercentage); final string roll=e1.gettext().tostring(); final string name=e2.gettext().tostring(); final string percent=e3.gettext().tostring(); b1.setonclicklistener(new onclicklistener() { @override public void onclick(view arg0) { // todo auto-generated method stub adb.insertentry(roll,name,percent); } }); b2.setonclicklistener(new onclicklistener() { @override public void onclick(view arg0) { // todo auto-generated method stub adb.updateentry(roll,name,percent); } }); b3.setonclicklistener(new onclicklistener() { @override public void onclick(view v) { // todo auto-generated method stub intent ine=new intent(v.getcontext(),displayatt.class); startactivityforresult(ine, 0); } }); } }
the adapter class follows:
package com.vidhi.grietportal; import android.content.contentvalues; import android.content.context; import android.database.cursor; import android.database.sqlexception; import android.database.sqlite.sqlitedatabase; public class attendancedatabaseadapter { public final string database_name="griet.db"; static final int database_version=2; static final int name_column=1; public sqlitedatabase db; private final context context; private databasehelper dbhelper; public attendancedatabaseadapter(context _context) { context=_context; dbhelper=new databasehelper(context, database_name, null, database_version); } public attendancedatabaseadapter open() throws sqlexception { db=dbhelper.getwritabledatabase(); return this; } public void close() { db.close(); } public sqlitedatabase getdatabaseinstance() { return db; } public void insertentry(string roll,string name,string percent) { contentvalues newcontent=new contentvalues(); newcontent.put("rollno",roll); newcontent.put("stuname", name); newcontent.put("percentage",percent); db.insert("attendance",null, newcontent); } public void updateentry(string roll,string name,string percent) { contentvalues newupdate=new contentvalues(); newupdate.put("rollno", roll); newupdate.put("stuname",name); newupdate.put("percentage", percent); string where="rollno=?"; db.update("attendance", newupdate, where, new string[] {roll}); } public cursor getall() { return db.query("attendance", new string[] {"rollno","stuname", "percentage"}, null, null, null, null, null); } }
the databasehelper class follows:
package com.vidhi.grietportal; import android.content.context; import android.database.sqlite.sqlitedatabase; import android.database.sqlite.sqliteopenhelper; import android.database.sqlite.sqlitedatabase.cursorfactory; public class databasehelper extends sqliteopenhelper { private static final string table1 = "login"; private static final string table2 = "attendance"; private static final string column_id = "id"; private static final string column_title1 = "username"; private static final string column_title2 = "password"; private static final string column_id1 = "id1"; private static final string column_title3 = "rollno"; private static final string column_title4 = "stuname"; private static final string column_title5 = "percentage"; public static final string table1_creation = "create table " + table1 + " (" + column_id + " integer primary key autoincrement, " + column_title1 + " text,"+ column_title2 +" text"+")"; public static final string table2_creation = "create table " + table2 + " (" + column_id1 + " integer primary key autoincrement, " + column_title3 + " text,"+ column_title4 +" text,"+ column_title5 +" text"+")"; public databasehelper(context context, string name, cursorfactory factory, int version) { super(context, name, factory, version); } @override public void oncreate(sqlitedatabase db) { db.execsql(table1_creation); db.execsql(table2_creation); } @override public void onupgrade(sqlitedatabase db, int paramint1, int paramint2) { db.execsql("drop table " + table1_creation + ";"); db.execsql("drop table " + table2_creation + ";"); oncreate(db); } }
the displayatt class given follows:
package com.vidhi.grietportal; import android.app.activity; import android.database.cursor; import android.os.bundle; import android.widget.textview; public class displayatt extends activity { attendancedatabaseadapter adb; databasehelper dbh1; textview tv1=(textview)findviewbyid(r.id.textview1999); @override protected void oncreate(bundle savedinstancestate) { // todo auto-generated method stub super.oncreate(savedinstancestate); setcontentview(r.layout.displayatt); adb.open(); //db.fetchallnotes(); cursor c = adb.getall(); int i=1; if (c.movetofirst()) { { displaycontact(c,i++); } while (c.movetonext()); } c.close(); adb.close(); } public void displaycontact(cursor c,int row ) { string name11 = c.getstring(1) + c.getstring(2) + c.getstring(3); tv1.settext(name11 ); } }
we wish display contents of attendance table using displayatt class. code giving me null pointer exception. please !
i don´t know if all, haven´t initialized attendancedatabaseadapter. must in oncreate()
adb = new attendancedatabaseadapter(this);
before call
adb.open();
Comments
Post a Comment