'How to update a specific value in List<Class>?
I need to update one data in my List<Cards>. Class Cards looks like this:
public class Cards implements Comparable<Cards>{
private int cardImage, cardID, cardScore;
public Cards(int cardImage, int cardID, int cardScore){
this.cardImage = cardImage;
this.cardID = cardID;
this.cardScore = cardScore;
}
public int getImage(){
return cardImage;
}
public int getId(){
return cardID;
}
public int getScore(){return cardScore;}
@Override
public int compareTo(Cards o) {
return this.cardID > o.cardID ? 1 : (this.cardID < o.cardID ? -1 : 0);
}
In my MainActivity clas I initiated List cardsList = new ArrayList<>(); and filled it like this
for (i = 0; i < lvl; i++) {
cardsList.add(new Cards(Constants.imageNumber[drawCard[i]], drawId[i], cardScore[i]));
}
Now I want to update values only for cardScore[i]. How can I do this?
Solution 1:[1]
Create setters for your Cards class, then you can call those setters after using List#get to get the card at a specific index.
public class Cards implements Comparable<Cards>{
// other methods omitted for brevity...
public void setCardScore(int cardScore){
this.cardScore = cardScore;
}
}
// Usage:
cardsList.get(i).setCardScore(newCardScore);
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Unmitigated |
