1

I'm working on a Java class in an Android project that summarizes array entries saved in previous classes, with each entry itself being an array with multiple elements.

I've have created methods to move forwards and backwards through the entries, but given there can be over 100 entries I would like to create another method that cycles through them instead of pressing the "Next" button over and over again. Is there a way to do this?

I've found that loops will only show the last entry, but below is the best example I can think of, of what I need.

for (int i = Selection; i<=Qty; i++){

Num.setText(Integer.toString(i));
    loadNext();

    try{
        Thread.sleep(1500);
    }catch(InterruptedException e){}

    if (Brk=true){
        break;
    }
}
1
  • I don't know about Android but in Swing there's a dedicated UI thread. If it's similar on Android you'd have to spawn another thread that executes the loop. Otherwise the UI update might be blocked. Commented Jan 19, 2012 at 17:41

3 Answers 3

2

The solution that would be closest to your original answer would be to create a background thread that does the loop, loading each item inside an Activity.runOnUiThread(). You can also do a similar thing with AsyncTask and progress updates. See this article for more information on both of these:

http://developer.android.com/resources/articles/painless-threading.html

However, a better solution is to not have a loop at all - just have a timer, and increment your loop variable each time the timer runs.

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

Comments

1

It may work. However, it will cause your UI to freeze during each time you call the sleep method. In general, when you are dealing with UI stuff, never use Thread class. Instead, use the Handler class. There are a lot of documentation but if, after you have search exhaustively, you can't find a good example just let me know.

Comments

1

Your break condition seems wrong, and causes the loop breaks at the first iteration:

  if (Brk=true){
        break;
    }

Brk=true is an assigment exception, not a comparation exception. It will return always true. The expresion should be Brk==trueto check if the variable value is true. But again, it is a boolean variable, so you don't need to compare, but just reference it at the if statement:

  if (Brk){
        break;
    }

Comments

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.