records added db calling dbadapter insert method on activity no runtime errors, data seems added db properly, listview object not appear when app being run
main activity
public class mainscreen extends actionbaractivity implements view.onclicklistener { button addnewbutton; button sortbynamebutton; button sortbybusinessbutton; button sortbyphonebutton; dbadapter userdb; listview cardlist; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(layout.activity_main_screen); //declare buttons addnewbutton= (button)findviewbyid(r.id.addnewbutton); sortbynamebutton= (button)findviewbyid(r.id.sortbynamebutton); sortbybusinessbutton= (button)findviewbyid(r.id.sortbybusinessbutton); sortbyphonebutton= (button)findviewbyid(r.id.sortbyphonebutton); //set onclick listeners addnewbutton.setonclicklistener(this); //set button font addnewbutton.settext("add new"); sortbynamebutton.settext("sort name"); sortbybusinessbutton.settext("sort business"); sortbyphonebutton.settext("sort phone #"); userdb = new dbadapter(this); userdb.open(); cardlist=(listview) findviewbyid(id.cardlist); populatelistviewfromdb(); } @override public void ondestroy(){ userdb.close(); } private void populatelistviewfromdb() { cursor cursor = userdb.getallrows(); //allow activity manage cursor lifetime (dont want memory leak) startmanagingcursor(cursor); //map cursor view fields string[] fromfieldnames = new string[]{dbadapter.key_bus_name}; int[] toviewids = new int[]{r.id.business_name}; //adapter map out columns , rows of db (deprecated, it's okay) simplecursoradapter cursoradapter = new simplecursoradapter( this, //context r.layout.item_layout, //layout template cursor, //cursor fromfieldnames, //information toviewids //where put information ); //string[] vals = {"me","you","he","she"}; //arrayadapter<string> adapter = new arrayadapter<string>(this, layout.activity_main_screen, vals); //set adapter list view element cardlist.setadapter(cursoradapter); } }
dbadapter class:
public class dbadapter { private static final string tag = "dbadapter"; // db fields public static final string key_rowid = "_id"; public static final int col_rowid = 0; /* * change 1: */ // todo: setup fields here: public static final string key_bus_name = "businessname"; public static final string key_pers_name = "personalname"; public static final string key_address = "address"; public static final string key_phone_num = "phonenum"; public static final string key_email = "email"; // todo: setup field numbers here (0 = key_rowid, 1=...) public static final int col_bus_name = 1; public static final int col_pers_name = 2; public static final int col_address = 3; public static final int col_phone_num = 4; public static final int col_email = 5; public static final string[] all_keys = new string[] {key_rowid, key_bus_name, key_pers_name, key_address, key_phone_num, key_email}; // db info: name, , table using (just one). public static final string database_name = "localcards"; public static final string database_table = "cardinfo"; // track db version if new version of app changes format. public static final int database_version = 3; private static final string database_create_sql = "create table " + database_table + " (" + key_rowid + " integer primary key autoincrement, " /* * change 2: */ // todo: place fields here! // + key_{...} + " {type} not null" // - key column name created above. // - {type} 1 of: text, integer, real, blob // (http://www.sqlite.org/datatype3.html) // - "not null" means required field (must given value). // note: must comma separated (end of line!) last 1 must have no comma!! + key_bus_name + " text, " + key_pers_name + " text, " + key_address + " text, " + key_phone_num + " text, " + key_email + " text" // rest of creation: + ");"; // context of application uses us. private final context context; private databasehelper mydbhelper; private sqlitedatabase db; ///////////////////////////////////////////////////////////////////// // public methods: ///////////////////////////////////////////////////////////////////// public dbadapter(context ctx) { this.context = ctx; mydbhelper = new databasehelper(context); //cursor x = db.rawquery("select "+key_bus_name+" " + database_table,null); } // open database connection. public dbadapter open() { db = mydbhelper.getwritabledatabase(); //db.execsql(database_create_sql); return this; } // close database connection. public void close() { mydbhelper.close(); } // add new set of values database. public void insertrow(string bus_name, string pers_name, string address, string phone, string email) { /* * change 3: // todo: update data in row new fields. // todo: change function's arguments need! // create row's data: contentvalues values = new contentvalues(); values.put(key_bus_name, bus_name); values.put(key_pers_name, pers_name); values.put(key_address, address); values.put(key_phone_num, phone); values.put(key_email, email);*/ // insert database. //return db.insert(database_table, null, values); db.rawquery("insert " + database_table + " (" + key_bus_name + "," + key_pers_name + "," + key_address + "," + key_phone_num + "," + key_email + ") values (" + "'" + bus_name + "','" + pers_name + "','" + address + "','" + phone + "','" + email + "');",null); } // delete row database, rowid (primary key) public boolean deleterow(long rowid) { string = key_rowid + "=" + rowid; return db.delete(database_table, where, null) != 0; } public void deleteall() { cursor c = getallrows(); long rowid = c.getcolumnindexorthrow(key_rowid); if (c.movetofirst()) { { deleterow(c.getlong((int) rowid)); } while (c.movetonext()); } c.close(); } // return data in database. public cursor getallrows() { string = null; cursor c = db.query(true, database_table, all_keys, where, null, null, null, null, null); //cursor c = db.rawquery("select * " + database_table, null); if (c != null) { c.movetofirst(); } return c; } // specific row (by rowid) public cursor getrow(long rowid) { string = key_rowid + "=" + rowid; cursor c = db.query(true, database_table, all_keys, where, null, null, null, null, null); if (c != null) { c.movetofirst(); } return c; } // change existing row equal new data. public boolean updaterow(long rowid, string bus_name, string pers_name, string address, string phone, string email) { string = key_rowid + "=" + rowid; /* * change 4: */ // todo: update data in row new fields. // todo: change function's arguments need! // create row's data: contentvalues newvalues = new contentvalues(); newvalues.put(key_bus_name, bus_name); newvalues.put(key_pers_name, pers_name); newvalues.put(key_address, address); newvalues.put(key_phone_num, phone); newvalues.put(key_email, email); // insert database. return db.update(database_table, newvalues, where, null) != 0; } ///////////////////////////////////////////////////////////////////// // private helper classes: ///////////////////////////////////////////////////////////////////// /** * private class handles database creation , upgrading. * used handle low-level database access. */ private static class databasehelper extends sqliteopenhelper { databasehelper(context context) { super(context, database_name, null, database_version); } @override public void oncreate(sqlitedatabase _db) { _db.execsql(database_create_sql); } @override public void onupgrade(sqlitedatabase _db, int oldversion, int newversion) { log.w(tag, "upgrading application's database version " + oldversion + " " + newversion + ", destroy old data!"); // destroy old database: _db.execsql("drop table if exists " + database_table); // recreate new database: oncreate(_db); } } }
item_layout.xml:
<?xml version="1.0" encoding="utf-8"?>
<textview android:layout_width="wrap_content" android:layout_height="wrap_content" android:textappearance="?android:attr/textappearancelarge" android:text="large text" android:id="@+id/business_name" android:layout_alignparenttop="true" android:layout_centerhorizontal="true" /> <textview android:layout_width="wrap_content" android:layout_height="wrap_content" android:textappearance="?android:attr/textappearancemedium" android:text="medium text" android:id="@+id/personal_name" android:layout_below="@+id/business_name" android:layout_alignparentleft="true" /> <textview android:layout_width="wrap_content" android:layout_height="wrap_content" android:textappearance="?android:attr/textappearancesmall" android:text="small text" android:id="@+id/phone_number" android:layout_below="@+id/business_name" android:layout_alignparentright="true" /> <textview android:layout_width="wrap_content" android:layout_height="wrap_content" android:textappearance="?android:attr/textappearancesmall" android:text="small text" android:id="@+id/email" android:layout_below="@+id/phone_number" android:layout_alignparentright="true" />
activity_main_screen.xml:
<?xml version="1.0" encoding="utf-8"?>
<button android:layout_width="75dp" android:layout_height="50dp" android:text="sort name" android:id="@+id/sortbynamebutton" android:layout_alignparenttop="true" android:layout_alignparentleft="true" /> <button android:layout_width="100dp" android:layout_height="50dp" android:text="sort business" android:id="@+id/sortbybusinessbutton" android:layout_alignparenttop="true" android:layout_torightof="@+id/sortbynamebutton" /> <button android:layout_width="100dp" android:layout_height="50dp" android:text="sort phone" android:id="@+id/sortbyphonebutton" android:layout_alignparenttop="true" android:layout_torightof="@+id/sortbybusinessbutton" /> <button android:layout_width="75dp" android:layout_height="50dp" android:text="add new" android:id="@+id/addnewbutton" android:layout_alignbottom="@+id/sortbyphonebutton" android:layout_torightof="@+id/sortbyphonebutton" /> <searchview android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/searchbox" android:layout_below="@+id/sortbynamebutton" android:layout_alignparentleft="true" android:layout_alignright="@+id/addnewbutton" /> <listview android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/cardlist" android:layout_alignparentright="true" android:layout_below="@+id/searchbox" /> <textview android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="new text" android:id="@+id/textview" android:layout_below="@+id/searchbox" android:layout_alignbottom="@+id/cardlist" android:layout_alignright="@+id/searchbox" android:layout_alignparentleft="true" />
Comments
Post a Comment