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;
}
}
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.
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.
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;
}
Related
Of the conditions I listed in SelectOutput8, one of them must be true every time.
But that is not the case, and it seems like there is no reason for that.
Details are shown in the image below.
The problem is the delay2, delay4, delay7 is full So no new agent needs to enter according to the condition but that is not the case.
First i have to say that those seize/release blocks are completely useless and probably are just a desperate way you are using to solve the problem
Now, according to your model, there is absolutely nothing blocking your model to move an agent to your seize block because the condition clearly states that if delay1 is free, the agent can exit delay (and in your picture, delay1.size IS equal to zero)
I don't know what you have in selectOutput7 though
Instead of taking select block, you can try with multiple split block. Split blocks allow you to change or create new agent.
I've been scratching my head at this for hours now, but I can't seem to figure this one out.
I'm writing a program that has a constant loop going in a Thread, which occasionally sends back data to another part of the program using an Arraylist.
I only want to send this data back after I have gathered 11 items in my arraylist. So I used this code:
//The rest of the loop in which i gather the values for key and velocity.
notenbuffer.add(new Noot(key,velocity));
if (notenbuffer.size() > 10){
System.out.println("Notenbuffer > 10, verstuur data");
if (notenbuffer.isEmpty()){
System.out.println("buffer is empty!");
}
else {
usbRead.writeNotes(notenbuffer);
System.out.println("emptied the buffer!");
notenbuffer.clear();
}
}
Now for some weird reason the program never empties the Arraylist, and just keeps on adding items to it. I've checked and it does in fact reach the usbRead.writeNotes(notenbuffer) part because this function gets called correctly. When I use the debugger it simply skips to the beginning of the loop after this function was called.
Is there a way in which I can empty an Arraylist once it reaches a certain size?
Edit: I made a logic error by writing if (notenbuffer.isEmpty()) this will always be false because I am already in an if statement which requires this to be false.
Did you put your second if inside your first by accident? (The missing indentation seems to suggest so). Having if
(notenbuffer.isEmpty()) inside the if (notenbuffer.size() > 10) block
makes no sense at all logically. After all if your List size is > 10
then the list is obviously not empty. So if (notenbuffer.isEmpty())
can never be true at all. – OH GOD SPIDERS 15 mins ago
This was indeed the problem. Removing this exposed an NoClassDefFoundError that needed resolving.
I am playing audio in background and I want the control of program to stay stand still till the audio playing is over for that I am using empty while loop as follows
while(isPlaying==true){};
mediaPlayer.stop();
as you can see while loop holds program control till audio is playing and after that next instruction is executed. This is working fine but I came to know that this is not a proper way to do this empty-while is expensive I am searching for alternative. Please Help.
Assuming your program is in Java (...why did you give it three language tags?) You have a few options. You could use a proper synchronization event, e.g.:
// fields
Object playerStopEvent = new Object();
boolean isPlaying;
// in your media player, when playback is complete:
synchronized (playerStopEvent) {
isPlaying = false;
playerStopEvent.notifyAll();
}
// elsewhere, when waiting for playback to complete:
synchronized (playerStopEvent) {
while (isPlaying) {
try {
playerStopEvent.wait();
} catch (InterruptedException x) {
// abort or ignore, up to you
}
}
}
mediaPlayer.stop();
See the official tutorial on Guarded Blocks for more examples.
You could also just have mediaPlayer call some callback when it is finished, and e.g. disable GUI components when you start playing and re-enable them when the finished callback is called (you could also use an event listener approach here).
Without more info, I recommend the latter, as it won't prevent you from doing other unrelated things (or keep your program from responding at all) while the player is playing, but the former may be more appropriate depending on your situation.
If it's in C or C++ the concept is the same. Use whatever equivalent of condition variables / events you have for the first option, or whatever equivalent of callbacks / listeners / signals+slots you have for the second.
well, in my humble opinion, it's better to use another implementation..
try to use thread so that it won't hang your program in there (it's a background audio afterall; you might want to do something else while the audio is playing)..
try to check this page out..
First thing is that you don't have to compare 2 Boolean fields that you have done in your code...
while(isPlaying==true){};
you can do so like..
while(isPlaying){};
and, now that you have told that you are using java, you can try this...
while(isPlaying){
Thread.sleep(1);
};
You may consider a sleep(time in milliseconds ). This will allow your thread executing while loop to sleep for specified milliseconds and then check the condition again.
while(isPlaying==true)
{
Thread.currentThread().sleep(1000); // sleep for 1 sec
};
This once is quick but the better way is to use some wait() and notify() mechanism as suggested by #JasonC in his answer.
You really don't need the {} in your empty while loop.
while(isPlaying); would suffice.
Also, as others have already suggested, consider using a delay inside your loop, i.e.
Thread.sleep(100); // sleeps for 1/10 of a seconds in Java
Or
delay(100); // leeps for 1/10 of a seconds in Java
The simple way is that put sleep(1) in while loop. And cpu usage won't take more.
I have a series of IF statements in order of execution, my dilemma is if one of the IF statements is entered I would like the calling method to wait till the called function is finished. The thing is my called function is recursive so I have no idea when it's going to end. Here's the calling method's code.
if(log.isChecked())
{
runlog(sdPath.getAbsolutePath());
}
if(tmp.isChecked())
{
runtmp(sdPath.getAbsolutePath());
}
if(txt.isChecked())
{
runtxt(sdPath.getAbsolutePath());
}
if(bf.isChecked())
{
runbf(sdPath.getAbsolutePath());
}
if(ed.isChecked())
{
runed(sdPath.getAbsolutePath());
}
If log.isChecked() is entered, I would like the calling function(the code I've shown here, that function) to wait till it checks the next condition which is tmp.isChecked()
As I mentioned before ALL the called functions runlog, runtmp, runtxt, runbf, runed are recursive. How do I achieve what I want?
I think recursion can is best explained with the movie "Inception". You have 3 things you want to do.
Go to sleep.
Dream.
Wake up and eat a bowl of cereal.
Imagine you are dreaming and then have a dream within that dream. You now can't wake up and eat until you exit out of the deeper dream and finish the current dream.
That is basically what is happening in your example except instead of dreaming you ran the first if and you can't exit it until it reaches its return condition then you get to wake up and eat (enter the second if).
This should happen by default. The nested functions being recursive makes no difference here.
Each if block will only be executed after one the previous one has completed unless your nested functions are starting threads or something of that sort.
Is there a way to control the speed of execution of a loop ?
I have a simulation that runs in a loop of 30000 steps. I want to visualise whats happening in that loop and if possible control the speed of execution while its running. Any ideas how i could do that ?
You could add a sleep to the loop to pause each iteration.
A better question though, is how are you visualizing this? I'm guessing you're watching the text flash by on the console... if that's the case you might want to consider outputting to files rather than the screen. That way you can read through the output at your leisure and you don't have to add artificial slowdowns to the program.
...but if by "visualise" it's an actual GUI thing, then yeah, the sleep might be better.
Put a Thread.sleep() statement inside the loop. Beware though that you have to handle the exception.
for(int i = 0; i < 30000; i++) {
...
try {
Thread.sleep(100);
}
catch(InterruptedException e) {
// do something with e
}
}
you can put inside your loop Thread.sleep(latency); where latency is in millis.
You can try to use 'Thread.sleep()' as the other guys said. But to "know" what happens in the loop I think you'd better debug it.. I think the worst case is to Print everything on the screen (wouldn't be so bad outside a loop, but considering 50+ loops it becomes impracticable.