0

I have 36 imageButtons and with each one I call the same popup in which I set random question from sqlite database. When user answers question by clicking one of the 4 possible solutions it closes and goes back to my 36 buttons. Now here I have 2 problems:

  1. I use this code to avoid repeating questions. In activity scope:

    LinkedList mAnsweredQuestions = new LinkedList();

    private String generateWhereClause(){
        StringBuilder result = new StringBuilder();
        for (Long l : mAnsweredQuestions){
            result.append(" AND _ID <> " + l);
        }
        return result.toString();
    }
    

    and in my question method:

    mAnsweredQuestions.add(c.getLong(0));

But this does not work when my activity resumes after popup question close. It's like it loads my activity from scratch. I use this in my other game and it works fine in "normal" activity, but for some reason after popup it does not.

2.I need for each imageButton press to launch this popup activity and after each correct answer to save different number of points. I used startActivityForResult in my other game to start activity and save result, but in that case I was starting different activities with each button. Now I have to start the same activity, and save back different results. How can I do that? Here's my popup question class:

public class Pitanja_Cigle extends Activity{

    public static String tacanOdg;
    int counter = 0;

    Button b1, b2, b3, b4;
    TextView question;


LinkedList<Long> mAnsweredQuestions = new LinkedList<Long>();

    private String generateWhereClause(){
        StringBuilder result = new StringBuilder();
        for (Long l : mAnsweredQuestions){
            result.append(" AND _ID <> " + l);
        }
        return result.toString();
    }

    Runnable mLaunchTaskFinish = new Runnable() {
        public void run() {
            finish();
        }
     };

    private class Answer {
        public Answer(String opt, boolean correct) {
            option = opt;
            isCorrect = correct;
        }

        String option;
        boolean isCorrect;
    }
    Handler mHandler = new Handler();

    final OnClickListener clickListener = new OnClickListener() {
        public void onClick(View v) {
            Answer ans = (Answer) v.getTag();
            if (ans.isCorrect) {
                Intent i = new Intent("rs.androidaplikacije.toplo_hladno.TACANODGOVOR");
                startActivity(i);
                mHandler.postDelayed(mLaunchTaskFinish,1200);
            }
            else{   
                Intent i = new Intent(getApplicationContext(), PogresanOdgovor.class);
                i.putExtra("tacanOdgovor", tacanOdg);
                startActivity(i);
                mHandler.postDelayed(mLaunchTaskFinish,2200);
            }
        }
     };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        requestWindowFeature(Window.FEATURE_NO_TITLE);   //full screen
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);

        setContentView(R.layout.pitanja_cigle);

        InicirajVariable();

        nextQuestion();
    }

    private void nextQuestion() {
        counter++;

        TestAdapter mDbHelper = new TestAdapter(this);
        mDbHelper.createDatabase();

        try{    //Pokusava da otvori db

            mDbHelper.open();  //baza otvorena

            Cursor c = mDbHelper.getPitanjaCigle(generateWhereClause());

            mAnsweredQuestions.add(c.getLong(0));

            List<Answer> labels = new ArrayList<Answer>();

            labels.add(new Answer(c.getString(2), true));
            labels.add(new Answer(c.getString(3), false));
            labels.add(new Answer(c.getString(4), false));
            labels.add(new Answer(c.getString(5), false));

            tacanOdg = c.getString(2);

            Collections.shuffle(labels);

            question.setText(c.getString(1));

            b1.setText(labels.get(0).option);
            b1.setTag(labels.get(0));
            b1.setOnClickListener(clickListener);

            b2.setText(labels.get(1).option);
            b2.setTag(labels.get(1));
            b2.setOnClickListener(clickListener);

            b3.setText(labels.get(2).option);
            b3.setTag(labels.get(2));
            b3.setOnClickListener(clickListener);

            b4.setText(labels.get(3).option);
            b4.setTag(labels.get(3));
            b4.setOnClickListener(clickListener);

    }

    finally{    // kada zavrsi sa koriscenjem baze podataka, zatvara db
        mDbHelper.close();
    }
    }

    private void InicirajVariable() {
        Typeface naslov = Typeface.createFromAsset(getAssets(), "Lobster.ttf");
        Typeface dugmad = Typeface.createFromAsset(getAssets(), "Bebas.ttf");

        question = (TextView) findViewById(R.id.tvPitanjeCigle);
        b1 = (Button) findViewById(R.id.bOdgCigle1);
        b2 = (Button) findViewById(R.id.bOdgCigle2);
        b3 = (Button) findViewById(R.id.bOdgCigle3);
        b4 = (Button) findViewById(R.id.bOdgCigle4);

        b1.setTypeface(dugmad);
        b2.setTypeface(dugmad);
        b3.setTypeface(dugmad);
        b4.setTypeface(dugmad);
        question.setTypeface(naslov);

    }

}
marjanbaz
  • 1,052
  • 3
  • 18
  • 35
  • I think your design is not that good. You shouldn't query the database every time - instead, you can load your questions on start-up and then simply mark them as "used". Also, you should read up on managing the Activity lifecycle and saving the instance state. – Shade Apr 08 '13 at 13:26
  • @Shade Did you mean to use only mAnsweredQuestions.add(c.getLong(0)); in my nextQuestion method and the rest db stuff in activity scope? I actually tried that but I get error on mDbHelper.createDatabase();. Can you give an example how would you do it? – marjanbaz Apr 08 '13 at 13:52
  • Sure. See here - http://paste.ubuntu.com/5689635/ – Shade Apr 08 '13 at 15:04
  • Thanks i'll have a look and keep you posted. – marjanbaz Apr 08 '13 at 15:10
  • I actually don't get any of that code. :) Sorry mate, i'm pretty new in Java waters, so your code is a big step for me right now. Thanks anyway for your trouble. Can you tell me this instead: why in my other game (same stuff, question and 4 answers, but not the popup dialog, just regular activity that reloads question after each answer) I use my code for remembering asked questions, and that code does not work here? [Here](https://github.com/Nabukodonosor/spojnice/blob/master/Kviz.java) you can see that code. – marjanbaz Apr 08 '13 at 15:18
  • Because your Activity gets paused and (later) recreated. You can control this process and avoid the problem, but you'll have to read up on the matter (it's not complex, but it's quite long to explain in a few words) - http://developer.android.com/training/basics/activity-lifecycle/index.html and this in particular - http://developer.android.com/training/basics/activity-lifecycle/pausing.html – Shade Apr 08 '13 at 15:21
  • Yes, I saw that link and I will start on reading that right away. Thanks alot good man. :) Can you take a look at [this](http://stackoverflow.com/questions/15851899/how-to-disable-button-in-another-activity) and [this](http://stackoverflow.com/questions/15754577/how-to-disable-interaction-between-buttons-of-the-same-layout) while in that good mood? :) I'm having trouble with this and no solution. – marjanbaz Apr 08 '13 at 15:25
  • Sure, but I'm leaving work in a few minutes. Will take a look tomorrow. – Shade Apr 08 '13 at 15:27
  • OK, thank. Don't forget me. :) – marjanbaz Apr 08 '13 at 15:47
  • I've managed to reload activity with new questions without repetition, using startActivityForResult, but I can't send anything using i.putExtra. – marjanbaz Apr 08 '13 at 21:29
  • @Shade Did you have a chance to take a look at that stuff from yesterday? – marjanbaz Apr 09 '13 at 12:10

0 Answers0