开发者

Using a method to get multiple, random strings to show as text in an XML

开发者 https://www.devze.com 2023-01-25 10:07 出处:网络
I have a list of abou开发者_StackOverflow中文版t 10 strings that all have different sayings (adages).How can I go about randomly displaying them for the user to read on an XML?

I have a list of abou开发者_StackOverflow中文版t 10 strings that all have different sayings (adages). How can I go about randomly displaying them for the user to read on an XML?

I have a math().random; that generates a random # between 1 and 10. When that number is hit, I use a simple switch/case that displays a saying to the user in an xml file. How do I go about displaying that predefined string in the xml?

ie:

private void randomIdioms(){
int saying = math.random()*10;

if (saying = 3){
 //THe code I am looking for that displays a string on a view in the XML file

}else if (saying = 2){
}

etc....

Thanks!


Not sure what you mean by display in a xml

but I would recommend this

1)store all your Idioms in a List or array list

2) shuffle it

http://www.java-examples.com/shuffle-elements-java-arraylist-example

3) display the n first ones you want to show.

Edit:

now I re read your question it occurs that your maybe waiting to display just 1 string. in that case

1)store your idioms in a tab[] or arrayList

2)do a index = Math.radom()*sizeOfCollection;

(sorry about that)


regarding the display I still don't get the xml part

say you have in your view a TextView in your xml layout, then what you want to do is

TextView myTextView = (TextView) findViewById(R.id.my_textView);
if(myTextView != null){
   myTextView.setTextView(myTextIdiom);
}

you can look at this for layout and changing layout content

hope it helps

jason

0

精彩评论

暂无评论...
验证码 换一张
取 消