how can i solve this logical error???What is error with the codes?

Kaushik Acharya

i am creating a image quiz app....with a image and 4 radio buttons.the image is added through ViewFlipper and the options and answer are taken from database. the app is working but i am unable to load result page and cannot get the toast "well done :)" even if the answer is correct.i am unable to track down whats the problem please help me out

this is my 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++;
}

}

my 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;
}
}

and finally 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;
}
}
Tim S

Check the output of

Log.d("yourans", currentQ.getANSWER() + " " + answer.getText());

to verify it is what you think it is...

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How can I solve this error in Flash game?

From Dev

How can i solve this java lang error?

From Dev

How can i solve this type of mysql error

From Dev

how can i solve this toString() error?

From Dev

How can I solve ACPI error?

From Dev

How can I solve "Runtime Error" on OJ

From Dev

How to solve the logical expression?

From Dev

How can I solve HTTP "404" and "405" error msgs?

From Java

How can I solve "ReferenceError: expect is not defined" error message?

From Java

How can I solve my connection error in mysql

From Java

Firebase Cloud Function - How can I solve this error: ECONNRESET

From Dev

How can I solve <ActiveRecord::Associations::CollectionProxy []> error?

From Dev

How can I solve ORA-00911: invalid character error?

From Dev

How can i solve `mix content type error` in googleMap

From Dev

How can I solve the `--port` error in meteor js

From Dev

How can I solve this "System.Data.Entity.DynamicProxies" error

From Dev

mongodb error. how can I solve the erro in mongoDB?

From Dev

Java: How can i solve the Error: "implicit super constructor is undefined"

From Dev

error while building signed apk how can i solve this?

From Dev

How can I solve this Java Jdbc connection Error?

From Dev

how can i solve "getSystemservices(String) " error in android application

From Dev

how can i solve java.lang.ArrayIndexOutOfBoundsException error

From Dev

How can I solve <ActiveRecord::Associations::CollectionProxy []> error?

From Dev

How can I solve the MongoDB Overflow sort stage error?

From Dev

How can I solve "Uncaught Error: Container is not defined"?

From Dev

How can I solve this error related to Supersonic SDK?

From Dev

How can i solve `mix content type error` in googleMap

From Dev

How can i solve a Math error between decimal and double?

From Dev

How can i made my app run and solve this error

Related Related

  1. 1

    How can I solve this error in Flash game?

  2. 2

    How can i solve this java lang error?

  3. 3

    How can i solve this type of mysql error

  4. 4

    how can i solve this toString() error?

  5. 5

    How can I solve ACPI error?

  6. 6

    How can I solve "Runtime Error" on OJ

  7. 7

    How to solve the logical expression?

  8. 8

    How can I solve HTTP "404" and "405" error msgs?

  9. 9

    How can I solve "ReferenceError: expect is not defined" error message?

  10. 10

    How can I solve my connection error in mysql

  11. 11

    Firebase Cloud Function - How can I solve this error: ECONNRESET

  12. 12

    How can I solve <ActiveRecord::Associations::CollectionProxy []> error?

  13. 13

    How can I solve ORA-00911: invalid character error?

  14. 14

    How can i solve `mix content type error` in googleMap

  15. 15

    How can I solve the `--port` error in meteor js

  16. 16

    How can I solve this "System.Data.Entity.DynamicProxies" error

  17. 17

    mongodb error. how can I solve the erro in mongoDB?

  18. 18

    Java: How can i solve the Error: "implicit super constructor is undefined"

  19. 19

    error while building signed apk how can i solve this?

  20. 20

    How can I solve this Java Jdbc connection Error?

  21. 21

    how can i solve "getSystemservices(String) " error in android application

  22. 22

    how can i solve java.lang.ArrayIndexOutOfBoundsException error

  23. 23

    How can I solve <ActiveRecord::Associations::CollectionProxy []> error?

  24. 24

    How can I solve the MongoDB Overflow sort stage error?

  25. 25

    How can I solve "Uncaught Error: Container is not defined"?

  26. 26

    How can I solve this error related to Supersonic SDK?

  27. 27

    How can i solve `mix content type error` in googleMap

  28. 28

    How can i solve a Math error between decimal and double?

  29. 29

    How can i made my app run and solve this error

HotTag

Archive