我有一个分配给ShuffleQuote()方法的按钮,该按钮绘制一个随机数组项。问题在于有时会重复绘制随机绘制的元素。我想避免它。我该怎么做? 有什么想法吗?
Quotes [] quotesArray = new Quotes[]{
quotes01,quotes02,quotes03,quotes04,quotes05,quotes06,quotes07,quotes08,quotes09,quotes10};
public void ShuffleQuote(){
Collections.shuffle(Arrays.asList(quotesArray));
}
mButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
ShowRandomQuote();
}
});
}
public void ShowRandomQuote(){
ShuffleQuote();
mImage.setImageResource(quotesArray[0].getmImage());
mQuoteText.setText(quotesArray[0].getmQuoteText());
mWiki.setText(quotesArray[0].getmWiki());
}
编辑
我正在尝试类似的操作,但出现错误:
public void ShowRandomQuote() {
ShuffleQuote();
List<Quotes> quotesList = Arrays.asList(quotesArrays);
if(quotesList.isEmpty()) return;
Quotes quotes= quotesList.remove(0);
mQuoteText.setText(quotes.getmQuoteText());
mWiki.setText(quotes.getmWiki());
mImage.setImageResource(quotes.getmImage());
}
java.lang.RuntimeException:无法启动活动ComponentInfo {com.example.cytatnadzi / com.example.cytatnadzi.MainActivity}:java.lang.UnsupportedOperationException ... 在com.example.cytatnadzi.MainActivity.ShowRandomQuote(MainActivity.java:70) 在com.example.cytatnadzi.MainActivity.onCreate(MainActivity.java:45)