Tuesday, 15 September 2015

java - android sqlite database fetch the data not working -



java - android sqlite database fetch the data not working -

my requirement create employee management system. have name, mobile num 1 & 2 , home. can add together them database. want show employees names when press "show them" button , when click name go intent , show mobile num 1 & 2 , home.

i have created:- contact.java

public class contact { //private variables int id; string name; string mobile1; string mobile2; string home; string about; // empty constructor public contact(){ } // constructor public contact(int id, string name, string mobile1,string mobile2,string home,string about){ this.id = id; this.name = name; this.mobile1 = mobile1; this.mobile2 = mobile2; this.home = home; this.about = about; } // constructor public contact(string name, string mobile1,string mobile2,string home,string about){ this.name = name; this.mobile1 = mobile1; this.mobile2 = mobile2; this.home = home; this.about = about; } public int getid() { homecoming id; } public string getname() { homecoming name; } public string getmobile1() { homecoming mobile1; } public string getmobile2() { homecoming mobile2; } public string gethome() { homecoming home; } public string getabout() { homecoming about; } public void setid(int id) { this.id = id; }`public void setname(string name) { this.name = name; } public void setmobile1(string mobile1) { this.mobile1 = mobile1; } public void setmobile2(string mobile2) { this.mobile2 = mobile2; } public void sethome(string home) { this.home = home; } public void setabout(string about) { this.about = about; }}`

databasehandler.java:--

public class databasehandler extends sqliteopenhelper { // static variables static final string database = "base2"; static final int version = 1; static final string table = "emp"; static final string c_id = "_id"; static final string c_name= "name"; static final string c_mobile1 = "mobile1"; static final string c_mobile2 = "mobile2"; static final string c_home= "home"; static final string c_about= "about"; public databasehandler(context context) { super(context,database, null, version); //3rd argument passed cursorfactory instance } // creating tables @override public void oncreate(sqlitedatabase db) { // create employee table next fields // _id, ename, designation , salary db.execsql("create table " + table + " ( " + c_id + " integer primary key autoincrement, " + c_name + " text, " + c_mobile1 + " integer, " + c_mobile2 + " integer, " + c_home + " text," + c_about + " text )"); } // upgrading database @override public void onupgrade(sqlitedatabase db, int oldversion, int newversion) { // drop older table if existed db.execsql("drop table if exists " + table); // create tables 1 time again oncreate(db); } // adding new contact void addcontact(contact contact) { sqlitedatabase db = this.getwritabledatabase(); contentvalues values = new contentvalues(); values.put(c_name, contact.getname()); // contact name values.put(c_mobile1, contact.getmobile1()); // contact phone values.put(c_mobile2, contact.getmobile2()); values.put(c_home, contact.gethome()); values.put(c_about, contact.getabout()); // inserting row db.insert(table, null, values); //2nd argument string containing nullcolumnhack db.close(); // closing database connection } // getting contacts public list<contact> getallcontacts() { list<contact> contactlist = new arraylist<contact>(); // select query string selectquery = "select * " + table; sqlitedatabase db = this.getwritabledatabase(); cursor cursor = db.rawquery(selectquery, null); // looping through rows , adding list if (cursor.movetofirst()) { { contact contact = new contact(); // contact.setid(integer.parseint(cursor.getstring(0))); contact.setname(cursor.getstring(1)); contact.setmobile1(cursor.getstring(2)); contact.setmobile2(cursor.getstring(3)); contact.sethome(cursor.getstring(4)); contact.setabout(cursor.getstring(5)); // adding contact list contactlist.add(contact); } while (cursor.movetonext()); } // homecoming contact list homecoming contactlist; } // updating single contact public int updatecontact(contact contact) { sqlitedatabase db = this.getwritabledatabase(); contentvalues values = new contentvalues(); values.put(c_name, contact.getname()); values.put(c_mobile1, contact.getmobile1()); values.put(c_mobile2, contact.getmobile2()); values.put(c_home, contact.gethome()); values.put(c_about, contact.getabout()); // updating row homecoming db.update(table, values, c_id + " = ?", new string[] { string.valueof(contact.getid()) }); } // deleting single contact public void deletecontact(contact contact) { sqlitedatabase db = this.getwritabledatabase(); db.delete(table, c_id + " = ?", new string[] { string.valueof(contact.getid()) }); db.close(); } // getting contacts count public int getcontactscount() { string countquery = "select * " + table; sqlitedatabase db = this.getreadabledatabase(); cursor cursor = db.rawquery(countquery, null); cursor.close(); // homecoming count homecoming cursor.getcount(); }}

here can add together data, problem fetching info employee name. name want show details corresponding name. have created below, it's showing 1 name.

list.java:--

public class list1 extends activity { string selected_id = ""; databasehandler helper; sqlitedatabase db; simplecursoradapter adapter; listview lv; cursor c; arraylist<string> list; string s1,s2,s3,s4,s5,s6; list<contact> contacts = helper.getallcontacts(); @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.llist); helper = new databasehandler(this); lv=(listview) findviewbyid(r.id.listview1); lv.setonitemclicklistener(new onitemclicklistener() { @override public void onitemclick(adapterview<?> adapter, view v, int position, long id) { list = new arraylist<string>(); (contact cn : contacts) { s2=cn.getname(); s3=cn.getmobile1(); s4=cn.getmobile2(); s5=cn.gethome(); s6=cn.getabout(); } bundle bundle = new bundle(); bundle.putstring("name",s2 ); bundle.putstring("mob",s3 ); bundle.putstring("mob1",s4 ); bundle.putstring("home",s5 ); bundle.putstring("about",s6 ); intent intent = new intent(getapplicationcontext(), details.class); intent.putextras(bundle); startactivity(intent); } });fetchdata();} private void fetchdata() {`private void fetchdata() { db = helper.getreadabledatabase(); c = db.query(databasehandler.table, null, null, null, null, null, null); adapter = new simplecursoradapter( this, r.layout.listname, c, new string[] { databasehandler.c_name,databasehandler. c_mobile1,databasehandler. c_mobile2,databasehandler. c_home,databasehandler.c_about}, new int[] { r.id.name },0);lv.setadapter(adapter);}`

and details.java is:--

public class details extends activity{ public bundle getbundle = null; @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); this.setcontentview(r.layout.single_list_item_view); textview txt2 = (textview) findviewbyid(r.id.tx2); textview txt3 = (textview) findviewbyid(r.id.tx3); textview txt4 = (textview) findviewbyid(r.id.tx4); textview txt5 = (textview) findviewbyid(r.id.tx5); textview txt6 = (textview) findviewbyid(r.id.tx6); getbundle = this.getintent().getextras(); string n2 = getbundle.getstring("name"); string n3 = getbundle.getstring("mob"); string n4 = getbundle.getstring("mob1"); string n5 = getbundle.getstring("home"); string n6 = getbundle.getstring("about"); txt2.settext(n2); txt3.settext(n3); txt4.settext(n4); txt5.settext(n5); txt6.settext(n6); }}

where problem? have suggestions?

java android sqlite

No comments:

Post a Comment