app活动会随机从sqlite数据库中获取10个问题。我想得到下一个活动的问题和答案以显示答案。我认为它应该返回" NextQ"按钮。如何设置?
public class QuizGram extends Activity {
private SQLiteDatabase database;
private static final String DB_NAME = "eng.db";
private static final String TABLE_NAME = "quiz";
private static final String QUIZ_ID = "_id";
private static final String QUIZ_TYPE = "quiz_type";
private static final String QUESTION = "question";
private static final String A = "a";
private static final String B = "b";
private static final String C = "c";
private static final String ANS = "ans";
List<Question> quesList;
int score=0;
int qid=0;
Question currentQ;
TextView Questions;
RadioButton Radio0, Radio1, Radio2;
Button NextQ;
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.quizdetail);
DBhelper dbHelper = new DBhelper(this, DB_NAME);
database = dbHelper.openDataBase();
quesList = this.getQuestions();
currentQ = quesList.get(qid);
Questions = (TextView)findViewById(R.id.questions);
Radio0 = (RadioButton)findViewById(R.id.radio0);
Radio1 = (RadioButton)findViewById(R.id.radio1);
Radio2 = (RadioButton)findViewById(R.id.radio2);
NextQ = (Button)findViewById(R.id.nextQ);
setQuestionView();
NextQ.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
RadioGroup grp=(RadioGroup)findViewById(R.id.radioGroup1);
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(QuizGram.this, QuizResult.class);
Bundle bundle = new Bundle();
bundle.putInt("score", score); //Your score
intent.putExtras(bundle); //Put your score to your next Intent
startActivity(intent);
finish();
}
}
});
}
public List<Question> getQuestions()
{
List<Question> quesList = new ArrayList<Question>();
//db.query(table, columns, selection, selectionArgs, groupBy, having, orderBy);
Cursor cursor = database.query(TABLE_NAME,
new String[] {QUIZ_ID, QUIZ_TYPE, QUESTION, A, B, C, ANS},
QUIZ_TYPE + "='grammar'" ,
null, null, null, "RANDOM()", "10");
// looping through all rows and adding to list
if (cursor.moveToFirst()) {
do {
Question quest = new Question();
quest.setID(cursor.getInt(0));
quest.setTYPE(cursor.getString(1));
quest.setQUESTION(cursor.getString(2));
quest.setOPTA(cursor.getString(3));
quest.setOPTB(cursor.getString(4));
quest.setOPTC(cursor.getString(5));
quest.setANSWER(cursor.getString(6));
quesList.add(quest);
} while (cursor.moveToNext());
}
// return quest list
return quesList;
}
private void setQuestionView()
{
Questions.setText(currentQ.getQUESTION());
Radio0.setText(currentQ.getOPTA());
Radio1.setText(currentQ.getOPTB());
Radio2.setText(currentQ.getOPTC());
qid++;
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event)
{
if(keyCode == KeyEvent.KEYCODE_BACK){
Intent myIntent = new Intent();
myIntent = new Intent(QuizGram.this, Quiz.class);
startActivity(myIntent);
this.finish();
}
return super.onKeyDown(keyCode, event);
}
}
答案 0 :(得分:1)
它非常简单,只需在你想要下一个活动的onclick功能下定义它
String strMessage = yourEdittext.getText().toString());
String strMessage1 = yourEdittext1.getText().toString());
String strMessage2 = yourEdittext2.getText().toString());
// so on you can directly set values
// you have to just set values in string
Intent i = new Intent(getApplicationContext(), nextactivity.class)
i.putExtra("new_variable_name",strMessage);
i.putExtra("new_variable_name1",strMessage1);
i.putExtra("new_variable_name2",strMessage2);
startActivity(i);
然后在您的下一个活动中通过
检索它Bundle extras = getIntent().getExtras();
String var="";
String var1="";
String var2="";
if (extras != null)
{
String value= extras.getString("new_variable_name");
value= var;
String value1= extras.getString("new_variable_name1");
value1= var1;
String value2= extras.getString("new_variable_name2");
value2= var;
}
答案 1 :(得分:0)
1.您可以通过将Question对象传递给intent来完成此操作。 在发送活动 - &gt;
intent .putExtra("Question", currentQ );
接收活动 - &gt;
Bundle bundle = getIntent().getExtras();
Question q= (Question) bundle.getSerializable("Question");
确保为Question类实现Serializable。
2.其他选项是通过意图传递单个值