Display from SQLite database and collection in an android app? -
click here see display related xml file posted
i dont errors when running application in emulator, , there no error message when open testactivity... problem cant see text displayed supposed be.
here xml file of activity:
<?xml version="1.0" encoding="utf-8"?> <linearlayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/relatively" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#fdb913" android:orientation="vertical" android:paddingbottom="16dp" android:paddingleft="20dp" android:paddingright="20dp" android:paddingtop="16dp" tools:context="com.slbw.stoplookandbewarned.testactivity" android:weightsum="1" android:gravity="center_vertical|center_horizontal|center"> <linearlayout android:id="@+id/linearlayout1" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="0.90" android:orientation="vertical" android:weightsum="1" > <linearlayout android:id="@+id/linearlayout11" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="0.20" android:orientation="vertical" android:weightsum="1" android:padding="0dp"> <textview android:id="@+id/tv_score" android:layout_width="match_parent" android:layout_height="0dp" android:layout_gravity="center" android:gravity="center" android:text="score" android:textcolor="#ffffff" android:textsize="25.0sp" android:textstyle="bold" android:layout_weight="1" android:textalignment="textstart" /> <textview android:layout_width="67dp" android:gravity="center" android:textsize="18sp" android:id="@+id/score" android:layout_height="wrap_content" /> </linearlayout> <linearlayout android:id="@+id/linearlayout12" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="0.60" android:orientation="vertical" android:weightsum="1" > <relativelayout android:layout_width="match_parent" android:layout_weight="0.24" android:layout_height="200dp" android:gravity="center_vertical|center_horizontal"> <textview android:id="@+id/txtquestion" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_gravity="center" android:gravity="center" android:textcolor="#ffffff" android:textsize="35.0sp" android:textstyle="bold" android:visibility="invisible" android:layout_alignbottom="@+id/imgquestion" android:layout_centerhorizontal="true" android:layout_marginbottom="30dp" /> <imageview android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/imgquestion" android:layout_centerhorizontal="true" android:visibility="invisible" /> </relativelayout> <linearlayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="0.20" android:orientation="vertical" android:weightsum="1" android:padding="10dp"> <button android:id="@+id/button1" android:layout_width="267dp" android:layout_height="wrap_content" android:layout_gravity="center" android:layout_marginleft="80dp" android:layout_marginright="80dp" android:layout_margintop="5dp" android:background="#fff" android:gravity="center" android:fontfamily="sans-serif-medium" /> <button android:id="@+id/button2" android:layout_width="268dp" android:layout_height="wrap_content" android:layout_gravity="center" android:layout_marginleft="80dp" android:layout_marginright="80dp" android:layout_margintop="5dp" android:background="#fff" android:gravity="center" android:textcolor="#000000" android:textsize="22.0sp" /> <button android:id="@+id/button3" android:layout_width="267dp" android:layout_height="wrap_content" android:layout_gravity="center" android:layout_marginleft="80dp" android:layout_marginright="80dp" android:layout_margintop="5dp" android:background="#fff" android:gravity="center" android:textcolor="#000000" android:textsize="22.0sp" /> </linearlayout> </linearlayout> <textview android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/tv_counter" android:layout_weight="0.20" /> </linearlayout> </linearlayout>
here relevant class files:
public class testactivity extends appcompatactivity { list<question> queslist; int score; int qid = 0; //static int n; question currentq; quizhelper db; textview scored, tv_score; public textview txtquestion, txtcounter; public button button1, button2, button3; public static string ans; public static imageview img; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.testactivity2); quizhelper db = new quizhelper(this); queslist = db.getallquestions(); currentq = queslist.get(qid); this.score = 0; tv_score = (textview) findviewbyid(r.id.score); tv_score.settext(string.valueof(score)); txtcounter = (textview) findviewbyid(r.id.tv_counter); db = new quizhelper(this); queslist = db.getallquestions(); currentq = queslist.get(qid); txtquestion = (textview) findviewbyid(r.id.txtquestion); img = (imageview) findviewbyid(r.id.imgquestion); button1 = (button) findviewbyid(r.id.button1); button2 = (button) findviewbyid(r.id.button2); button3 = (button) findviewbyid(r.id.button3); scored = (textview) findviewbyid(r.id.score); //db.getquestion(); setquestionview(); // button click listeners button1.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { // passing button text other method // check whether answer correct or not // same 3 buttons getanswer(button1.gettext().tostring()); } }); button2.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { getanswer(button2.gettext().tostring()); } }); button3.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { getanswer(button3.gettext().tostring()); } }); } private void setquestionview(){ //display control txtquestion.settext(currentq.getquestion()); button1.settext(currentq.getopta()); button2.settext(currentq.getoptb()); button3.settext(currentq.getoptc()); qid++; } public void getanswer(string answerstring) { if (currentq.getanswer().equals(answerstring)) { // if conditions matches increase int (score) 1 // , set text of score view score++; scored.settext("" + this.score); toast.maketext(getapplicationcontext(), "correct!", toast.length_short).show(); //db.getquestion(); } else { intent intent = new intent(testactivity.this, result.class); // if unlucky start activity , finish game // intent intent = new intent(testactivity.this, result.class); toast.maketext(getapplicationcontext(), "wrong, sorry!", toast.length_short).show(); //db.getquestion(); // db.getscore(test_entry.username, this.score); // passing int value bundle b = new bundle(); b.putint("score", score); // score intent.putextras(b); // put score next startactivity(intent); finish(); } if(qid < 20 ) { currentq = queslist.get(qid); setquestionview(); }else{ intent intent = new intent(testactivity.this,result.class); bundle b = new bundle(); b.putint("score", score); intent.putextras(b); startactivity(intent); finish(); } } }
here questions class:
public class question extends activity{ private int id; private string question, opta, optb, optc, answer; private int isimage; public string getopta; public string getoptb; public string getoptc; public int getimg; //public byte[] getimg; //public static int id; /* public static string question; public static string opta; public static string optb; public static string optc; public static int isimage; public static string answer;*/ public question() { id = 0; question= ""; opta = ""; optb = ""; optc = ""; answer = ""; isimage = integer.parseint("0"); } public question(string question, string opta, string optb, string optc, string answer, int isimage){ this.question = question; this.opta = opta; this.optb = optb; this.optc = optc; this.isimage = isimage; this.answer = answer; } public int getid(){ return this.id; } public string getquestion(){ return question; } public string getopta(){ return opta; } public string getoptb(){ return optb; } public string getoptc(){ return optc; } public string getanswer(){ return answer; } public int getimg(){ return isimage; } public void setid(int id) { // this.id = id; } public void setquestion(string question) { this.question = question; } public void setopta(string opta) { this.opta = opta; } public void setoptb(string optb) { this.optb = optb; } public void setoptc(string optc) { this.optc = optc; } public void setanswer(string ans) { this.answer = ans; } public void setisimage(int isimage) { this.isimage = isimage; } }
i've played around using 'static' display functions , variables too. none seems working.
here dbhelper have named quizhelper.java
public class quizhelper extends sqliteopenhelper { testactivity ta; scores s; private static final int database_version = 1; // database name private static final string database_name = "mathsone"; // tasks table name private static final string table_name = "quest"; private static final string table_user = "userscore"; // tasks table columns names private static final string key_id = "qid"; private static final string key_ques = "question"; private static final string key_answer = "answer"; // correct option private static final string key_opta = "opta"; // option private static final string key_optb = "optb"; // option b private static final string key_optc = "optc"; // option c private static final string key_isimage = "isimage"; // check if question image private static final string user_name = "username"; private static final string user_score = "score"; private int ctr; private sqlitedatabase dba; public quizhelper(context context) { super(context, database_name, null, database_version); // oncreate(db); } @override public void oncreate(sqlitedatabase db) { dba = db; ta = new testactivity(); string create_table = "create table if not exists " + table_name + " ( " + key_id + " integer primary key autoincrement, " + key_ques + " text, " + key_opta + " text, " + key_optb+ " text, " + key_optc+ " text, " + key_answer + " text, " + key_isimage + " integer(11));"; db.execsql(create_table); addquestion(db); } private void addquestion(sqlitedatabase db) { question q1 = new question("a driver got caught on drugs or alchohol punishable by", "suspension of license","six months of imprisonment","all of above","all of above", 0); this.addquestiontodb(q1,db); question q2 = new question("what fine driving without valid license?", "php 500.00","php 500.00 , vehicle impounded","php 3, 000.00","php 3, 000.00", 0); this.addquestiontodb(q2,db); question q3 = new question("if caught, how many days take fix case , redeem license", "30 days","15 days","10 days","15 days", 0); this.addquestiontodb(q3,db); ); } @override public void onupgrade(sqlitedatabase db, int oldv, int newv) { // drop older table if existed db.execsql("drop table if exists " + table_name); oncreate(db); } // adding new question public void addquestiontodb(question question, sqlitedatabase db) { // db = this.getwritabledatabase(); contentvalues values = new contentvalues(); //values.put(key_id, question.id); values.put(key_ques, question.getquestion()); values.put(key_answer, question.getanswer()); values.put(key_opta, question.getopta); values.put(key_optb, question.getoptb); values.put(key_optc, question.getoptc); values.put(key_isimage, question.getimg); // inserting row db.insert(table_name, null, values); } /* public void getquestion() { random r = new random(); int n = r.nextint(20); db = this.getreadabledatabase(); ctr++; cursor cursor = db.rawquery("select * " + table_quest + " `qid` = " + n + ";", null); if (cursor.movetofirst()) { if(cursor.getint(6) == 1) { ta.txtquestion.setvisibility(view.visible); ta.txtquestion.settext(cursor.getstring(1).tostring()); /*log.e("image", "i here"); ta.img.setvisibility(view.visible); int imageresource = draw.getresources().getidentifier("@drawable/"+cursor.getstring(1), null, package); drawable res = draw.getresources().getdrawable(imageresource); ta.img.setimagedrawable(res); //ta.img.setimageresource(imageresource); } else { ta.txtquestion.setvisibility(view.visible); ta.txtquestion.settext(cursor.getstring(1).tostring()); } ta.button1.settext(cursor.getstring(3).tostring()); ta.button2.settext(cursor.getstring(4).tostring()); ta.button3.settext(cursor.getstring(5).tostring()); ta.ans = cursor.getstring(2).tostring(); log.e("current question id", integer.tostring(n)); testactivity.txtcounter.settext(integer.tostring(ctr)); } }*/ public void getscore(string name, int score) { dba = this.getwritabledatabase(); contentvalues values = new contentvalues(); values.put(user_name, name); values.put(user_score, score); dba.insert(table_user, null, values); dba = this.getreadabledatabase(); cursor cursor = dba.rawquery("select * " + table_user + " order " + user_score + " desc, " + user_score + " asc;", null); s = new scores(); int ctr = cursor.getcount(), i; log.e("count", integer.tostring(ctr)); if (ctr > 0) { cursor.movetofirst(); for(i = 0; < ctr; i++) { s.name += cursor.getstring(0).tostring() + "\n"; s.score += cursor.getstring(1).tostring() + "\n"; cursor.movetonext(); } } } public boolean removescores() { dba = this.getwritabledatabase(); dba.execsql("drop table if exists " + table_user + ";"); return true; } public list<question> getallquestions() { list<question> queslist = new arraylist<question>(); // select query string selectquery = "select * " + table_name; dba = this.getreadabledatabase(); cursor cursor = dba.rawquery(selectquery, null); // looping through rows , adding list if (cursor.movetofirst()) { { //question quest = new question(); //quest.setid(cursor.getint(0)); question quest = new question(); quest.setid(cursor.getint(0)); quest.setquestion(cursor.getstring(1)); quest.setanswer(cursor.getstring(5)); quest.setopta(cursor.getstring(2)); quest.setoptb(cursor.getstring(3)); quest.setoptc(cursor.getstring(4)); quest.setisimage(cursor.getint(6)); queslist.add(quest); } while (cursor.movetonext()); } // return quest list cursor.close(); dba.close(); return queslist; } public int rowcount(){ int row = 0; string selectquery = "select * from" + table_name; sqlitedatabase db = this.getwritabledatabase(); cursor cursor = db.rawquery(selectquery, nul); row = cursor.getcount(); return row; } }
Comments
Post a Comment