-4

我正在开发一个测验,其中我存储了 10 个问题。它是随机显示的,我的问题是在那之后我的第 10 个问题我不知道如何将它用于我的分数等级,哪个分数必须显示最近玩测验的玩家。

这是加载问题的地方:

public class Question1 extends Activity {



Intent menu = null;
BufferedReader bReader = null;
static JSONArray quesList = null;
static int index = 50;



/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.question10);

    Thread thread = new Thread() {
        public void run() {
            try {
                Thread.sleep(1 * 1000);
                finish();
                loadQuestions();
                Intent intent = new Intent(Question1.this,
                        Question2.class);
                Question1.this.startActivity(intent);
            } catch (Exception e) {
            }
        }
    };
    thread.start();

}

//List<JSONObject> question = null;

private void loadQuestions() throws Exception {
    try {


        InputStream questions = this.getBaseContext().getResources()
                .openRawResource(R.raw.questions);
        bReader = new BufferedReader(new InputStreamReader(questions));
        StringBuilder quesString = new StringBuilder();
        String aJsonLine = null;
        while ((aJsonLine = bReader.readLine()) != null) {
            quesString.append(aJsonLine);
        }

        Log.d(this.getClass().toString(), quesString.toString());
        JSONObject quesObj = new JSONObject(quesString.toString());
        quesList = quesObj.getJSONArray("Questions");
        Log.d(this.getClass().getName(),
                "Num Questions " + quesList.length());

        /*  question = new ArrayList<JSONObject>();
             int n = Math.min(10, quesList.length());
             for(int i = 0; i < n; i++) {
                 JSONObject questions1 = quesList.getJSONObject(i);
                 question.add(questions1);*/




    } catch (Exception e) {

    } finally {
        try {
            bReader.close();
        } catch (Exception e) {
            Log.e("", e.getMessage().toString(), e.getCause());
        }

    }

}

public static JSONArray getQuesList()throws JSONException{

      Random rnd = new Random();

        for (int i = quesList.length() - 1; i >= 0; i--)
        {
          int j = rnd.nextInt(i + 1);
          // Simple swap
          Object object = quesList.get(j);
          quesList.put(j, quesList.get(i));
          quesList.put(i, object);
        }
        return quesList;


}

这是问题所在:

public class Question2 extends Activity {
/** Called when the activity is first created. */


TextView question, items = null;
RadioButton answer1 = null;
RadioButton answer2 = null;
RadioButton answer3 = null;
RadioGroup answers = null;
int selectedAnswer = -1;
int quesIndex = 0;
int numEvents = 0;
int selected[] = null;
int correctAns[] = null;
boolean review = false;
Button next = null;
int score = 0;


@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.startquiz);

    try {
        score = getIntent().getIntExtra("score",0);
        items = (TextView)findViewById(R.id.displayitems);
        question = (TextView) findViewById(R.id.displayquestion);
        answer1 = (RadioButton) findViewById(R.id.option1);
        answer2 = (RadioButton) findViewById(R.id.option2);
        answer3 = (RadioButton) findViewById(R.id.option3);
        answers = (RadioGroup) findViewById(R.id.QueGroup1);


        next = (Button) findViewById(R.id.selected);
        next.setOnClickListener(nextListener);

        selected = new int[Question1.getQuesList().length()];
        java.util.Arrays.fill(selected, -1);
        correctAns = new int[Question1.getQuesList().length()];
        java.util.Arrays.fill(correctAns, -1);

        this.showQuestion(0, review);



    } catch (Exception e) {
        Log.e("", e.getMessage().toString(), e.getCause());
    }

}



