counter in the loop, after finish start again - android

I need a counter that when it reaches the maximum value, starts to count from the beginning. I tried some if statements, but i am new in android developing and my attempts was wrong. Please help me a little. Thank you

Example:
int maxValue = 10;
for (int i = 0; i <= maxValue; i++)
{
if ( i == maxValue)
{
i = 0;
}
}

Related

I try to make components to be invisible when I clicked the button but it make my app stop right after I clicked the button

I try to make components to be invisible but after I clicked a button. My app stop immediately
I have an array like this
I try to make components to disappear like this
Your loop is going out of bounds because your array has 6 elements in it and you're iterating from 0 to 6 (that's seven iterations)
Replace:
for (int i = 0; i < 7; i++) {
findViewById(groupSong2[i]).setVisibility(View.INVISIBLE)
}
With this:
for (int i = 0; i < 6; i++) {
findViewById(groupSong2[i]).setVisibility(View.INVISIBLE)
}
Or even better:
for (int i = 0; i < groupSong2.length; i++) {
findViewById(groupSong2[i]).setVisibility(View.INVISIBLE)
}

For loop not acting as expected

Good night.
I have a for loop to calculate some values along the week, during some weeks. My code compare day of the week with the quantity of days that have passed.
The for only works fine on monday when workday is the same than totdays. The rest of days totact is 0 and the fianl equation become NaN.
int totdias = PrefProteos.getInt("dia",0);
int totact = 0;
float califtot = 0;
int stateday = 0;
float promdias;
float promcalif;
int workdays;
Date day = new Date();
SimpleDateFormat formatter = new SimpleDateFormat("EE", Locale.US);
String dayweek = formatter.format(day);
if (dayweek.equals("Mon")) {
stateday = 1;
}
else if (dayweek.equals("Tue")) {
stateday = 2;
}
else if (dayweek.equals("Wed")) {
stateday = 3;
}
else if (dayweek.equals("Thu")) {
stateday = 4;
}
else if (dayweek.equals("Fri")) {
stateday = 5;
}
else if (dayweek.equals("Sat")) {
stateday = 6;
}
else if (dayweek.equals("Sun")) {
stateday = 7;
}
if (stateday >= totdias) {
workdays = 1;
promdias = totdias;
} else {
workdays = (totdias - (stateday - 1));
promdias = stateday;
}
for (int y = workdays; y == totdias;y++) {
for (int x = 1; x <= 12; x++) {
String activ=PrefProteos.getString("act" + x + "-habit", "");
if (!activ.equals("")) {
float notactiv = PrefProteos.getFloat("act" + x + "-puntdia"+y, 0);
califtot = califtot+notactiv;
totact = totact+1;
}
}
}
promcalif = califtot / (totact * promdias);
You have write your first loop condition wrong, you ask only for to run only if workdays = totdias. I guess you want to loop for each days from workdays to totdias
So correct this block :
for (int y = workdays; y == totdias;y++) {
Into
for (int y = workdays; y <= totdias;y++) {
PS :
Your current loop is like writing
if(workdays == totdias) {
You should provide some more code because we can't understand your PrefProteos class and probably the error is in there.
You should also add the full logcat
And you should tell us which line throws the error.
Anyway there are some errors:
Here
for (int y = workdays;y==totdias;y++){
There is no sense of using an equals condition in a for loop exit condition if both equals condition and looping variable are the same, because it will result in an if loop. This is because the for will work only if workdays == totdias since the beginning, elseway it will not work.
For conditions are:
for(counter = defaultvalue; condition that if true, makes the loop goes on; what to do each loop end)
This means that your code will do:
is y(workdays) == totdias?
if yes, do the loop
add one to y(workdays)
exit because the condition is no more true
if not, don't run the loop
So you simply have to call:
if(y == totdias){
//do code
y++;
}
But probably this is an error, because except for monday, this code will never run! so in others days promdias is not istantiated
In the loop
for (int x = 1; x<=12; x++){
String activ=PrefProteos.getString("act" + x + "-habit", "");
if (!activ.equals("")){
//there must be an error here somewhere
float notactiv=PrefProteos.getFloat("act" + x + "-puntdia"+y, 0);
califtot=califtot+notactiv;
totact=totact+1;
}
}
And this must be throwing an error.
promcalif = califtot/(totact*promdias);
Two options:
promidias or totact are 0. you can't do number/0
As said above, promdias might not be istantiated in other days of the week, because the for loop never run
Btw, float name = 0; is not perfect, change them to float name = 0f;

notifyItemRemoved of recyclerView in loop bugs?

When I remove some item in RecyclerView, but it comes to a bug, the bug is hard to occur,but it will occur sometimes. Sometimes the item is not deleted, and still show in recyclerView and can not remove forever. Below is my code:
//the positions is a collection,which collect my item positions that should be removed.
int realPosition;
int temp = 0;
for (int i = 0; i < positions.size(); i++) {
realPosition = positions.get(i) - temp;
getAdapter().remove(realPosition);
getAdapter().notifyItemRemoved(realPosition);
temp++;
}
and my English is poor, hope you can understand. Can anyone help me? Thank you very much.
If you want to remove items from recycler view with in loop then try this
int realPosition;
int temp = 0;
for (int i = 0; i < positions.size(); i++) {
realPosition = positions.get(i) - temp;
getAdapter().remove(realPosition);
temp++;
}
getAdapter().notifyDataSetChanged();
The problem with your code is that each time your loop runs recyclerView's positions get updated so your notifyItemRemoved(realPosition) is removing something other than what you actually want....Hope this helps

Progressbar for two or more operations

I have this code in doInBackground method of an AsyncTask:
for (int i = 0; i < lenght; i++) {
// Do something
count++;
publishProgress(count * 100 / lenght);
}
and all works fine. If i add another operation, how to reflect this with the progress bar?
Now i have this code:
for (int i = 0; i < lenght1; i++) {
// Do something
count++;
publishProgress(count * 100 / lenght1);
}
for (int i = 0; i < lenght2; i++) {
// Do something
count++;
publishProgress(count * 100 / lenght2);
}
How to make the bar start from 0 when operation 1 starts and finish at 100 when operation 2 ends? I tried to change count*100 to count*50 but it seems not to be the right way...
Find an arithmetic formula involving length1 and length2, to publish the progress accordingly.
For example, if you want to give same weight to each increase of count and 100% means count == length1+length2, then you can use something like this: publishProgress(count * 100 / (length1+length2));

Trouble running my code: java.lang.IndexOutOfBoundsException: Index: 0, Size: 0

can someone help me out with this problem. this is the error message I get:
Exception in thread "LWJGL Application" java.lang.IndexOutOfBoundsException: Index: 0, Size: 0
at java.util.ArrayList.rangeCheck(ArrayList.java:635)
at java.util.ArrayList.get(ArrayList.java:411)
at com.mygdx.Papermadness.Papermadness.render(Papermadness.java:102)
at com.badlogic.gdx.backends.lwjgl.LwjglApplication.mainLoop(LwjglApplication.java:206)
at com.badlogic.gdx.backends.lwjgl.LwjglApplication$1.run(LwjglApplication.java:114)
This is the code of my for loop through the huisArray (huis= house).
private ArrayList<Sprite> huisArray = new ArrayList<Sprite>();
spriteBatch.begin();
sprite.draw(spriteBatch);
for (int i = 0; i < huisArray.size(); i++) {
huisArray.get(i).setY(huisArray.get(i).getY() - huisVelocity * delta);
if (huisArray.get(i).getY() <= 200) {
huisArray.remove(huisArray.get(i));
}
}
if (huisArray.get(0).getY() < 1200) {
addNewHuis();
}
huisBatch.begin();
for (int i = 0; i < huisArray.size(); i++) {
huisArray.get(i).setY(huisArray.get(i).getY() - huisVelocity * delta);
if (huisArray.get(i).getY() <= 200) {
huisArray.remove(huisArray.get(i));
}
}
if (huisArray.get(0).getY() < 1200) {
addNewHuis();
}
huisBatch.begin();
for (int i = 0; i < huisArray.size(); i++) {
huisBatch.draw(huisArray.get(i), huisArray.get(i).getX(), huisArray.get(i).getY());
}
spriteBatch.end();
huisBatch.end();
The idea of this code is that houses at the two edges of the screen fall continuesly.
Any help would be appreciated :)
The problem is that huisArray is empty and you are getting values from this list at line
if (huisArray.get(0).getY() < 1200) {
So first add items in huisArray before checking item in huisArray as
private ArrayList<Sprite> huisArray = new ArrayList<Sprite>();
// Add huisArray item here as huisArray.add(addspritehere);
Or change
if (huisArray.get(0).getY() < 1200) {
to
if (huisArray.size() > 0 && huisArray.get(0).getY() < 1200) {
Your error is likely right here:
if (huisArray.get(0).getY() < 1200) {
If your huisArray has a size of zero, then getting an object out of it will throw an exception. You should check if it has anything in it first, as follows
if (huisArray.size() > 0 && huisArray.get(0).getY() < 1200) {
Here:
if (huisArray.get(0).getY() < 1200) {
addNewHuis();
}
You're accessing to the first element in the huisArray, but never checking if it's empty.
Add a size check before:
if (huisArray.size > 0 &&huisArray.get(0).getY() < 1200) {
addNewHuis();
}
There's also another problem in your for loops:
for (int i = 0; i < huisArray.size(); i++) {
huisArray.get(i).setY(huisArray.get(i).getY() - huisVelocity * delta);
if (huisArray.get(i).getY() <= 200) {
huisArray.remove(huisArray.get(i));
}
}
If you remove things inside the for loop just like that, you'll be ignoring some items:
Let's say you're array have 3 elements [A, B, C], and the first one have the conditions to be removed.
So in the first loop, you'll have:
i = 0
array = [A, B, C]
A is removed, and the second loop starts
i = 1
array = [B, C]
See that now you'll check for C (which is in position 1), and you'll never check B
To avoid this (using that kind of for loop) just add:
for (int i = 0; i < huisArray.size(); i++) {
huisArray.get(i).setY(huisArray.get(i).getY() - huisVelocity * delta);
if (huisArray.get(i).getY() <= 200) {
huisArray.remove(huisArray.get(i));
i--;
}
}
Also as an advice, if you know the position of the item you wan't to remove, remove it by index instead of removing by object (it's faster)
huisArray.remove(i);

Categories

Resources