i need function on mydbadapter extract users sqlite database, , then, return list<friend>.
this munch hard me, testing , trying understand lot of google examples , stackoverflow posts can't it
i tryed code doesn't works, cursorindexoutofboundsexception: index -1 requested, size of 3
public list<friend> retrieveallusers() { list <friend> friends=new arraylist<friend>(); cursor result=fetchallusers(); (int i=0; i<result.getcount();i++) { //note.getstring(note.getcolumnindexorthrow(notesdbadapter.key_title))); friends.add(new friend(result.getstring(result.getcolumnindexorthrow("email")), "","","","")); } return friends; }
please can me doing function ?
i have user (friend) class, this:
public class friend { private string email; //id de usuario private string password; private string fullname; private string movilephone; private string movileoperatingsystem;
and have sqlite database this:
public class mydbadapter { private static final string tag = "notesdbadapter"; private databasehelper mdbhelper; private sqlitedatabase mdb; private static final string database_name = "gpslocdb"; private static final string permission_table_create = "create table permission ( fk_email1 varchar, fk_email2 varchar, validated tinyint, hour1 time default '08:00:00', hour2 time default '20:00:00', date1 date, date2 date, weekend tinyint default '0', fk_type varchar, primary key (fk_email1,fk_email2))"; private static final string user_table_create = "create table user ( email varchar, password varchar, fullname varchar, mobilephone varchar, mobileoperatingsystem varchar, primary key (email))"; private static final int database_version = 2; private final context mctx; private static class databasehelper extends sqliteopenhelper { databasehelper(context context) { super(context, database_name, null, database_version); } @override public void oncreate(sqlitedatabase db) { db.execsql(permission_table_create); db.execsql(user_table_create); } @override public void onupgrade(sqlitedatabase db, int oldversion, int newversion) { db.execsql("drop table if exists user"); db.execsql("drop table if exists permission"); oncreate(db); } } /** * constructor - takes context allow database * opened/created * * @param ctx context within work */ public mydbadapter(context ctx) { this.mctx = ctx; } /** * open database. if cannot opened, try create new * instance of database. if cannot created, throw exception * signal failure * * @return (self reference, allowing chained in * initialization call) * @throws sqlexception if database neither opened or created */ public mydbadapter open() throws sqlexception { mdbhelper = new databasehelper(mctx); mdb = mdbhelper.getwritabledatabase(); return this; } public void close() { mdbhelper.close(); } public long createuser(string email, string password, string fullname, string mobilephone, string mobileoperatingsystem) { contentvalues initialvalues = new contentvalues(); initialvalues.put("email",email); initialvalues.put("password",password); initialvalues.put("fullname",fullname); initialvalues.put("mobilephone",mobilephone); initialvalues.put("mobileoperatingsystem",mobileoperatingsystem); return mdb.insert("user", null, initialvalues); } public cursor fetchallusers() { return mdb.query("user", new string[] {"email", "password", "fullname", "mobilephone", "mobileoperatingsystem"}, null, null, null, null, null); } public cursor fetchuser(string email) throws sqlexception { cursor mcursor = mdb.query(true, "user", new string[] {"email", "password", "fullname", "mobilephone", "mobileoperatingsystem"} , "email" + "=" + email, null, null, null, null, null); if (mcursor != null) { mcursor.movetofirst(); } return mcursor; } }
cursor result=fetchallusers(); if( result.movetofirst() ){ do{ //note.getstring(note.getcolumnindexorthrow(notesdbadapter.key_title))); friends.add(new friend(result.getstring(result.getcolumnindexorthrow("email")), "","","","")); }while( result.movetonext() ); }
Comments
Post a Comment