如何计算问题的数量?

时间:2013-03-02 02:41:51

标签: java android android-sqlite

我有这个问题的活动和4个可能的答案,并且在每个用户回答之后,它会重新加载另一个问题。我在右下角放置了一个textView,其起始值为1/10rezultat.setText(counter+"/10");)。我希望在每个问题之后将该值增加到2 / 10,3 / 10等等,但我不知道在哪里放置计数器++;在我的代码中,我到处尝试。这是我的代码:

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Typeface;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.TextView;

public class Kviz extends Activity implements View.OnClickListener{

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

    Button bIzlazIzKviza, bOdgovor1, bOdgovor2, bOdgovor3, bOdgovor4;
    TextView question, proba, rezultat;
    int counter = 1;

    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) {
                mHandler.postDelayed(mLaunchTaskFinish, 1200);
                mHandler.postDelayed(mLaunchTask,1000);
                Intent i = new Intent("rs.androidaplikacijekvizopstekulture.TACANODGOVOR");
                startActivity(i);

            }else{
                mHandler.postDelayed(mLaunchTaskFinish, 2200);
                mHandler.postDelayed(mLaunchTask,2000);
                Intent i = new Intent("rs.androidaplikacijekvizopstekulture.POGRESANODGOVOR");
                startActivity(i);
                }
            }
     };

    @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.kviz);

        Typeface dugmad = Typeface.createFromAsset(getAssets(), "Bebas.ttf");
        Typeface pitanje = Typeface.createFromAsset(getAssets(), "Lobster.ttf");
        bIzlazIzKviza = (Button) findViewById(R.id.bIzlazIzKviza);
        rezultat = (TextView) findViewById(R.id.tvBrojPitanja);
        question = (TextView) findViewById(R.id.tvPitanje);
        bOdgovor1 = (Button) findViewById(R.id.bOdgovor1);
        bOdgovor2 = (Button) findViewById(R.id.bOdgovor2);
        bOdgovor3 = (Button) findViewById(R.id.bOdgovor3);
        bOdgovor4 = (Button) findViewById(R.id.bOdgovor4);
        bOdgovor1.setTypeface(dugmad);
        bOdgovor2.setTypeface(dugmad);
        bOdgovor3.setTypeface(dugmad);
        bOdgovor4.setTypeface(dugmad);
        bIzlazIzKviza.setTypeface(dugmad);
        rezultat.setTypeface(dugmad);
        question.setTypeface(pitanje);

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


        try{    //Pokusava da otvori db
        mDbHelper.open();  //baza otvorena

        Cursor c = mDbHelper.getTestData();

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

        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));

        Collections.shuffle(labels);

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

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

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

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

        rezultat.setText(counter+"/10");
        }

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

        bIzlazIzKviza.setOnClickListener(new OnClickListener() {

            public void onClick(View v) {
                finish();   
            }
        });
    }


    @Override
    protected void onPause() {
        // TODO Auto-generated method stub
        super.onPause();
    }

    public void onClick(View v) {

    }
}

3 个答案:

答案 0 :(得分:1)

“每个用户回答它是否会重新加载另一个问题”的意思是什么。 ?您是否对diff问题使用不同的活动?
您可以对所有问题使用相同的活动,当用户按任何答案的按钮时,只需调用方法

public void nextQuestion() {
    counter++;
    question.setText("");
    bOdgovor1.setText("");
    bOdgovor2.setText("");
    bOdgovor3.setText("");
    bOdgovor4.setText("");
    //reset your next question and all four options here
    rezultat.setText(counter + "/10");
}

答案 1 :(得分:0)

这里很难测试,但看起来像是在行

之前添加counter++
Answer ans = (Answer) v.getTag();

应该正常工作,因为只要点击了似乎负责回答问题的按钮,就会将counter设置为递增。请记住,您还需要致电

rezultat.setText(counter+"/10");

同样,要更新文本以反映counter的新值。

答案 2 :(得分:0)

稍微扩展我的问题。我确实设法用计数器类计算问题,它是2/10,但因为我在每个问题后重新加载我的活动,我的textView也重新加载到1/10。我认为这是我问题的主要原因。