0

setImage(cachedImage1, image1, prjcts.get(x)); setImage(cachedImage2, image2, prjcts.get(x); setImage(cachedImage3, image3, prjcts.get(x));

I need to replace x from prjcts.get(x) with a random number between 1 until the maximum entry on my JSON file. The maximum number should be felxible, I can add and delete the entry as much as I want. And one more thing, the numbers aren't allowed to be the same between each other.

Can anybody help me with this?

Thank you very much.

1 Answer 1

1

Try creating a List list and fill it with values from 1 to jsonMax. Then use Java Collections.shuffle(..) to randomize the list.

List<Integer> list = new ArrayList<Integer>();
int jsonMax = getMaxFromJSON(); //supplied by you
for(int i = 1; i<=jsonMax; i++)
    list.add(i);

Collections.shuffle(list);

Increment through list to get the random numbers.

Sign up to request clarification or add additional context in comments.

3 Comments

So I did this, but then the program crashed: int i=0; List<Integer> list = new ArrayList<Integer>(); Random generator = new Random(); int jsonMax = prjcts.size(); //supplied by you for(i = 1; i<=jsonMax; i++) list.add(i); Collections.shuffle(list); setImage(cachedImage1, image1, prjcts.get(i));
I can't help without seeing the stack trace.
Find the exception in the logcat. You can type adb logcat into your command prompt if you have adb setup properly in your environment variables (in Windows). It should also be available in your DDMS perspective in Eclipse.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.