dimanche 20 mars 2016

Comparison logic not working

I have a 9 names that I need to display in a rotation sequence. Like the image:

rotation schedule

As you can see, the names go one position down in the list, with the 9th name taking top of it at each rotation.

The variable that controls the rotation is the integer iName. Theres a method that just reads an user input (an imagebutton touch) and adds or decreases the value of iName. It starts at 0 (list is displayed Adams to Ida), and when the user gives the command it assumes 1 (so the list is displayed from Ida to Henry). Actually, the user can go back in time and turn iName from 5 to 4, so the list changes from Easy-Denver, to Frank-Easy.

I have 9 TextViews doing this job, and it's updated inside a loop, as follows:

int[] arrNames_Ids = {R.id.Name1, R.id.Name2, R.id.Name3, R.id.Name4, R.id.Name5, R.id.Name6, R.id.Name7, R.id.Name8, R.id.Name9};
String arrPeople = new String {"Adams","Boston","Chicago","Denver","Easy","Frank","George","Henry","Ida"}
String sAlternative_Name = "";

for (iCounter = 0; iCounter < 9; iCounter++){
    // iName is a global variable, stars at 0, but it changes upon user request:
    sName = arrPeople[iName];
    // this "if" statement is the problematic piece of code.......
    if (((iCounter == 1)||(iCounter == 3)||(iCounter == 5))&& (sName.equals("Adams")){
        sAlternative_Name = mtAlternative_Name();
        sName = sAlternative_Name;
    } else {
        if ((sAlternative_Name != "") && (sName.equals(sAlternative_Name))){
            sName = "Adams";
        }
    }
    // without the previous "if" the following part works properly, so the list rotates accordingly;
    tvNames = (TextView) getView().findViewById(arrNames_Ids[iCounter]);
    tvNames.setText(sName);
    iName = iName + 1;
    if (iName > 8){
        iName = 0;
    }
}

public String mtAlternative_Name(){
    int iCycle;
    String sName_Aux = "";
    // iCurrent_Cycle is a global variable, assumes values from 0 to 73, and it controls the order in which arrPeople is supposed to be shown:
    iCycle = iCurrent_Cycle % 18;
    switch (iCycle){
        case 2: sName_aux = "George"; break;
        case 4: sName_aux = "Easy"; break;
        case 6: sName_aux = "Chicago"; break;
        case 11: sName_aux = "Henry"; break;
        case 13: sName_aux = "Frank"; break;
        case 15: sName_aux = "Denver"; break;
    }
    return sName_Aux;
}

The idea is to prevent "Adams" from occupying the positions 1, 3 and 5. Everytime he's in those positions someone else (sAlternative_Name) takes the place, and "Adams" goes to the position that person was supposed to take. The array arrPeople doesn't change, example:

If "Adams" was supposed to be in position 4, "Easy" takes the place and "Adams" goes to position 8. But when the list rotates agains (going foward), "Adams" takes position 5 and "Easy" takes position 9.

I'm new to Java and don't have much experience in programming either, but I can't see what's wrong with this code...

The % operator I use it's to make the names in green to take place in "Adams" position. As you can see, "Boston" and "Ida" are not in the game... but were supposed to be......... this is ok, and I'll manage to think something later, but right now I'd wish I could understand why my code isn't working...

Thanks a lot...

Aucun commentaire:

Enregistrer un commentaire