如何根据我的测验分数启用和禁用listview项目?

时间:2013-12-20 02:23:11

标签: android android-listview

我的列表视图项:Easy Medium Hard。 我怎么去禁用中等硬度? 。以及如何在之后启用它 在“简单”测验中达到一定分数。请帮帮我们。 :(

这是我的代码。

    String classes[] = {"Easy", "Medium", "Hard" };


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setListAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, classes));

    }
    @Override
    protected void onListItemClick(ListView l, View v, int position, long id) {
        // TODO Auto-generated method stub
        super.onListItemClick(l, v, position, id);
        String easy = classes[position];
        try{
        Class openClass = Class.forName("first.project." + easy);
        Intent openIntent = new Intent(this , openClass);
        startActivity(openIntent);
        }
        catch (ClassNotFoundException e){
            e.printStackTrace();
        }

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

这是我的QuestionActivity上的代码

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

    EditText question = null;
    RadioButton answer1 = null;
    RadioButton answer2 = null;
    RadioButton answer3 = null;
    RadioButton answer4 = null;
    RadioGroup answers = null;
    Button finish = null;
    int selectedAnswer = -1;
    int quesIndex = 0;
    int numEvents = 0;
    int selected[] = null;
    int correctAns[] = null;
    boolean review =false;
    Button prev, next = null;

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

        TableLayout quizLayout = (TableLayout)  findViewById(R.id.quizLayout);
        quizLayout.setVisibility(android.view.View.INVISIBLE);

        try {
            question = (EditText) findViewById(R.id.question);
            answer1 = (RadioButton) findViewById(R.id.a0);
            answer2 = (RadioButton) findViewById(R.id.a1);
            answer3 = (RadioButton) findViewById(R.id.a2);
            answer4 = (RadioButton) findViewById(R.id.a3);
            answers = (RadioGroup) findViewById(R.id.answers);
            RadioGroup questionLayout = (RadioGroup)findViewById(R.id.answers);
            Button finish = (Button)findViewById(R.id.finish);
            finish.setOnClickListener(finishListener);

            prev = (Button)findViewById(R.id.Prev);
            prev.setOnClickListener(prevListener);
            next = (Button)findViewById(R.id.Next);
            next.setOnClickListener(nextListener);


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


            this.showQuestion(0,review);

            quizLayout.setVisibility(android.view.View.VISIBLE);

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

    }


    private void showQuestion(int qIndex,boolean review) {
        try {
            JSONObject aQues = Easy.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.WHITE);
            answer2.setTextColor(Color.WHITE);
            answer3.setTextColor(Color.WHITE);
            answer4.setTextColor(Color.WHITE);
            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());
            aAns = ansList.getJSONObject(3).getString("Answer");
            answer4.setText(aAns.toCharArray(), 0, aAns.length());
            Log.d("",selected[qIndex]+"");
            if (selected[qIndex] == 0)
                answers.check(R.id.a0);
            if (selected[qIndex] == 1)
                answers.check(R.id.a1);
            if (selected[qIndex] == 2)
                answers.check(R.id.a2);
            if (selected[qIndex] == 3)
                answers.check(R.id.a3);

            setScoreTitle();
            if (quesIndex == (Easy.getQuesList().length()-1))
                next.setEnabled(false);

            if (quesIndex == 0)
                prev.setEnabled(false);

            if (quesIndex > 0)
                prev.setEnabled(true);

            if (quesIndex < (Easy.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 (selected[qIndex] == 3)
                        answer4.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);
                if (correctAns[qIndex] == 3)
                    answer4.setTextColor(Color.GREEN);
            }
        } catch (Exception e) {
            Log.e(this.getClass().toString(), e.getMessage(), e.getCause());
        }
    }


    private OnClickListener finishListener = new OnClickListener() {
        public void onClick(View v) {
            setAnswer();
            //Calculate Score
            int score = 0;
            for(int i=0; i<correctAns.length; i++){
                if ((correctAns[i] != -1) && (correctAns[i] == selected[i]))
                    score++;
            }
            AlertDialog alertDialog;
            alertDialog = new AlertDialog.Builder(QuestionActivity.this).create();
            alertDialog.setTitle("Score");
            alertDialog.setMessage((score) +" out of " + (Easy.getQuesList().length()));

            alertDialog.setButton(AlertDialog.BUTTON_NEUTRAL, "Retake", new DialogInterface.OnClickListener(){

                public void onClick(DialogInterface dialog, int which) {
                    review = false;
                    quesIndex=0;
                    QuestionActivity.this.showQuestion(0, review);
                }
            });

            alertDialog.setButton(AlertDialog.BUTTON_POSITIVE, "Review", new DialogInterface.OnClickListener(){

                public void onClick(DialogInterface dialog, int which) {
                    review = true;
                    quesIndex=0;
                    QuestionActivity.this.showQuestion(0, review);
                }
            });

            alertDialog.setButton(AlertDialog.BUTTON_NEGATIVE,"Quit", new DialogInterface.OnClickListener(){

                public void onClick(DialogInterface dialog, int which) {
                    review = false;
                    finish();
                }
            });

            alertDialog.show();

        }
    };

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

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

    }

    private OnClickListener nextListener = new OnClickListener() {
        public void onClick(View v) {
            setAnswer();
            quesIndex++;
            if (quesIndex >= Easy.getQuesList().length())
                quesIndex = Easy.getQuesList().length() - 1;

            showQuestion(quesIndex,review);
        }
    };

        private OnClickListener prevListener = new OnClickListener() {
            public void onClick(View v) {
                setAnswer();
                quesIndex--;
                if (quesIndex < 0)
                    quesIndex = 0;

                showQuestion(quesIndex,review);
                }
        };

        private void setScoreTitle() {
            this.setTitle("Question # " + (quesIndex+1)+ "/" + Easy.getQuesList().length());
        }


        @Override
         public void onBackPressed() {

                new AlertDialog.Builder(this)
                .setTitle("Warning!!")
                .setMessage("Are you sure you want to exit?")
                .setCancelable(false)
                .setPositiveButton("Yes", new DialogInterface.OnClickListener() 
                   {
                       public void onClick(DialogInterface dialog, int which) {
                    finish();
                       }
                   })
                   .setNegativeButton("No", null)
                   .show();


               }
               }

2 个答案:

答案 0 :(得分:0)

使用如下循环:

int count = adapter.getCount();
for (int i = 0; i < count; i++) {
 adapter.remove(adapter.getItem(i));
}

我认为应该有效

XcutionX

答案 1 :(得分:0)

这就是我的理解,你要做的是:

首先,'easy'按钮将被启用,其余两个将被禁用(我猜)。

buttonEasy.setEnabled(true);
buttonMediun.setEnabled(false);
buttonHard.setEnabled(false);

然后在完成“简单”等级的测验后,检查分数是否已达到某个值。如果是,那么您将启用带有代码的“中”按钮:

buttonMedium.setEnabled(true);