BekaKK
BekaKK

Reputation: 2243

custom arraylist change item by position

I have a custom ArrayList(Card). I inserted some items into my ArrayList. This is my ArrayList Item class

public class Card {
    private int primary;

    public int getPrimary() {
        return primary;
    }

    public void setPrimary(int primary) {
        this.primary = primary;
    }
}

I want to change my int primary by position

 for (int j=0;j<=CardsNewFragment.card_list.size();j++) {
     CardsNewFragment.card_list.get(mCurrentPage).setPrimary(1);
     if (CardsNewFragment.card_list.get(mCurrentPage).getPrimary() == 1) {
         primaryCardSwitch.setChecked(true);
         primaryCardSwitch.setEnabled(false);
     } else {
         primaryCardSwitch.setChecked(false);
         primaryCardSwitch.setEnabled(true);
     }
  }

for example in my ArrayList's setPrimary() has 1,0,3, now i want to change setPrimary() by position. for example if i selected second item, in second items i want to insert 1 setPrimary() and another position 0 or another values how i can solve my problem?

Upvotes: 2

Views: 419

Answers (1)

Derek Fung
Derek Fung

Reputation: 8211

Actually I am not sure if I understand what you want, but I would give it a try anyway.

public void setPrimaryForPosition(int position, int newPrimary) {

    for (int j=0;j<CardsNewFragment.card_list.size();j++) {
        if(j+1==position) {
            CardsNewFragment.card_list.get(j).setPrimary(newPrimary);
        } else {
            CardsNewFragment.card_list.get(j).setPrimary(0);
        }
    }

}

For your sample suggest in comment, for changing from (1, 2, 4, 0) to (0, -1, 0, 0), you run setPrimaryForPosition(2, -1)

Upvotes: 1

Related Questions