我正在尝试为Android做一个简单的测验,
我被告知我应该尝试使用对象引用。
我不太清楚怎么做,这是我到目前为止所做的......
package com.aleksei.etb;
public class ETBetaActivity extends Activity implements View.OnClickListener {
private String[] questions =
{"Q1",
"Q2",
"Q3",
"Q4",
"Q5", //5
"Q6",
"Q7",
"Q8",
"Q9",
"Q10" //10
};
private String[] answers_correct =
{"Correct answer - 1",
"Correct answer - 2",
"Correct answer - 3",
"Correct answer - 4",
"Correct answer - 5",
"Correct answer - 6",
"Correct answer - 7",
"Correct answer - 8",
"Correct answer - 9",
"Correct answer - 10"
};
private String[][] answers_wrong =
{ {"Q1-1", "Q1-2" , "Q1-3"},
{"Q2-1", "Q2-2" , "Q2-3"},
{"Q3-1", "Q3-2" , "Q3-3"},
{"Q4-1", "Q4-2" , "Q4-3"},
{"Q5-1", "Q5-2" , "Q5-3"},
{"Q6-1", "Q6-2" , "Q6-3"},
{"Q7-1", "Q7-2" , "Q7-3"},
{"Q8-1", "Q8-2" , "Q8-3"},
{"Q9-1", "Q9-2" , "Q9-3"},
{"Q10-1", "Q10-2" , "Q10-3"}
};
private int counter = 0;
public Button answer_1,
answer_2,answer_3,
answer_4,skip;
public TextView question_tv,
tip_tv;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
getData();
quizBegin();
}
public static int getResults(){
return 5;
}
@Override
public void onClick(View view) {
switch(view.getId()){
case R.id.button5: //main
break;
case R.id.button1: //answer_1
break;
case R.id.button2: //answer_2
break;
case R.id.button3: //answer_3
break;
case R.id.button4: //answer_3
break;
}
}
private void getData(){
//Everything
skip = (Button) findViewById(R.id.button5);
answer_1 = (Button) findViewById(R.id.button1);
answer_2 = (Button) findViewById(R.id.button2);
answer_3 = (Button) findViewById(R.id.button3);
answer_4 = (Button) findViewById(R.id.button4);
question_tv = (TextView) findViewById(R.id.question);
tip_tv = (TextView) findViewById(R.id.answ1);
//Buttons
skip.setOnClickListener(this);
answer_1.setOnClickListener(this);
answer_2.setOnClickListener(this);
answer_3.setOnClickListener(this);
answer_4.setOnClickListener(this);
//Text
skip.setText("Begin!");
answer_4.setText("");
answer_3.setText("");
answer_2.setText("");
answer_1.setText("");
tip_tv.setText("");
}
private void quizBegin(){
String question = Question.(questions[counter], answers_correct[counter],
answers_wrong[counter],counter); //error here.
//What am I suppose to do??
// How to use an object reference??
// Help please!
question_tv.setText("");
counter++;
}
}
问题类,它封装了一个问题及其正确和错误的答案。
public class Question {
private String question;
private String correctAnswer;
private List<String> incorrectAnswers;
private int counter = 0;
public Question(final String question, final String correctAnswer,
final String[] incorrectAnswers, int counter) {
// TODO: empty strings/nulls checks
this.question = question;
this.correctAnswer = correctAnswer;
this.incorrectAnswers = Arrays.asList(incorrectAnswers);
}
public List<String> getPossibleAnswers() {
final List<String> result = new ArrayList<String>();
result.addAll(incorrectAnswers);
result.add(correctAnswer);
Collections.shuffle(result);
return result;
}
public boolean isCorrectAnswer(final String answer) {
if (answer.equals(correctAnswer)) {
return true;
}
return false;
}
public String getQuestion() {
return question;
}
}
答案 0 :(得分:2)
我只能猜测你的实际问题,但为什么你使用字符串数组来表示问题,答案正确和错误答案?为什么不使用Question
个对象数组,如果正确或错误地回答了Question
对象,请让它管理。
修改强>
语句new Question(questions[counter], answers_correct[counter], answers_wrong[counter],counter);
调用Question
类中的构造函数,该构造函数采用给定的参数的类型,顺序和数量来创建对象的instance
。
Question q = new Question();
创建一个新的Question
对象实例并分配对变量q
的引用,该变量可用于从其引用的Question
类的isntance中访问信息。这是面向对象编程的基本概念。
第二次编辑
此行适用于您:
String [] sl =(String [])q_class.getPossibleAnswers()。toArray(new String [0]);
但是对String数组的强制转换((String[])
)是没有意义的。方法toArray(T[])
使用generics并返回一个与传递给它的数组类型相同的数组。因此,将new String[0]
传递给该方法可确保您获得String[]
。因此,您要将String[]
投射到String[]
。
你真的想在第一时间这样做吗?为什么不能使用List<String>
代替数组?
如果你要从一个类型到另一个类型进行大量的转换,你应该问自己是否可以调整你的代码来操作单一类型。
(旁注,您可以使用反引号(`)来表示内联代码部分,这有助于保持您在注释中放置的代码片段更容易拉出和读取。)