【发布时间】:2023-03-15 06:34:01
【问题描述】:
我正在使用 XML 来存储短语数据库,其中一次将向用户显示其中的 5 个短语。我需要确保这 5 个短语是唯一的,当然,仅仅获取随机数据并不能确保这一点。如果我可以将我正在使用的字符串数组转换为列表,我想我可以做到这一点,但我找不到关于如何做到这一点的好信息。有人对此有意见吗?
public String getResults(){
// Get a random string from our results XML and return the string.
Resources r = getResources();
String[] resultsList = r.getStringArray(R.array.bossResults);
List<String> resultsArrayList = new ArrayList<String>(Arrays.asList(resultsList));
//ArrayList resultsArrayList = ;
String q = resultsList[rgenerator.nextInt(resultsList.length)];
return q;
//resultsList.remove(q);
}
private OnClickListener mAddListener = new OnClickListener()
{
public void onClick(View v)
{
//Declare our TextViews for population from the random results from XML
TextView t1 = new TextView(getApplicationContext());
t1=(TextView)findViewById(R.id.textView1);
TextView t2 = new TextView(getApplicationContext());
t2=(TextView)findViewById(R.id.textView2);
TextView t3 = new TextView(getApplicationContext());
t3=(TextView)findViewById(R.id.textView3);
TextView t4 = new TextView(getApplicationContext());
t4=(TextView)findViewById(R.id.textView4);
TextView t5 = new TextView(getApplicationContext());
t5=(TextView)findViewById(R.id.textView5);
// Get a random result for each textview
String result1 = getResults();
String result2 = getResults();
String result3 = getResults();
String result4 = getResults();
String result5 = getResults();
}
}
【问题讨论】: