java - SQL Database in Android doesnt work -
i'm trying create database 3 columns name, wins, losses. datatable should have 1 line - 1 player. tried not working, app crashes. have 2 activities , 1 regular class. in main activity checks if players name in database , if not asks him enter name. in second activity (gameactivity) supposes show me name , how time won , lost computer. there 2 buttons in there, add win , add loss, each 1 should raise number of wins or losses. sql database class extend sqliteopenhelper
mainactivity
sqlgametable db; button btn; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); btn = (button) findviewbyid(r.id.leaderboards); db = new sqlgametable(this); if(db.getname() == "") { final score score = new score("", 0, 0); button send = (button) findviewbyid(r.id.btnsend); final edittext et = (edittext) findviewbyid(r.id.edittext1); send.setvisibility(android.view.view.visible); et.setvisibility(android.view.view.visible); send.setonclicklistener(new onclicklistener() { @override public void onclick(view v) { string name = et.gettext().tostring(); score.setname(name); } }); db.addplayer("daniel", score.getwins(), score.getloses()); send.setvisibility(android.view.view.invisible); et.setvisibility(android.view.view.invisible); } final intent = new intent(this, gameactivity.class); btn.setonclicklistener(new onclicklistener() { @override public void onclick(view v) { startactivity(i); } }); }
gameactivity
button awin, alose; sqlgametable scoretable; textview tv1, tv3, tv4; score score; string name; int win=0, lose=0; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_game); scoretable = new sqlgametable(this); awin = (button) findviewbyid(r.id.button1); alose = (button) findviewbyid(r.id.button2); tv1 = (textview) findviewbyid(r.id.textview1); tv3 = (textview) findviewbyid(r.id.textview3); tv4 = (textview) findviewbyid(r.id.textview4); name = scoretable.getname(); win = scoretable.getwin(); lose = scoretable.getlose(); tv1.settext(name + ""); tv3.settext(win + ""); tv4.settext(lose + ""); alose.setonclicklistener(new onclicklistener() { @override public void onclick(view v) { lose++; scoretable.reset(); scoretable.addplayer(name, win, lose); lose = scoretable.getlose(); tv4.settext("" + (lose)); } }); awin.setonclicklistener(new onclicklistener() { @override public void onclick(view v) { win++; scoretable.reset(); scoretable.addplayer(name, win, lose); win = scoretable.getwin(); tv3.settext("" + (win)); } }); }
sqlgametable extends sqliteopenhelper
public static string database_name = "highscores"; public static string table_name = "scores"; public static string key_id = "id"; public static string key_name = "name"; public static string key_win = "win"; public static string key_lose = "lose"; public sqlgametable(context c) { super(c, database_name, null, 1); } @override public void oncreate(sqlitedatabase db) { string sql = "create table if not exists scores (id integer primary key autoincrement, name string, win integer, lose integer)"; db.execsql(sql); } @override public void onupgrade(sqlitedatabase db, int oldversion, int newversion) { } public void addplayer(string name, int win, int lose) { contentvalues values = new contentvalues(); values.put(key_name, name); values.put(key_win, win); values.put(key_lose, lose); sqlitedatabase db = getwritabledatabase(); db.insert(table_name, null, values); db.close(); } public void reset() { sqlitedatabase db = getwritabledatabase(); db.delete(table_name, null, null); db.close(); } public string getname() { string sql = "select * " + table_name; sqlitedatabase db = getwritabledatabase(); cursor c = db.rawquery(sql, null); int row = c.getcolumnindex(key_name); db.close(); return c.getstring(row); } public int getwin() { string sql = "select * " + table_name; sqlitedatabase db = getwritabledatabase(); cursor c = db.rawquery(sql, null); int row = c.getcolumnindex(key_win); db.close(); return integer.parseint(c.getstring(row)); } public int getlose() { string sql = "select * " + table_name; sqlitedatabase db = getwritabledatabase(); cursor c = db.rawquery(sql, null); int row = c.getcolumnindex(key_lose); db.close(); return integer.parseint(c.getstring(row)); }
Comments
Post a Comment