0

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?

3
  • What do you mean by cardScore[i]? Commented May 14, 2022 at 16:40
  • My List has 3 variables in row. cardScore[i] is the last one Commented May 14, 2022 at 16:45
  • Your Cards class has private fields and not set methods, so you can't update the values. Add a set or update method to Cards; then find the card you want at cardsList.get(index) and call that method. Commented May 14, 2022 at 16:49

1 Answer 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);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.