I'm using XML to store a database of phrases where 5 of these phrases will be displayed to the user at a time. I need to ensure these 5 phrases are unique and, of course, just getting random data can't ensure that. I think I could do it if I could convert the string array that I'm using into a List, but I can't find good info on how to do that. Does anyone have any input on this?开发者_如何学Python
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();
}
}
The easiest way is probably to rewrite getResults()
to return all the strings you need (and additionally not load the "bossResults" array 5 times).
public List<String> getResults(int count){
// Get a random string from our results XML and return the string.
List<String> ret = new ArrayList<String>();
Resources r = getResources();
List<Integer> picked = new List<Integer>();
String[] resultsList = r.getStringArray(R.array.bossResults);
while (ret.size() < count) {
int i = rgenerator.nextInt(resultsList.length);
if (picked.contains(i)) { continue; }
picked.add(i);
ret.add(resultsList[i]);
}
return ret;
}
It might be slow for large values of count
.
EDIT: It falls into an infinite loop if count
is greater than the size of the array!
The easiest way is to remember which items you already selected and if you select one of them again, just discard it. This could be very slow, if the count of items to choose from is low. If that was the case, you could shuffle the whole array and return first n items.
I would make a change to getResults() to do all the work for you in one pass. This way it could remember what values have already been chosen, and shrink the values it then chooses from in the future....something like this:
public List<String> getResults(int count) {
List<String> results = new ArrayList<String>();
Resources r = getResources();
String[] resultsList = r.getStringArray(R.array.bossResults);
List<String> resultsArrayList = new ArrayList<String>(Arrays.asList(resultsList));
for(int i = 0; i < count; ++i) {
int next = rgenerator.nextInt(resultsArrayList.size());
String nextVal = resultsArrayList.remove(next);
results.add(nextVal);
}
return results;
}
精彩评论