我有一系列的话。当活动启动时,它会显示一个单词,然后当您单击该屏幕时,它会显示该数组中的另一个随机单词。我能够做到这一点(有一些帮助),但我有点卡住了。单击屏幕以显示新单词时,如何确保单词尚未显示在单词中。这是我到目前为止所拥有的。
private String[] myString;
public static final Random rgenerator = new Random();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.study);
//Takes a random item from array and creates string
Resources res = getResources();
myString = res.getStringArray(R.array.list);
String q = myString[rgenerator.nextInt(myString.length)];
//Displays random array string in TextView
TextView wordTextView = (TextView) findViewById(R.id.word);
wordTextView.setText(q);
//Set up click listener for new word
View newWord = findViewById(R.id.word);
newWord.setOnClickListener(this);
}
public void onClick(View v) {
switch (v.getId()) {
case R.id.word:
//Takes a random item from array and creates string
String q = myString[rgenerator.nextInt(myString.length)];
//Displays random array string in TextView
TextView wordTextView = (TextView) findViewById(R.id.word);
wordTextView.setText(q);
}
}
答案 0 :(得分:1)
如果你想一想,你有两种方法可以做到:
答案 1 :(得分:0)
我的建议是使用ArrayList。如果要保留内容,则可能需要2个ArrayLists。
实际上非常简单:
List wordsList = new ArrayList<String>();
List usedList = new ArrayList<String>();
String s = wordsList.get(rgenerator.nextInt(wordsList.size()));
wordsList.remove(s);
usedList.add(s);
如果您想要列出您使用过的单词,则第二个列表是可选的。如果您不需要它,则可以删除“usedList”行。对不起,如果有任何错误。写得有点仓促。在任何一种情况下,在Javadocs中查找ArrayList,你会更好地理解它。您实际上是从ArrayList中删除了String,因此在这种情况下您不必担心重复。