java - Sending data from activity to class -
i need guys. i'm making quiz i'm stuck. first of got activity can choose between categories:
package com.laurenswuytsjordipapen.cultural.pursuit; import android.app.activity; import android.content.intent; import android.content.sharedpreferences; import android.os.bundle; import android.preference.preferencemanager; import android.view.view; import android.widget.button; import com.laurenswuytsjordipapen.cultural.pursuit.library.userfunctions; public class dashboardactivity extends activity { userfunctions userfunctions; button btnlogout; button btneuropa; button btnamerika; button btnafrika; button btnazie; @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); /** * dashboard screen application * */ // check login status in database userfunctions = new userfunctions(); if(userfunctions.isuserloggedin(getapplicationcontext())){ setcontentview(r.layout.dashboard); btnlogout = (button) findviewbyid(r.id.btnlogout); btneuropa = (button) findviewbyid(r.id.btneuropa); btnamerika = (button) findviewbyid(r.id.btnamerika); btnafrika = (button) findviewbyid(r.id.btnafrika); btnazie = (button) findviewbyid(r.id.btnazie); btnlogout.setonclicklistener(new view.onclicklistener() { public void onclick(view arg0) { // todo auto-generated method stub userfunctions.logoutuser(getapplicationcontext()); intent login = new intent(getapplicationcontext(), loginactivity.class); login.addflags(intent.flag_activity_clear_top); startactivity(login); // closing dashboard screen finish(); } }); btneuropa.setonclicklistener(new view.onclicklistener() { public void onclick(view arg0) { intent start = new intent(getapplicationcontext(), mainactivity.class); start.putextra("continent","europa"); startactivity(start); // closing dashboard screen finish(); } }); btnamerika.setonclicklistener(new view.onclicklistener() { public void onclick(view arg0) { intent start = new intent(getapplicationcontext(), mainactivity.class); start.putextra("continent","amerika"); startactivity(start); // closing dashboard screen finish(); } }); btnafrika.setonclicklistener(new view.onclicklistener() { public void onclick(view arg0) { intent start = new intent(getapplicationcontext(), mainactivity.class); start.putextra("continent","afrika"); startactivity(start); // closing dashboard screen finish(); } }); btnazie.setonclicklistener(new view.onclicklistener() { public void onclick(view arg0) { intent start = new intent(getapplicationcontext(), mainactivity.class); start.putextra("continent","azie"); startactivity(start); // closing dashboard screen finish(); } }); }else{ // user not logged in show login screen intent login = new intent(getapplicationcontext(), loginactivity.class); login.addflags(intent.flag_activity_clear_top); startactivity(login); // closing dashboard screen finish(); } } }
and go quiz = mainactivity. send intent mainacitivty knows category is:
package com.laurenswuytsjordipapen.cultural.pursuit; import java.util.list; import com.laurenswuytsjordipapen.cultural.pursuit.library.userfunctions; import android.os.bundle; import android.preference.preferencemanager; import android.app.activity; import android.content.intent; import android.content.sharedpreferences; import android.util.log; import android.view.menu; import android.view.view; import android.widget.button; import android.widget.radiobutton; import android.widget.radiogroup; import android.widget.textview; public class mainactivity extends activity { userfunctions userfunctions; button btnlogout; list<vragen> queslist; int score=0; int qid=0; vragen currentq; textview txtvraag; radiobutton rda, rdb, rdc; button butvolgende; string value; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); bundle extras = getintent().getextras(); if (extras != null) { value = extras.getstring("continent"); } dbhelper db=new dbhelper(this); queslist=db.getallquestions(); currentq=queslist.get(qid); txtvraag=(textview)findviewbyid(r.id.txtvraag); rda=(radiobutton)findviewbyid(r.id.antwoord1); rdb=(radiobutton)findviewbyid(r.id.antwoord2); rdc=(radiobutton)findviewbyid(r.id.antwoord3); butvolgende=(button)findviewbyid(r.id.btnvolgende); setquestionview(); butvolgende.setonclicklistener(new view.onclicklistener() { public void onclick(view v) { radiogroup grp=(radiogroup)findviewbyid(r.id.grpantwoord); radiobutton answer=(radiobutton)findviewbyid(grp.getcheckedradiobuttonid()); log.d("yourans", currentq.getanswer()+" "+answer.gettext()); if(currentq.getanswer().equals(answer.gettext())) { score++; log.d("score", "your score"+score); } if(qid<10){ currentq=queslist.get(qid); setquestionview(); }else{ intent intent = new intent(mainactivity.this, resultactivity.class); bundle b = new bundle(); b.putint("score", score); //your score intent.putextras(b); //put score next intent startactivity(intent); finish(); } } }); } @override public boolean oncreateoptionsmenu(menu menu) { // inflate menu; adds items action bar if present. getmenuinflater().inflate(r.menu.activity_main, menu); return true; } private void setquestionview() { txtvraag.settext(currentq.getquestion()); rda.settext(currentq.getopta()); rdb.settext(currentq.getoptb()); rdc.settext(currentq.getoptc()); qid++; } }
but questions in class dbhelper:
package com.laurenswuytsjordipapen.cultural.pursuit; import java.util.arraylist; import java.util.list; import android.content.contentvalues; import android.content.context; import android.database.cursor; import android.database.sqlite.sqlitedatabase; import android.database.sqlite.sqliteopenhelper; public class dbhelper extends sqliteopenhelper { private static final int database_version = 1; // database name private static final string database_name = "dbquiz"; // tasks table name private static final string table_quest = "quest"; // tasks table columns names private static final string key_id = "id"; 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 sqlitedatabase dbase; public dbhelper(context context) { super(context, database_name, null, database_version); } @override public void oncreate(sqlitedatabase db) { dbase=db; string sql = "create table if not exists " + table_quest + " ( " + key_id + " integer primary key autoincrement, " + key_ques + " text, " + key_answer+ " text, "+key_opta +" text, " +key_optb +" text, "+key_optc+" text)"; db.execsql(sql); addquestions(); //db.close(); } private void addquestions() { vragen q1=new vragen("op welke hooglanden draven er wilde rendieren rond in groot brittanie?","andes gebergte", "schotse hooglanden", "britse hooglanden", "schotse hooglanden"); this.addvraag(q1); vragen q2=new vragen("in welke italiaanse stad zijn de ponte vechio en het palazzo medici belangrijke bezienswaardigheden??", "milan", "rome", "firenze", "firenze"); this.addvraag(q2); vragen q3=new vragen("wat het favoriete land van een gallomaan?","frankrijk", "egypte", "duitsland", "frankrijk"); this.addvraag(q3); vragen q4=new vragen("welke europese hoofdstad net zoals rome op zeven heuvels gebouwd?", "brussel", "parijs", "rome", "brussel"); this.addvraag(q4); vragen q5=new vragen("wat naast vaticaanstad, monaco, san marino en andorra het vijfde europese ministaatje?","corsica", "luxemburg", "lichtenstein", "lichtenstein"); this.addvraag(q5); vragen q6=new vragen("aan welke golf ligt de kuststreek tussen marseille en perpignan?", "golf van frankrijk", "golf van lyon", "middellandse zee", "golf van lyon"); this.addvraag(q6); vragen q7=new vragen("welke nationaliteit heeft hordeloper jonathan n senga?","belgische", "canadees", "ganees", "belgische"); this.addvraag(q7); vragen q8=new vragen("welke munt werd de alpendollar genoemd?", "oostenrijkse shilling", "bulgaarse lef", "franse frank", "oostenrijkse shilling"); this.addvraag(q8); vragen q9=new vragen("aan welke natie gaf zanzibar zich in 1896 on na exact 38 minuten oorlog voeren?","engeland", "china", "amerika", "engeland"); this.addvraag(q9); vragen q10=new vragen("welke stad telde als eerste in de geschiedenis meer dan 1 miljoen inwoners?", "rome", "tokyo", "kopenhagen", "rome"); this.addvraag(q10); } @override public void onupgrade(sqlitedatabase db, int oldv, int newv) { // drop older table if existed db.execsql("drop table if exists " + table_quest); // create tables again oncreate(db); } // adding new question public void addvraag(vragen quest) { //sqlitedatabase db = this.getwritabledatabase(); contentvalues values = new contentvalues(); values.put(key_ques, quest.getquestion()); values.put(key_answer, quest.getanswer()); values.put(key_opta, quest.getopta()); values.put(key_optb, quest.getoptb()); values.put(key_optc, quest.getoptc()); // inserting row dbase.insert(table_quest, null, values); } public list<vragen> getallquestions() { list<vragen> queslist = new arraylist<vragen>(); // select query string selectquery = "select * " + table_quest; dbase=this.getreadabledatabase(); cursor cursor = dbase.rawquery(selectquery, null); // looping through rows , adding list if (cursor.movetofirst()) { { vragen quest = new vragen(); quest.setid(cursor.getint(0)); quest.setquestion(cursor.getstring(1)); quest.setanswer(cursor.getstring(2)); quest.setopta(cursor.getstring(3)); quest.setoptb(cursor.getstring(4)); quest.setoptc(cursor.getstring(5)); queslist.add(quest); } while (cursor.movetonext()); } // return quest list return queslist; } public int rowcount() { int row=0; string selectquery = "select * " + table_quest; sqlitedatabase db = this.getwritabledatabase(); cursor cursor = db.rawquery(selectquery, null); row=cursor.getcount(); return row; } }
i need somehow put intent got in mainativity class dbhelper. can make if , else clausules questions. know how can send intent "continent" mainactivity dbhelper :)
thanks in advance!
yes, can send data. think best solution giving in dbhelper
constructor.
Comments
Post a Comment