0

So I'm learning how to make an app, and I have a list of facts, but I only want one fact to be displayed each time the app is opened, and of course it would be different than the last one.

Any ideas on how to do this?

I think it would have something to do with arrays, but I'm not fluent in app development to know for sure.

Thank you for taking the time to answer this, or even just look at it.

~~~~~~

Further explained:

I'm new to app development, so bear with me.

I have a list of different facts, say 50. I know I will need to store them in an array. So I would like it that each time a user opens the app, a random fact is displayed. So I'm not sure how to code this. I'd prefer not to have to use a database - just because I'm not familiar with it (but yes, I'm aware that it is probably only a Google search away).

3 Answers3

1

You need to store all the facts in local database or in file. When user opens application, just select random fact by with depending on the primary key of each fact records.

There are 2 ways to generate a random number, one is by using Math.random() and another is by using Random class. Read more Getting random numbers in Java

Community
  • 1
  • 1
Paresh Mayani
  • 127,700
  • 71
  • 241
  • 295
1

You can simply store your facts in the form of key-value pair in Database. The key could be an increasing counter value and the value would be the fact string itself. Then you can produce random key integers using:

import java.util.Random;
.
.
Random rand;
int randomNum = rand.nextInt((max - min) + 1) + min;
//here min and max is the range of keys to generate random key value

Then just get the Value from the DB using the key. For a more sophisticated solution you can further enhance this and store the keys of facts already shown to the user (By creating a new boolean column besides the key and value columns, like | key | value | isShown |), and then keep generating random keys till a new fact is returned.

Mustafa sabir
  • 4,130
  • 1
  • 19
  • 28
  • Is there a way to do it without a database? I'm not sure how to "link" a database with an app - because I am new. XD –  Jan 12 '17 at 05:56
  • Since you have will have to persist the data in a permanent storage, if not DB you can store them in a file. You can create a csv file with the required fields. For example the header would be `key,fact,isshown` and then the values `1,earth is round,false`. Although I would recommend spending some time and try using the DB since it is very trivial in android and will have many benefits in compared to storage in filessytem, like querying the data later on to replace the facts etc. Check this link for more info https://developer.android.com/training/basics/data-storage/databases.html – Mustafa sabir Jan 12 '17 at 06:01
  • Thank you very much for this advice, I'll start learning how to do this. Also, thank you for pointing me in the right direction. –  Jan 12 '17 at 06:04
  • You're welcome. Please let me know if you need any further help :) – Mustafa sabir Jan 12 '17 at 06:14
1

If you have list and you have to display any of random item from the list then you can do like this:

 Random randomGenerator = new Random();
 ArrayList<Item> randomItem = new ArrayList<Item>();

public Item anyItem()
{
    int index = randomGenerator.nextInt(randomItem.size());
    Item item = randomItem.get(index);
    System.out.println("Random Item is::" + item);
    return item;
}

I hope it helps you.

Vijay Makwana
  • 506
  • 5
  • 14