我制作了一个应用程序,它从两个文本文件(一个带有单词,另一个带有定义)中获取单词和定义。使用随机数生成要显示的单词,然后将 5 个定义与正确的定义一起随机排列以显示选项。该应用程序运行完美,但当使用列表视图单击任何选项时,该应用程序将停止工作。
ArrayList<String> word=new ArrayList<>();
List<String> dfn=new ArrayList<>();
String que="",ans="";
int counter=0;
private void random(){
Random num = new Random();
int nw = num.nextInt(word.size());
que = word.get(nw);
ans = dfn.get(nw);
dfn.remove(ans);
Collections.shuffle(dfn);
dfn = dfn.subList(0,4);
dfn.add(ans);
Collections.shuffle(dfn);
}
TextView t;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_game);
Scanner sc = new Scanner(getResources().openRawResource(R.raw.word2));
Scanner sc2 = new Scanner(getResources().openRawResource(R.raw.def2));
while(sc.hasNextLine()&&sc2.hasNextLine()){
String a = sc.nextLine();
String b = sc2.nextLine();
word.add(a);
dfn.add(b);
我想上面的部分很好。
}
sc.close();
sc2.close();
random();
t = (TextView) findViewById(R.id.t);
t.setText(que);
run();
}
ArrayAdapter<String> adap;
public void run(){
ListView list = (ListView) findViewById(R.id.li);
adap = new ArrayAdapter<>(
this,
android.R.layout.simple_list_item_1,
dfn
);
list.setAdapter(adap);
list.setOnItemClickListener((adapterView, view,i,l)->{//lambda expression
if(dfn.get(i).equals(ans)){
Toast.makeText(getApplicationContext(),"Correct!",Toast.LENGTH_SHORT).show();
counter++;
}
else{
Toast.makeText(getApplicationContext(),"Wrong!",Toast.LENGTH_SHORT).show();
}
TextView t2 = (TextView) findViewById(R.id.t2);
t2.setText("Score : "+counter);
random();
t.setText(que);
run();
}
);
}
}
红糖糍粑
相关分类