0

I have a loop that runs methods with 2 parameters. However, the indexPath parameter always equals 0 after the if statement but not before.

    ArrayList<RelativeLayout> containers2 = new ArrayList<>();
    containers2.add(dayNameContainer);
    containers2.add(dayNumberContainer);
    containers2.add(monthContainer);
    containers2.add(yearContainer);


    for (RelativeLayout container : containers2) {
        addContainerToView(container, 0);
        addContainerToView(container, 1);
        addContainerToView(container, 2);
        addContainerToView(container, 3);
    }

Method:

public void addContainerToView(RelativeLayout container, int indexPath) {
// HERE (BEFORE THE IF STATEMENT) THE INDEXPATH PARAMETER IS CORRECT
    if (sharedPreferences.getIntegerValue(container.getTag().toString()) == indexPath) {
        // ISSUE HERE CONSTANTLY RECIEVING INDEX PATH AS 0 BUT ONLY AFTER THE IF STATEMENT ABOVE

        positioningContainer.addDragView(container, container);
    }
}

I don't really know what more to say to be honest. It seems like very strange behavior.

Luke C
  • 140
  • 2
  • 12
  • You're receiving `indexPath` as 0 because you execute `addContainertToView` 4 time given the `for` loop you have written here. Are you sure that's really what you want? Still sort of unclear what the problem is exactly. – Brian Jan 17 '18 at 22:00
  • I know it's not the best way but for the moment it'd be easier for me to leave it how it is for now. The problem is that I'm passing in a value 0-4 as a parameter of a method and then an if statement changes the value completely randomly. Hope that clears a few things up :) – Luke C Jan 17 '18 at 22:06
  • I even tried it without a for loop and the problem still persisted. – Luke C Jan 17 '18 at 22:11
  • You have no code showing anything where you're updating the value in your `SharedPreferences`. If you're not updating the int value you're querying, it's never going to change. It's not completely clear why you're using `SharedPreferences` to track your indexPath comparison in a look up. – Bryan Dormaier Jan 17 '18 at 22:45
  • I have tried changing the if statement altogether to something completely different just to see if I can find the issue further. I replaced it with a simple comparison statement seeing if indexPath < 1 and every time it returns null because it's 0 despite it not being 0 before the if statement. Therefore I have reason to believe it is not anything to do with my SharedPreferences. – Luke C Jan 17 '18 at 22:48

0 Answers0