private void showQuestion(int qIndex, boolean review) {
    try {
        JSONObject aQues = Question1.getQuesList().getJSONObject(
                qIndex);
        String quesValue = aQues.getString("Question");
        if (correctAns[qIndex] == -1) {
            String correctAnsStr = aQues.getString("CorrectAnswer");
            correctAns[qIndex] = Integer.parseInt(correctAnsStr);
        }

        question.setText(quesValue.toCharArray(), 0, quesValue.length());
        answers.check(-1);
        answer1.setTextColor(Color.BLACK);
        answer2.setTextColor(Color.BLACK);
        answer3.setTextColor(Color.BLACK);
        JSONArray ansList = aQues.getJSONArray("Answers");
        String aAns = ansList.getJSONObject(0).getString("Answer");
        answer1.setText(aAns.toCharArray(), 0, aAns.length());
        aAns = ansList.getJSONObject(1).getString("Answer");
        answer2.setText(aAns.toCharArray(), 0, aAns.length());
        aAns = ansList.getJSONObject(2).getString("Answer");
        answer3.setText(aAns.toCharArray(), 0, aAns.length());
        Log.d("", selected[qIndex] + "");
        if (selected[qIndex] == 0)
            answers.check(R.id.option1);
        if (selected[qIndex] == 1)
            answers.check(R.id.option2);
        if (selected[qIndex] == 2)
            answers.check(R.id.option3);

        setText();
        if (quesIndex == (Question1.getQuesList().length() - 1))
            next.setEnabled(false);

        if (quesIndex < (Question1.getQuesList().length() - 1))
            next.setEnabled(true);

        if (review) {
            Log.d("review", selected[qIndex] + "" + correctAns[qIndex]);

            if (selected[qIndex] != correctAns[qIndex]) {
                if (selected[qIndex] == 0)
                    answer1.setTextColor(Color.RED);
                if (selected[qIndex] == 1)
                    answer2.setTextColor(Color.RED);
                if (selected[qIndex] == 2)
                    answer3.setTextColor(Color.RED);
            }
            if (correctAns[qIndex] == 0)
                answer1.setTextColor(Color.GREEN);
            if (correctAns[qIndex] == 1)
                answer2.setTextColor(Color.GREEN);
            if (correctAns[qIndex] == 2)
                answer3.setTextColor(Color.GREEN);
        }
    } catch (Exception e) {
        Log.e(this.getClass().toString(), e.getMessage(), e.getCause());
    }
}


private void setAnswer() {
    if (answer1.isChecked())
        selected[quesIndex] = 0;
    if (answer2.isChecked())
        selected[quesIndex] = 1;
    if (answer3.isChecked())
        selected[quesIndex] = 2;



    Log.d("", Arrays.toString(selected));
    Log.d("", Arrays.toString(correctAns));

}

private OnClickListener nextListener = new OnClickListener() {


    public void onClick(View v) {
        int i = correctAns.length;

        if (v == next){
            if (correctAns[i] == selected[i])
                    {

                        score++;
                        Toast.makeText(getApplicationContext(), "Your answer is correct!", Toast.LENGTH_SHORT).show();
                    }else
                    {
                    Toast.makeText(getApplicationContext(), "Your answer is wrong..." + correctAns, Toast.LENGTH_SHORT).show();
                    }
        }

        quesIndex++;
        try {
            if (quesIndex >= Question1.getQuesList().length())
                quesIndex = Question1.getQuesList().length() - 1;


        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        showQuestion(quesIndex, review);

    }
};
private void setText() throws JSONException {
    this.setTitle("Question " + (quesIndex + 1) + "  out of  "
            + Question1.getQuesList().length());
    items.setGravity(250);
}

public void reload() {
    setAnswer();



Intent intent = getIntent();
overridePendingTransition(0, 0);
intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
finish();

overridePendingTransition(0, 0);
startActivity(intent);
}
4

1 回答 1

0

我无法理解您为什么要在 Question1.onCreate() 中运行单独的线程。如果要在后台加载问题,请使用 AsyncTask。就个人而言,我更喜欢使用 IntentService 来执行您希望保留结果的后台任务。IntentService 对于解码 JSON 特别有用。

您没有说您的应用程序现在发生了什么,但我的猜测是 Activity Question2 永远不会启动。我不确定,但我怀疑这是因为您试图从后台线程启动 Activity。您可以使用 runOnUiThread() 来解决此问题,但最好将问题加载到IntentService中,在加载完成后向您的 Activity 发送本地 BroadcastMessage,然后显示结果。此外,我会将 JSON 解析为数据库或内容提供程序。

于 2013-02-22T23:16:43.650 回答