覆盖等于和哈希码时,元素不会添加到列表中

时间:2018-10-15 20:42:36

标签: android arraylist equals contains hashcode

我有一个包含String id的类。我试图覆盖equals和哈希代码到我的模型中以使equals返回true或false取决于id。我这样做是因为我有此模型的数组列表,并且需要在添加之前检查该项目是否在列表中。问题在于,当应将新元素添加到列表中时,它会出现在“回收者”视图中,然后迅速消失

这是我的问号和hashCode方法:

    @Override
public boolean equals(Object obj) {
    boolean isEqual = false;

    if (obj != null && obj instanceof Challenge) {
        isEqual = this.id.equals(((Challenge) obj).getId());
    }

    return isEqual;
}

@Override
public int hashCode() {
    return id.hashCode();
}

这是获取数据的代码:

    public String getChallengeData(DataSnapshot dataSnapshot, String tag) {
    Log.v("ChallengesFragPresenter", "get challenge data called");
    Log.v("ChallengesFragPresenter", "completedChallengesList : " + completedChallengesList.size()
            + ", uncompletedChallengesList " + uncompletedChallengesList.size());
    view.startCompletedChallengesAdapter(completedChallengesList);
    view.startUnCompletedChallengesAdapter(uncompletedChallengesList);

    GenericTypeIndicator<List<Question>> t = new GenericTypeIndicator<List<Question>>() {
    };
    challenge = new Challenge();
    String challengeDate = dataSnapshot.child("date").getValue().toString();
    String challengeSubject = dataSnapshot.child("subject").getValue().toString();
    String challengeState = dataSnapshot.child("state").getValue().toString();
    String challengeId = dataSnapshot.getKey();
    ArrayList challengeQuestionsList = (ArrayList) dataSnapshot.child("questionsList").getValue(t);
    long player1Score = (long) dataSnapshot.child("player1score").getValue();
    long player2Score = (long) dataSnapshot.child("player2score").getValue();

    String player1Name = dataSnapshot.child("player1Name").getValue().toString();
    String player1Image = dataSnapshot.child("player1Image").getValue().toString();
    String player1Uid = dataSnapshot.child("player1Uid").getValue().toString();
    String player2Name = dataSnapshot.child("player2Name").getValue().toString();
    String player2Image = dataSnapshot.child("player2Image").getValue().toString();
    String player2Uid = dataSnapshot.child("player2Uid").getValue().toString();

    String challengerName, challengerImage;

    if (player1Uid.equals(currentUserUid)) {
        currentPlayer = 1;
        challengerName = player2Name;
        challengerImage = player2Image;
        challenge.setSecondChallengerUid(player2Uid);//second means that it is not the player who starts the challenge
        if (tag.equals("onChildAdded")) {
            player1childrenCount++;
        }
    } else {
        currentPlayer = 2;
        challengerName = player1Name;
        challengerImage = player1Image;
        challenge.setSecondChallengerUid(player1Uid);//second means that it is not the player who starts the challenge
        if (tag.equals("onChildAdded")) {
            player2childrenCount++;
        }
    }
    challenge.setCurrentPlayer(currentPlayer);
    challenge.setChallengerName(challengerName);
    challenge.setDate(challengeDate);
    challenge.setImage(challengerImage);
    challenge.setSubject(challengeSubject);
    challenge.setState(challengeState);
    challenge.setId(challengeId);
    challenge.setQuestionsList(challengeQuestionsList);
    String score;
    if (currentPlayer == 1) {
        score = player2Score + " : " + player1Score;
    } else {
        score = player1Score + " : " + player2Score;
    }
    challenge.setScore(score);
    if (challenge.getState().equals("اكتمل")) {
        view.showCompletedChallengesTv();

        if (!completedChallengesList.contains(challenge)) {
            completedChallengesList.add(0, challenge);
            view.notifyAdapters(completedChallengesList.size(), uncompletedChallengesList.size());
        }

    } else if (challenge.getState().equals(refusedChallengeText)) {
        view.showCompletedChallengesTv();

        if (!completedChallengesList.contains(challenge)) {
            completedChallengesList.add(0, challenge);
            view.notifyAdapters(completedChallengesList.size(), uncompletedChallengesList.size());
        }

    } else if (challenge.getState().equals(uncompletedChallengeText)) {
        view.showUncompletedChallengesTv();

        if (!uncompletedChallengesList.contains(challenge)) {
            uncompletedChallengesList.add(0, challenge);
            view.notifyAdapters(completedChallengesList.size(), uncompletedChallengesList.size());
        }
    }

    return player1Uid;
}

0 个答案:

没有答案