android - Listview does not show data fetched from database -
i trying populate listview fetching data sqlite using cursor. no data displayed in listview though cursor has expected rows.
here code:
public class showusers extends activity { userdbadapter db; @override protected void oncreate(bundle savedinstancestate) { try { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_user_profile); db = new userdbadapter(getapplicationcontext()); db.open(); populatelistviewfromdb(); } catch (exception e) { log.e("error", "error occured: " + e.tostring()); e.printstacktrace(); } } @suppresswarnings("deprecation") private void populatelistviewfromdb() { cursor cursor = db.getallrecords(); startmanagingcursor(cursor); log.i("myapp", "total users: " + cursor.getcount()); toast.maketext(getapplicationcontext(), "number of rows: " + cursor.getcount(), toast.length_long) .show(); string[] databasecolumnnames = new string[] { userdbadapter.key_name }; int[] toviewids = new int[] { r.id.textviewname }; simplecursoradapter mycursordapter = new simplecursoradapter(this, r.layout.users_layout, cursor, databasecolumnnames, toviewids); listview list = (listview) findviewbyid(r.id.listviewfromdb); list.setadapter(mycursordapter); } @override protected void ondestroy() { super.ondestroy(); db.close(); } }
xml files: activity_user_profile.xml
<?xml version="1.0" encoding="utf-8"?> <linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <listview android:id="@+id/listviewfromdb" android:layout_width="fill_parent" android:layout_height="fill_parent" android:divider="#0000cc" android:dividerheight="0.1dp" > </listview> </linearlayout>
userlayout.xml:
<?xml version="1.0" encoding="utf-8"?> <linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <textview android:id="@+id/textviewname" android:layout_margintop="10dp" android:layout_marginbottom="10dp" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textsize="25dp" android:textcolor="#0000ff" android:text="textview" /> </linearlayout>
userdbadapeter file
public class userdbadapter { public static final string key_userid = "id"; public static final string key_name = "name"; public static final string key_birthate = "birthdate"; public static final string key_pubage = "pubage"; public static final string key_password = "password"; private static final string tag = "userdbadapter"; private static final string database_name = "userdb"; private static final string database_table = "users"; private static final int database_version = 1; private static final string database_create = "create table if not exists " + database_table + " (id integer primary key autoincrement, " + "name varchar not null, birthdate date, pubage number, password varchar );"; private final context context; private databasehelper dbhelper; private sqlitedatabase db; public userdbadapter(context ctx) { this.context = ctx; dbhelper = new databasehelper(context); } private static class databasehelper extends sqliteopenhelper { databasehelper(context context) { super(context, database_name, null, database_version); } @override public void oncreate(sqlitedatabase db) { try { db.execsql(database_create); } catch (sqlexception e) { e.printstacktrace(); } } @override public void onupgrade(sqlitedatabase db, int oldversion, int newversion) { log.w(tag, "upgrading database version " + oldversion + " " + newversion + ", destroy old data"); db.execsql("drop table if exists contacts"); oncreate(db); } } // ---opens database--- public userdbadapter open() throws sqlexception { db = dbhelper.getwritabledatabase(); return this; } // ---closes database--- public void close() { dbhelper.close(); } // ---insert record database--- public long insertrecord(string name, string password, string birthdate, int pubage) { contentvalues initialvalues = new contentvalues(); initialvalues.put(key_name, name); initialvalues.put(key_birthate, birthdate); initialvalues.put(key_pubage, pubage); initialvalues.put(key_password, password); return db.insert(database_table, null, initialvalues); } // ---deletes particular record--- public boolean deleteuser(long userid) { return db.delete(database_table, key_userid + "=" + userid, null) > 0; } // ---retrieves records--- public cursor getallrecords() { return db.query(database_table, new string[] { key_userid, key_name, key_birthate, key_pubage, key_password }, null, null, null, null, null); } // ---retrieves particular record--- public cursor getrecord(long userid) throws sqlexception { cursor mcursor = db.query(true, database_table, new string[] { key_userid, key_name, key_birthate, key_pubage, key_password }, key_userid + "=" + userid, null, null, null, null, null); if (mcursor != null) { mcursor.movetofirst(); } return mcursor; } // ---updates record--- public boolean updaterecord(long userid, string name, string birthdate, int pubage, string password) { contentvalues args = new contentvalues(); args.put(key_name, name); args.put(key_birthate, birthdate); args.put(key_pubage, pubage); args.put(key_password, password); return db.update(database_table, args, key_userid + "=" + userid, null) > 0; } }
i able insert data table activity , able read activity not able display in list view. please let me know , wrong in code added here.
Comments
Post a Comment