0

我做了一个测验,它使用 ListArray 通过 Question 对象生成问题。看起来像这样:

public class Glavno extends Activity implements OnClickListener {

int score  = 0;

TextView textView1, textView2, textView3, countdown;
Button btn1, btn2, btn3, btn4;

ArrayList<Question> qsts = new ArrayList<Question>();
List<Integer> generated = new ArrayList<Integer>();

ArrayList<String> allAnswers = new ArrayList<String>();

Random rng = new Random();
Question nextQuestion;

Question q1 = new Question(
    "Q1",

    "Correct answer - q1",
    "Wrong answer 1 - q1",
    "Wrong answer 2 - q1",
    "Wrong answer 3 - q1"
    );
Question q2 = new Question(
    "Q2?",

    "Correct answer - q2",
    "Wrong answer 1 - q2",
    "Wrong answer 2 - q2",
    "Wrong answer 3 - q2"
    );
Question q3 = new Question(
    "Q3?",

    "Correct answer - q3"
    "Wrong answer 1 - q3",
    "Wrong answer 2 - q3",
    "Wrong answer 3 - q3"
    );

Question q4 = new Question(
    "Q4?",

    "Correct answer - q4",
    "Wrong answer 1 - q4",
    "Wrong answer 2 - q4",
    "Wrong answer 3 - q4"
    );

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,   WindowManager.LayoutParams.FLAG_FULLSCREEN);

setContentView(R.layout.pitanja);

// ADD THE QUESTIONS IN THE ArrayList qsts

qsts.add(q1);           
qsts.add(q2);
qsts.add(q3);
qsts.add(q4);

textView1 = (TextView) findViewById(R.id.textView1);
textView2 = (TextView) findViewById(R.id.textView2);
textView3 = (TextView) findViewById(R.id.textView3);
countdown = (TextView) findViewById(R.id.countdown);

textView3.setText("Rezultat: " + score);

    btn1 = (Button) findViewById(R.id.btn1);
    btn2 = (Button) findViewById(R.id.btn2);
    btn3 = (Button) findViewById(R.id.btn3);
    btn4 = (Button) findViewById(R.id.btn4);

    btn1.setOnClickListener(this);
    btn2.setOnClickListener(this);
    btn3.setOnClickListener(this);
    btn4.setOnClickListener(this);

generateQuestion();

}

public void generateQuestion(){

    while(true){

        int nxt = rng.nextInt(4);

        if (!generated.contains(nxt)){

            generated.add(nxt);

            nextQuestion = qsts.get(nxt);

            textView1.setText(nextQuestion.questionText);

            allAnswers.add(nextQuestion.correctAnswerText);
            allAnswers.add(nextQuestion.wrongAnswer1);
            allAnswers.add(nextQuestion.wrongAnswer2);
            allAnswers.add(nextQuestion.wrongAnswer3);

            Collections.shuffle(allAnswers);

            btn1.setText(allAnswers.get(0));
            btn2.setText(allAnswers.get(1));
            btn3.setText(allAnswers.get(2));
            btn4.setText(allAnswers.get(3));

            break;
        }
    }
}


