java - passing data from sqlite to a texview -
i trying pass data sqlite table username textview in home fragment class, having difficulties displaying data in textview. here databasehelper class table created:
public class databasehandler extends sqliteopenhelper {
// static variables // database version private static final int database_version = 1; // database name private static final string database_name = "u168512214_barzd"; // login table name private static final string table_login = "login"; // login table columns names private static final string key_id = "id"; private static final string key_name = "name"; private static final string key_email = "email"; private static final string key_uid = "uid"; private static final string key_created_at = "created_at"; public databasehandler(context context) { super(context, database_name, null, database_version); } // creating tables @override public void oncreate(sqlitedatabase db) { string create_login_table = "create table " + table_login + "(" + key_id + " integer primary key," + key_name + " text," + key_email + " text unique," + key_uid + " text," + key_created_at + " text" + ")"; db.execsql(create_login_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_login); // create tables again oncreate(db); } /** * storing user details in database * */ public void adduser(string name, string email, string uid, string created_at) { sqlitedatabase db = this.getwritabledatabase(); contentvalues values = new contentvalues(); values.put(key_name, name); // name values.put(key_email, email); // email values.put(key_uid, uid); // email values.put(key_created_at, created_at); // created @ // inserting row db.insert(table_login, null, values); db.close(); // closing database connection } /** * getting user data database * */ public hashmap<string, string> getuserdetails(){ hashmap<string,string> user = new hashmap<string,string>(); string selectquery = "select * " + table_login; sqlitedatabase db = this.getreadabledatabase(); cursor cursor = db.rawquery(selectquery, null); // move first row cursor.movetofirst(); if(cursor.getcount() > 0){ log.i("key_name","value is: " + cursor.getstring(1)); log.i("key_email","value is: " + cursor.getstring(2)); log.i("key_uid","value is: " + cursor.getstring(3)); log.i("key_created_at","value is: " + cursor.getstring(4)); } cursor.close(); db.close(); // return user return user; } /** * getting user login status * return true if rows there in table * */ public int getrowcount() { string countquery = "select * " + table_login; sqlitedatabase db = this.getreadabledatabase(); cursor cursor = db.rawquery(countquery, null); int rowcount = cursor.getcount(); db.close(); cursor.close(); // return row count return rowcount; }
her fragmenthome class want display username in textview:
databasehandler db;
alertdialogmanager alert = new alertdialogmanager();
// session manager class arraylist> namelist;
public homefragment(){} @override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { view rootview = inflater.inflate(r.layout.fragment_home, container, false); btnviewbars = (button)rootview.findviewbyid(r.id.btnviewbars); btnnewbar = (button)rootview.findviewbyid(r.id.btncreatebar); // return rootview; db= new databasehandler(getactivity()); hashmap<string, string> user = db.getuserdetails(); for(int i=0; i<user.size();i++) { username.settext(user.get(i).get("name")); }
Comments
Post a Comment