我对ArrayList
有疑问。我有问题文件结构,如:
Y aaaaaa。
Y bbbbbb。
N ccccccc。
N ddddddd。
我想在textview3中输入第一个字母,如Y或N
public void value (){
Scanner answerScanner = new Scanner(getResources().openRawResource(R.raw.answer));
Scanner questionScanner = new Scanner(getResources().openRawResource(R.raw.question));
ArrayList<String> answerList = new ArrayList<String>();
ArrayList<String> questionList = new ArrayList<String>();
try {
while (answerScanner.hasNextLine() ) {
answerList.add(answerScanner.nextLine());
questionList.add(questionScanner.nextLine());
}
} finally {
answerScanner.close();
questionScanner.close();
}
int nextInt = random.nextInt(questionList.size());
String answerString = answerList.get(nextInt);
String questionString = questionList.get(nextInt);
yourAnswerString = answerString.substring(0);
yourQuestionString = questionString.substring(2);
shortform = questionString.substring(0,1);
TextView textGenerateNumber = (TextView)findViewById(R.id.textView1);
TextView textGenerateNumber2 = (TextView)findViewById(R.id.textView2);
TextView textGenerateNumber3 = (TextView)findViewById(R.id.textView3);
textGenerateNumber.setText(yourQuestionString);
textGenerateNumber2.setText(yourAnswerString);
textGenerateNumber3.setText(shortform);
}
然后我补充说:
shortform = questionString.substring(0,1);
TextView textGenerateNumber3 = (TextView)findViewById(R.id.textView3);
textGenerateNumber3.setText(shortform);
我得到了很好的答案我没有第一个......当radom从文本文件中获取第一行时我得到空值(我的textView3为空)。