java - How to retrieve data on button and move to Next button? -
i working on android quiz . have created database 1 question , 4 options along-with difficulty level. have created layout display question 4 buttons . problem how connect database question , 4 buttons.
so to, when click on right button moves next question , when click on wrong button gives error , exits.
code in 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" android:gravity="center_horizontal" android:background="@drawable/background"> <linearlayout android:orientation="horizontal" android:layout_width="wrap_content" android:layout_height="110dp" android:paddingtop="5dip" android:paddingbottom="5dip" android:gravity="center_horizontal"> <imageview android:id="@+id/logo" android:layout_width="wrap_content" android:layout_height="wrap_content" android:paddingbottom="1dip" android:paddingtop="1dip" android:src="@drawable/logo2" /> </linearlayout> <linearlayout android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="wrap_content" android:paddingtop="5dip" android:paddingbottom="5dip" android:gravity="center_horizontal"> <radiogroup android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="vertical" android:background="#99ccff" android:id="@+id/group1"> <textview android:layout_width="fill_parent" android:layout_height="wrap_content" android:background="#0000cc" android:textstyle="bold" android:id="@+id/question"/> <button android:onclick="false" android:id="@+id/answer1" android:layout_width="150dip" /> <button android:onclick="false" android:id="@+id/answer2" android:layout_width="150dip" /> <button android:onclick="false" android:id="@+id/answer3" android:layout_width="150dip"/> <button android:onclick="false" android:id="@+id/answer4" android:layout_width="150dip" /> </radiogroup> </linearlayout>
my dbhelper.java
public class dbhelper extends sqliteopenhelper{ //the android's default system path of application database. private static string db_path = "/data/data/com.starchazer.cyk/databases/"; private static string db_name = "questionsdb"; private sqlitedatabase mydatabase; private final context mycontext; /** * constructor * takes , keeps reference of passed context in order access application assets , resources. * @param context */ public dbhelper(context context) { super(context, db_name, null, 1); this.mycontext = context; } /** * creates empty database on system , rewrites own database. * */ public void createdatabase() throws ioexception{ boolean dbexist = checkdatabase(); if(!dbexist) { //by calling method , empty database created default system path //of application gonna able overwrite database our database. this.getreadabledatabase(); try { copydatabase(); } catch (ioexception e) { throw new error("error copying database"); } } } /** * check if database exist avoid re-copying file each time open application. * @return true if exists, false if doesn't */ private boolean checkdatabase(){ sqlitedatabase checkdb = null; try{ string mypath = db_path + db_name; checkdb = sqlitedatabase.opendatabase(mypath, null, sqlitedatabase.open_readonly); }catch(sqliteexception e){ //database does't exist yet. } if(checkdb != null){ checkdb.close(); } return checkdb != null ? true : false; } /** * copies database local assets-folder created empty database in * system folder, can accessed , handled. * done transfering bytestream. * */ private void copydatabase() throws ioexception{ //open local db input stream inputstream myinput = mycontext.getassets().open(db_name); // path created empty db string outfilename = db_path + db_name; //open empty db output stream outputstream myoutput = new fileoutputstream(outfilename); //transfer bytes inputfile outputfile byte[] buffer = new byte[1024]; int length; while ((length = myinput.read(buffer))>0){ myoutput.write(buffer, 0, length); } //close streams myoutput.flush(); myoutput.close(); myinput.close(); } public void opendatabase() throws sqlexception{ //open database string mypath = db_path + db_name; mydatabase = sqlitedatabase.opendatabase(mypath, null, sqlitedatabase.open_readonly); } @override public synchronized void close() { if(mydatabase != null) mydatabase.close(); super.close(); } @override public void oncreate(sqlitedatabase db) { } @override public void onupgrade(sqlitedatabase db, int oldversion, int newversion) { } // add public helper methods access , content database. // return cursors doing "return mydatabase.query(....)" it'd easy // create adapters views. public list<question> getquestionset(int difficulty, int numq){ list<question> questionset = new arraylist<question>(); cursor c = mydatabase.rawquery("select * questions difficulty=" + difficulty + " order random() limit " + numq, null); while (c.movetonext()){ //log.d("question", "question found in db: " + c.getstring(1)); question q = new question(); q.setquestion(c.getstring(1)); q.setanswer(c.getstring(2)); q.setoption1(c.getstring(3)); q.setoption2(c.getstring(4)); q.setoption3(c.getstring(5)); q.setrating(difficulty); questionset.add(q); } return questionset; } }
my questionactivity
public class questionactivity extends activity implements onclicklistener{ private question currentq; private gameplay currentgame; @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.question); /** * configure current game , question */ currentgame = ((xyz_application)getapplication()).getcurrentgame(); currentq = currentgame.getnextquestion(); button c1 = (button) findviewbyid(r.id.answer1 ); c1.setonclicklistener(this); button c2 = (button) findviewbyid(r.id.answer2 ); c2.setonclicklistener(this); button c3 = (button) findviewbyid(r.id.answer3 ); c3.setonclicklistener(this); button c4 = (button) findviewbyid(r.id.answer4 ); c4.setonclicklistener(this); /** * update question , answer options.. */ setquestions(); } /** * method set text question , answers current games * current question */ private void setquestions() { //set question text current question string question = utility.capitalise(currentq.getquestion()) + "?"; textview qtext = (textview) findviewbyid(r.id.question); qtext.settext(question); //set available options list<string> answers = currentq.getquestionoptions(); textview option1 = (textview) findviewbyid(r.id.answer1); option1.settext(utility.capitalise(answers.get(0))); textview option2 = (textview) findviewbyid(r.id.answer2); option2.settext(utility.capitalise(answers.get(1))); textview option3 = (textview) findviewbyid(r.id.answer3); option3.settext(utility.capitalise(answers.get(2))); textview option4 = (textview) findviewbyid(r.id.answer4); option4.settext(utility.capitalise(answers.get(3))); } @override public void onclick(view arg0) { /** * validate buttonselected has been selected */ if (!checkanswer()) return; /** * check if end of game */ if (currentgame.isgameover()){ intent = new intent(this, main.class); startactivity(i); finish(); } else{ intent = new intent(this, questionactivity.class); startactivity(i); finish(); } } @override public boolean onkeydown(int keycode, keyevent event) { switch (keycode) { case keyevent.keycode_back : return true; } return super.onkeydown(keycode, event); } /** * check if checkbox has been selected, , if * has check if correct , update gamescore */ private boolean checkanswer() { string answer = getselectedanswer(); if (answer==null){ return false; } else { if (currentq.getanswer().equalsignorecase(answer)) { //log.d("questions", "correct answer!"); currentgame.incrementrightanswers(); } else{ //log.d("questions", "incorrect answer!"); currentgame.incrementwronganswers(); } return true; } } /** * */ private string getselectedanswer() { button c1 = (button)findviewbyid(r.id.answer1); button c2 = (button)findviewbyid(r.id.answer2); button c3 = (button)findviewbyid(r.id.answer3); button c4 = (button)findviewbyid(r.id.answer4); if (c1.callonclick()) { return c1.gettext().tostring(); } if (c2.callonclick()) { return c2.gettext().tostring(); } if (c3.callonclick()) { return c3.gettext().tostring(); } if (c4.callonclick()) { return c4.gettext().tostring(); } return null; } }
i sorry not giving code here.
here have done when actvity starts
- load question db options
- i ll prefer option array/arraylist
- assign text option each button
add common onclick listener this
public void onclick(view v){ int id=v.getid(); string answer=v.gettext(); if(answer.equals('answer db'){ // if right next question // else mainactivity } /**switch(id){ case r.id.answer1: processanswer(1); break; case r.id.answer2: processanswer(2); break; // on } **/ }
and processanswer
void processanswer(int option){ // since have option here // check database // if correct next question else exit }
Comments
Post a Comment