我正在创建一个图像测验应用....带有图像和4个单选按钮。图像通过ViewFlipper添加,选项和答案取自数据库。 该应用程序正在工作,但我无法加载结果页面,无法得到祝酒词#34;干得好:)和#34;即使答案是正确的。我无法追查问题,请帮帮我
这是我的MainActivity.java
public class MainActivity extends ActionBarActivity {
List<Question> quesList;
int score=0;
int qid=0;
Question currentQ;
TextView txtQuestion;
RadioButton rda, rdb, rdc,rdd;
ImageButton butNext;
private ViewFlipper mViewFlipper;
private GestureDetector mGestureDetector;
int[] resources = {
R.drawable.apple,
R.drawable.ball,
R.drawable.cat,
R.drawable.dog,
R.drawable.elephant
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
CustomGestureDetector customGestureDetector = new CustomGestureDetector();
mGestureDetector = new GestureDetector(this, customGestureDetector);
mViewFlipper = (ViewFlipper) findViewById(R.id.viewFlipper);
mViewFlipper.setInAnimation(this, android.R.anim.fade_in);
mViewFlipper.setOutAnimation(this, android.R.anim.fade_out);
// Add all the images to the ViewFlipper
for (int i = 0; i < resources.length; i++) {
ImageView imageView = new ImageView(this);
imageView.setImageResource(resources[i]);
mViewFlipper.addView(imageView);
}
DbHelper db=new DbHelper(this);
quesList=db.getAllQuestions();
currentQ=quesList.get(qid);
txtQuestion=(TextView)findViewById(R.id.textView1);
rda=(RadioButton)findViewById(R.id.radio0);
rdb=(RadioButton)findViewById(R.id.radio1);
rdc=(RadioButton)findViewById(R.id.radio2);
rdd=(RadioButton)findViewById(R.id.radio3);
butNext=(ImageButton)findViewById(R.id.button1);
setQuestionView();
butNext.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()))
{
Toast.makeText(MainActivity.this, "Well Done :)", Toast.LENGTH_SHORT).show();
mViewFlipper.showNext();
score++;
currentQ=quesList.get(qid);
Log.d("score", "Your score"+score);
setQuestionView();
}
else
{
Toast.makeText(MainActivity.this, "Sorry :( Try again", Toast.LENGTH_SHORT).show();
currentQ=quesList.get(qid);
mViewFlipper.showNext();
setQuestionView();
}
if(qid<6)
{
Intent intent = new Intent(MainActivity.this, ResultActivity.class);
Bundle b = new Bundle();
b.putInt("score", score); //Your score
intent.putExtras(b); //Put your score to your next Intent
startActivity(intent);
finish();
}
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
mGestureDetector.onTouchEvent(event);
return super.onTouchEvent(event);
}
class CustomGestureDetector extends GestureDetector.SimpleOnGestureListener {
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
// Swipe left (next)
if (e1.getX() > e2.getX()) {
mViewFlipper.showNext();
}
return super.onFling(e1, e2, velocityX, velocityY);
}
}
private void setQuestionView()
{
txtQuestion.setText(currentQ.getQUESTION());
rda.setText(currentQ.getOPTA());
rdb.setText(currentQ.getOPTB());
rdc.setText(currentQ.getOPTC());
rdd.setText(currentQ.getOPTD());
qid++;
}
}
我的DbHElper.java
public class DbHelper extends SQLiteOpenHelper
{
private static final int DATABASE_VERSION = 1;
// Database Name
private static final String DATABASE_NAME = "KidQuiz";
// 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 a
private static final String KEY_OPTB= "optb"; //option b
private static final String KEY_OPTC= "optc"; //option c
private static final String KEY_OPTD= "optd"; //option d
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,"+KEY_OPTD+" TEXT)";
db.execSQL(sql);
Question q1=new Question("What is this?",
"APPLE", "BALL", "CAT","BAT","APPLE");
this.addQuestion(q1);
Question q2=new Question(
"What is this?", "BALL", "CAT", "ELEPHANT","BAT","BALL");
this.addQuestion(q2);
Question q3=new Question(
" What is this?","CAT", "DOG","ELEPHANT","BAT","CAT");
this.addQuestion(q3);
Question q4=new Question(
" What is this?", "DOG", "ELEPHANT","BALL","BAT","DOG");
this.addQuestion(q4);
Question q5=new Question(
" What is this?","ELEPHANT","BAT","APPLE","CAT","ELEPHANT");
this.addQuestion(q5);
db.close();
}
@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 addQuestion(Question 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());
values.put(KEY_OPTD, quest.getOPTD());
// Inserting Row
dbase.insert(TABLE_QUEST, null, values);
}
public List<Question> getAllQuestions() {
List<Question> quesList = new ArrayList<Question>();
// Select All Query
String selectQuery = "SELECT * FROM " + TABLE_QUEST;
dbase=this.getReadableDatabase();
Cursor cursor = dbase.rawQuery(selectQuery, null);
// looping through all rows and adding to list
if (cursor.moveToFirst()) {
do {
Question quest = new Question();
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));
quest.setOPTD(cursor.getString(6));
quesList.add(quest);
} while (cursor.moveToNext());
}
// return quest list
return quesList;
}
public int rowcount()
{
int row=0;
String selectQuery = "SELECT * FROM " + TABLE_QUEST;
SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);
row=cursor.getCount();
return row;
}
}
Question.java
public class Question
{
private int ID;
private String QUESTION;
private String OPTA;
private String OPTB;
private String OPTC;
private String OPTD;
private String ANSWER;
public Question()
{
ID=0;
QUESTION="";
OPTA="";
OPTB="";
OPTC="";
OPTD="";
ANSWER="";
}
public Question(String qUESTION, String oPTA, String oPTB, String oPTC,String oPTD,
String aNSWER) {
QUESTION = qUESTION;
OPTA = oPTA;
OPTB = oPTB;
OPTC = oPTC;
OPTD = oPTD;
ANSWER = aNSWER;
}
public int getID()
{
return ID;
}
public String getQUESTION() {
return QUESTION;
}
public String getOPTA() {
return OPTA;
}
public String getOPTB() {
return OPTB;
}
public String getOPTC() {
return OPTC;
}
public String getOPTD() {
return OPTD;
}
public String getANSWER() {
return ANSWER;
}
public void setID(int id)
{
ID=id;
}
public void setQUESTION(String qUESTION) {
QUESTION = qUESTION;
}
public void setOPTA(String oPTA) {
OPTA = oPTA;
}
public void setOPTB(String oPTB) {
OPTB = oPTB;
}
public void setOPTC(String oPTC) {
OPTC = oPTC;
}
public void setOPTD(String oPTD) {
OPTD = oPTD;
}
public void setANSWER(String aNSWER) {
ANSWER = aNSWER;
}
}
最后是ResultActivity.java
public class ResultActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_result);
//get rating bar object
RatingBar bar=(RatingBar)findViewById(R.id.ratingBar1);
//get text view
TextView t=(TextView)findViewById(R.id.textResult);
//get score
Bundle b = getIntent().getExtras();
int score= b.getInt("score");
//display score
bar.setRating(score);
switch (score)
{
case 1:
case 2: t.setText("Oopsie! Better Luck Next Time!");
break;
case 3:
case 4:t.setText("Hmmmm.. Someone's been reading a lot of trivia");
break;
case 5:t.setText("Who are you? A trivia wizard???");
break;
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
}
答案 0 :(得分:0)
检查
的输出 Log.d("yourans", currentQ.getANSWER() + " " + answer.getText());
验证它是你认为的......