How to display database content on android Listview? -
i working on contact book application.i have database and,i want display data in listview. can tell me how achieve this? below have tried far,
public class contacts { private int id; private string name; private string phone_number; private string email; public contacts() { } public contacts(int id, string name, string phone_number, string email) { this.id = id; this.name = name; this.phone_number = phone_number; this.email = email; } public contacts(string name, string phone_number, string email) { this.name = name; this.phone_number = phone_number; this.email = email; } public int getid() { return id; } public void setid(int id) { this.id = id; } public string getname() { return name; } public void setname(string name) { this.name = name; } public string getphone_number() { return phone_number; } public void setphone_number(string phone_number) { this.phone_number = phone_number; } public void setemail(string email) { this.email = email; } public string getemail() { return email; } } public class databasehandler extends sqliteopenhelper { private static final int database_version = 2; private static final string database_name = "contactsmanager"; private static final string table_contacts = "contacts"; private static final string key_id = "id"; private static final string key_name = "name"; private static final string key_ph_no = "phone_number"; private static final string key_email = "email"; public databasehandler(context context) { super( context, database_name, null, database_version); // todo auto-generated constructor stub } @override public void oncreate(sqlitedatabase db) { // todo auto-generated method stub string create_contacts_table = "create table " + table_contacts + "(" + key_id + " integer primary key," + key_name + " text," + key_ph_no + " text," + key_email + " text" + ")"; db.execsql(create_contacts_table); } @override public void onupgrade(sqlitedatabase db, int oldversion, int newversion) { // todo auto-generated method stub db.execsql("drop table if exists " + table_contacts); oncreate(db); } public void insertcontact(contacts contacts) { sqlitedatabase db = this.getwritabledatabase(); contentvalues cv = new contentvalues(); cv.put(key_name, contacts.getname()); // contact name cv.put(key_ph_no, contacts.getphone_number()); // contact phone cv.put(key_email, contacts.getemail()); //contact email // inserting row db.insert(table_contacts, null, cv); db.close(); } // getting contacts public list<contacts> getallcontacts() { list<contacts> contactlist = new arraylist<contacts>(); // 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()) { { contacts contact = new contacts(); //contact.setid(integer.parseint(cursor.getstring(0))); contact.setname(cursor.getstring(1)); contact.setphone_number(cursor.getstring(2)); contact.setemail(cursor.getstring(3)); // adding contact list contactlist.add(contact); } while (cursor.movetonext()); } // return contact list return contactlist; } } public class view_data extends listactivity { databasehandler db = new databasehandler(this); cursor c ; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_view_data); db.getwritabledatabase(); list<contacts> values = db.getallcontacts(); arrayadapter<contacts> adapter = new arrayadapter<contacts>(this, android.r.layout.simple_list_item_1, values); setlistadapter(adapter); } @override public boolean oncreateoptionsmenu(menu menu) { // inflate menu; adds items action bar if present. getmenuinflater().inflate(r.menu.view_data, menu); return true; } }
in view_data
class tried display data couldn't it. suggestion highly appreciated.
try this:
you can use
http://dj-android.blogspot.in/2012/10/android-show-data-from-sqlite-db-into.html
and solution.. in have use listview instead of gridview.
Comments
Post a Comment