Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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);
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading