Android Application:Print out value from SQLite in textView -
after running application not print value db. prints else looks location address of value?
adding value db , trying print out in textview1
package com.androidhive.androidsqlite; import java.util.list; import android.app.activity; import android.os.bundle; import android.util.log; import android.widget.textview; public class androidsqlitetutorialactivity extends activity { /** called when activity first created. */ textview text; @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.main); databasehandler db = new databasehandler(this); /** * crud operations * */ // inserting contacts log.d("insert: ", "inserting .."); db.addcontact(new contact("ravi", "9100000000")); db.addcontact(new contact("srinivas", "9199999999")); db.addcontact(new contact("tommy", "9522222222")); db.addcontact(new contact("karthik", "9533333333")); // reading contacts log.d("reading: ", "reading contacts.."); list<contact> contacts = db.getallcontacts(); (contact cn : contacts) { string log = "id: "+cn.getid()+" ,name: " + cn.getname() + " ,phone: " + cn.getphonenumber(); // writing contacts log log.d("name: ", log); } contact rando = db.getcontact(1); text = (textview) findviewbyid(r.id.textview1); text.settext(string.valueof(rando)); } }
contact class
package com.androidhive.androidsqlite; public class contact { //private variables int _id; string _name; string _phone_number; // empty constructor public contact(){ } // constructor public contact(int id, string name, string _phone_number){ this._id = id; this._name = name; this._phone_number = _phone_number; } // constructor public contact(string name, string _phone_number){ this._name = name; this._phone_number = _phone_number; } // getting id public int getid(){ return this._id; } // setting id public void setid(int id){ this._id = id; } // getting name public string getname(){ return this._name; } // setting name public void setname(string name){ this._name = name; } // getting phone number public string getphonenumber(){ return this._phone_number; } // setting phone number public void setphonenumber(string phone_number){ this._phone_number = phone_number; } }
handler
package com.androidhive.androidsqlite; import java.util.arraylist; import java.util.list; import android.content.contentvalues; import android.content.context; import android.database.cursor; import android.database.sqlite.sqlitedatabase; import android.database.sqlite.sqliteopenhelper; public class databasehandler extends sqliteopenhelper { // static variables // database version private static final int database_version = 1; // database name private static final string database_name = "contactsmanager"; // contacts table name private static final string table_contacts = "contacts"; // contacts table columns names private static final string key_id = "id"; private static final string key_name = "name"; private static final string key_ph_no = "phone_number"; public databasehandler(context context) { super(context, database_name, null, database_version); } // creating tables @override public void oncreate(sqlitedatabase db) { string create_contacts_table = "create table " + table_contacts + "(" + key_id + " integer primary key," + key_name + " text," + key_ph_no + " text" + ")"; db.execsql(create_contacts_table); } // upgrading database @override public void onupgrade(sqlitedatabase db, int oldversion, int newversion) { // drop older table if existed db.execsql("drop table if exists " + table_contacts); // create tables again oncreate(db); } /** * crud(create, read, update, delete) operations */ // adding new contact void addcontact(contact contact) { sqlitedatabase db = this.getwritabledatabase(); contentvalues values = new contentvalues(); values.put(key_name, contact.getname()); // contact name values.put(key_ph_no, contact.getphonenumber()); // contact phone // inserting row db.insert(table_contacts, null, values); db.close(); // closing database connection } // getting single contact contact getcontact(int id) { sqlitedatabase db = this.getreadabledatabase(); cursor cursor = db.query(table_contacts, new string[] { key_id, key_name, key_ph_no }, key_id + "=?", new string[] { string.valueof(id) }, null, null, null, null); if (cursor != null) cursor.movetofirst(); contact contact = new contact(integer.parseint(cursor.getstring(0)), cursor.getstring(1), cursor.getstring(2)); // return contact return contact; } // getting contacts public list<contact> getallcontacts() { list<contact> contactlist = new arraylist<contact>(); // select query string selectquery = "select * " + table_contacts; 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.setphonenumber(cursor.getstring(2)); // adding contact list contactlist.add(contact); } while (cursor.movetonext()); } // return contact list return contactlist; } // updating single contact public int updatecontact(contact contact) { sqlitedatabase db = this.getwritabledatabase(); contentvalues values = new contentvalues(); values.put(key_name, contact.getname()); values.put(key_ph_no, contact.getphonenumber()); // updating row return db.update(table_contacts, values, key_id + " = ?", new string[] { string.valueof(contact.getid()) }); } // deleting single contact public void deletecontact(contact contact) { sqlitedatabase db = this.getwritabledatabase(); db.delete(table_contacts, key_id + " = ?", new string[] { string.valueof(contact.getid()) }); db.close(); } // getting contacts count public int getcontactscount() { string countquery = "select * " + table_contacts; sqlitedatabase db = this.getreadabledatabase(); cursor cursor = db.rawquery(countquery, null); cursor.close(); // return count return cursor.getcount(); } }
what co-incidence. else had exact same problem hour ago!
the listview calling tostring()
represent instance of contact
class. have not overridden tostring()
method hence default object.tostring()
method called.
@override public string tostring(){ return this._name; // whatever need represent instance of contact }
also, instead of:
text.settext(string.valueof(rando));
use
text.settext(rando.tostring());
Comments
Post a Comment