@Override
public void onClick(View v) {
    Button b = (Button)v;
    String buttonText = b.getText().toString();

    if(buttonText.equals(nextQuestion.correctAnswerText)) { 

        textView2.setText("TOČNO!");
        textView2.setTextColor(Color.GREEN);
        textView3.setText("Rezultat: " + (score += 10));

        allAnswers.clear();
        generateQuestion();

        return;

    }else{

        textView2.setText("NETOČNO!");
        textView2.setTextColor(Color.RED);
        textView3.setText("Rezultat: " + (score -= 5));

        allAnswers.clear();
        generateQuestion();

        return; 
    }

问题类:

package com.matej.hajdukkviz;

public class Question {

String questionText;
String correctAnswerText;       
String wrongAnswer1;
String wrongAnswer2;
String wrongAnswer3;

Question (String qst, String cAns, String wAns1, String wAns2, String wAns3){

    questionText = qst;
    correctAnswerText = cAns;
    wrongAnswer1 = wAns1;
    wrongAnswer2 = wAns2;
    wrongAnswer3 = wAns3;

}
}

现在我可以向这个 Question 类添加一个 ImageView 但是我怎样才能让一个出现在 Question 对象中..

4

2 回答 2

0

您可以在问题类中添加一个新字段,其中包含一个 url 或一个可绘制对象(或一个资源 ID,如果您将所有图像作为资源提供)。然后将该值加载到ImageViewpitanja中(将其添加到您还定义了 TextViews的布局中),它负责显示该可绘制对象。更改问题类不会以任何方式影响 ArrayList。


在布局中的某处定义 ImageView,您可能希望调整属性:

<ImageView
    android:id="@+id/someimageviewid"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />

在您的onCreate方法中,获取对 ImageView 的引用,就像对 TextViews 所做的那样:

public class Glavno extends Activity /* ... */ {

  /* ... */

  ImageView someImageViewVariable;

  /* ... */

  @Override
  public void onCreate(/* ... */) {
    /* ... */
    someImageViewVariable = (ImageView) findViewById(R.id.someimageviewid);
    /* ... */
  }

  /* ... */

  protected void loadQuestion(Question question) {
    /* ... */
    someImageViewVariable.setImageResource(question.getDrawableRessource());
    /* ... */
  }

   /* ... */
}

在您的问题课程中:

public class Question {

  /* ... */

  private final int drawableRessource;

  /* ... */

  public Question(/* ... */, int drawableRessource) {
    /* ... */
    this.drawableRessource = drawableRessource;
    /* ... */
  }

  /* ... */

  public int getDrawableRessource() {
    return drawableRessource;
  }
}

请注意,我确实使用了一些您没有使用的模式:在其中声明字段Question,因为final它们在类创建后不应更改(不可变实例)。此外,我没有使用公共字段,而是使用 get 方法,一旦您使用接口和类似的东西,它们就会派上用场。两者都不是功能本身所必需的,但会让您的生活更轻松。

于 2013-08-05T00:35:02.623 回答
0

我还没有测试过,但这应该可以工作:(你的drwable文件夹中需要image1、image2、image3、image4,你还应该在你的xml文件中添加一个名为imageView1的imageview)

public class Glavno extends Activity implements OnClickListener {

int score  = 0;

TextView textView1, textView2, textView3, countdown;
Button btn1, btn2, btn3, btn4;
ImageView imageView1;

ArrayList<Question> qsts = new ArrayList<Question>();
List<Integer> generated = new ArrayList<Integer>();

ArrayList<String> allAnswers = new ArrayList<String>();

Random rng = new Random();
Question nextQuestion;

Question q1 = new Question(
        "Q1",

        "Correct answer - q1",
        "Wrong answer 1 - q1",
        "Wrong answer 2 - q1",
        "Wrong answer 3 - q1",
        R.drawable.image1
        );
Question q2 = new Question(
        "Q2?",

        "Correct answer - q2",
        "Wrong answer 1 - q2",
        "Wrong answer 2 - q2",
        "Wrong answer 3 - q2",
        R.drawable.image2
        );
Question q3 = new Question(
        "Q3?",

        "Correct answer - q3"
        "Wrong answer 1 - q3",
        "Wrong answer 2 - q3",
        "Wrong answer 3 - q3",
        R.drawable.image3
        );

Question q4 = new Question(
        "Q4?",

        "Correct answer - q4",
        "Wrong answer 1 - q4",
        "Wrong answer 2 - q4",
        "Wrong answer 3 - q4",
        R.drawable.image4
        );

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,   WindowManager.LayoutParams.FLAG_FULLSCREEN);

    setContentView(R.layout.pitanja);

    // ADD THE QUESTIONS IN THE ArrayList qsts

    qsts.add(q1);           
    qsts.add(q2);
    qsts.add(q3);
    qsts.add(q4);

    textView1 = (TextView) findViewById(R.id.textView1);
    textView2 = (TextView) findViewById(R.id.textView2);
    textView3 = (TextView) findViewById(R.id.textView3);
    countdown = (TextView) findViewById(R.id.countdown);
    imageView1 = (ImageView)findViewById(R.id.imageView1);

    textView3.setText("Rezultat: " + score);

    btn1 = (Button) findViewById(R.id.btn1);
    btn2 = (Button) findViewById(R.id.btn2);
    btn3 = (Button) findViewById(R.id.btn3);
    btn4 = (Button) findViewById(R.id.btn4);

    btn1.setOnClickListener(this);
    btn2.setOnClickListener(this);
    btn3.setOnClickListener(this);
    btn4.setOnClickListener(this);

    generateQuestion();

}

public void generateQuestion(){

    while(true){

        int nxt = rng.nextInt(4);

        if (!generated.contains(nxt)){

            generated.add(nxt);

            nextQuestion = qsts.get(nxt);

            textView1.setText(nextQuestion.questionText);
            imageView1.setImageResource(nextQuestion.imageID);

            allAnswers.add(nextQuestion.correctAnswerText);
            allAnswers.add(nextQuestion.wrongAnswer1);
            allAnswers.add(nextQuestion.wrongAnswer2);
            allAnswers.add(nextQuestion.wrongAnswer3);

            Collections.shuffle(allAnswers);

            btn1.setText(allAnswers.get(0));
            btn2.setText(allAnswers.get(1));
            btn3.setText(allAnswers.get(2));
            btn4.setText(allAnswers.get(3));

            break;
        }
    }
}


@Override
public void onClick(View v) {
    Button b = (Button)v;
    String buttonText = b.getText().toString();

    if(buttonText.equals(nextQuestion.correctAnswerText)) { 

        textView2.setText("TOČNO!");
        textView2.setTextColor(Color.GREEN);
        textView3.setText("Rezultat: " + (score += 10));

        allAnswers.clear();
        generateQuestion();

        return;

    }else{

        textView2.setText("NETOČNO!");
        textView2.setTextColor(Color.RED);
        textView3.setText("Rezultat: " + (score -= 5));

        allAnswers.clear();
        generateQuestion();

        return; 
    }

    allAnswers.clear();
    generateQuestion();

    return; 
}

问题类:

package com.matej.hajdukkviz;

public class Question {

String questionText;
String correctAnswerText;       
String wrongAnswer1;
String wrongAnswer2;
String wrongAnswer3;
int imageID;

Question (String qst, String cAns, String wAns1, String wAns2, String wAns3, int img){

    questionText = qst;
    correctAnswerText = cAns;
    wrongAnswer1 = wAns1;
    wrongAnswer2 = wAns2;
    wrongAnswer3 = wAns3;
    imageID = img;

}
}
于 2013-08-05T00:52:45.730 回答