0

我的 DiffUtil 课程:

public class DiffUtilAskQuestionAdmin extends DiffUtil.Callback{

private ArrayList<ModelAskQuestionAdmin> oldArrayList;
private ArrayList<ModelAskQuestionAdmin> newArrayList;


public DiffUtilAskQuestionAdmin(ArrayList<ModelAskQuestionAdmin> oldArrayList, ArrayList<ModelAskQuestionAdmin> newArrayList){
    this.oldArrayList = oldArrayList;
    this.newArrayList = newArrayList;
}

@Override
public int getOldListSize(){
    return oldArrayList.size();
}

@Override
public int getNewListSize(){
    return newArrayList.size();
}

@Override
public boolean areItemsTheSame(int oldItemPosition, int newItemPosition){
    //Compare the uniqueness.. here the key
    return (oldArrayList.get(oldItemPosition).getQuestionId() == newArrayList.get(oldItemPosition).getQuestionId());
    /*return  oldItemPosition == newItemPosition;*/
}

@Override
public boolean areContentsTheSame(int oldItemPosition, int newItemPosition){
    //Check whole object
    return (oldArrayList.get(oldItemPosition) == newArrayList.get(newItemPosition));

}

@Nullable
@org.jetbrains.annotations.Nullable
@Override
public Object getChangePayload(int oldItemPosition, int newItemPosition){
    return super.getChangePayload(oldItemPosition, newItemPosition);
}

}

我的 DiffUtil 适配器:

公共类 AdapterAskQuestionAdmin 扩展 RecyclerView.Adapter<AdapterAskQuestionAdmin.HolderAskQuestionAdmin>{

private Context context;
private ArrayList<ModelAskQuestionAdmin> askQuestionAdminArrayList;

public AdapterAskQuestionAdmin(Context context, ArrayList<ModelAskQuestionAdmin> askQuestionAdminArrayList){
    this.context = context;
    this.askQuestionAdminArrayList = askQuestionAdminArrayList;
}




public void setAskQuestionAdminArrayList(ArrayList<ModelAskQuestionAdmin> newArrayList){
    DiffUtilAskQuestionAdmin diffUtilAskQuestionAdmin = new DiffUtilAskQuestionAdmin(askQuestionAdminArrayList, newArrayList);
    DiffUtil.DiffResult diffResult = DiffUtil.calculateDiff(diffUtilAskQuestionAdmin);
    askQuestionAdminArrayList.clear();
    askQuestionAdminArrayList.addAll(newArrayList);
    diffResult.dispatchUpdatesTo(this);
}

回收站视图实施的主要活动

//First time loading Questions
private Query askQuestionQuery, lastItemQuery;
private ChildEventListener loadAskQuestionChildEventListener, lastItemChildEventListener;
private ValueEventListener loadAskQuestionValueEventListener;
private void loadAskQuestions(){





    //get all questions


    DatabaseReference reference = FirebaseDatabase.getInstance().getReference("DataBase").child("Asked Questions");

    loadAskQuestionValueEventListener = new ValueEventListener(){
      @Override
        public void onDataChange(@NonNull @NotNull DataSnapshot snapshot){
            try {

                for(DataSnapshot ds: snapshot.getChildren()){
                    ModelAskQuestionAdmin modelAskQuestionAdmin = ds.getValue(ModelAskQuestionAdmin.class);
                    askQuestionAdminArrayList.add(modelAskQuestionAdmin);
                    Log.d("QuestionLoading", "For Loop: no of question: " + askQuestionAdminArrayList.size());

                }

                adapterAskQuestionAdmin.setAskQuestionAdminArrayList(askQuestionAdminArrayList);
                questionPosition++;
                if(questionPosition == 1) {
                    String questionKey = snapshot.getKey();

                    mLastQuestionKey = questionKey;
                    mPrevQuestionKey = questionKey;
                    Log.d("QuestionLoading", "onChildAdded: question Key: " + questionKey);
                    Log.d("QuestionLoading", "onChildAdded: mLastQuestionKey Key: " + mLastQuestionKey);
                    Log.d("QuestionLoading", "onChildAdded: mPrevQuestionKey Key: " + mPrevQuestionKey);


                }




                askQuestionRv.setAdapter(adapterAskQuestionAdmin);

                String questionKey = snapshot.getKey();
                Log.d("QuestionLoading", "onChildAdded: questionPosition: " + questionPosition);
                Log.d("QuestionLoading", "onChildAdded: question Key: " + questionKey);
                Log.d("QuestionLoading", "onChildAdded: no of question: " + askQuestionAdminArrayList.size());


            }catch (Exception e) {
                e.printStackTrace();
            }
        }

        @Override
        public void onCancelled(@NonNull @NotNull DatabaseError error){

        }
    };

    reference.addValueEventListener(loadAskQuestionValueEventListener);

我的模特班:

public class ModelAskQuestionAdmin{

private String questionId, type, timestamp, uid, question, option1, option2, option3, option4, option5, option6, answer, image;


public ModelAskQuestionAdmin(){
}


public ModelAskQuestionAdmin(String questionId, String type, String timestamp, String uid, String question, String option1, String option2, String option3, String option4, String option5, String option6, String answer, String image){
    this.questionId = questionId;
    this.type = type;
    this.timestamp = timestamp;
    this.uid = uid;
    this.question = question;
    this.option1 = option1;
    this.option2 = option2;
    this.option3 = option3;
    this.option4 = option4;
    this.option5 = option5;
    this.option6 = option6;
    this.answer = answer;
    this.image = image;
}

public String getQuestionId(){
    return questionId;
}

public void setQuestionId(String questionId){
    this.questionId = questionId;
}

public String getType(){
    return type;
}

public void setType(String type){
    this.type = type;
}

public String getTimestamp(){
    return timestamp;
}

public void setTimestamp(String timestamp){
    this.timestamp = timestamp;
}

public String getUid(){
    return uid;
}

public void setUid(String uid){
    this.uid = uid;
}

public String getQuestion(){
    return question;
}

public void setQuestion(String question){
    this.question = question;
}

public String getOption1(){
    return option1;
}

public void setOption1(String option1){
    this.option1 = option1;
}

public String getOption2(){
    return option2;
}

public void setOption2(String option2){
    this.option2 = option2;
}

public String getOption3(){
    return option3;
}

public void setOption3(String option3){
    this.option3 = option3;
}

public String getOption4(){
    return option4;
}

public void setOption4(String option4){
    this.option4 = option4;
}

public String getOption5(){
    return option5;
}

public void setOption5(String option5){
    this.option5 = option5;
}

public String getOption6(){
    return option6;
}

public void setOption6(String option6){
    this.option6 = option6;
}

public String getAnswer(){
    return answer;
}

public void setAnswer(String answer){
    this.answer = answer;
}

public String getImage(){
    return image;
}

public void setImage(String image){
    this.image = image;
}

@Override
public boolean equals(Object o){
    if(this == o) return true;
    if(!(o instanceof ModelAskQuestionAdmin)) return false;
    ModelAskQuestionAdmin that = (ModelAskQuestionAdmin) o;
    return getQuestionId().equals(that.getQuestionId()) && getType().equals(that.getType()) && getTimestamp().equals(that.getTimestamp()) && getUid().equals(that.getUid()) && Objects.equals(getQuestion(), that.getQuestion()) && Objects.equals(getOption1(), that.getOption1()) && Objects.equals(getOption2(), that.getOption2()) && Objects.equals(getOption3(), that.getOption3()) && Objects.equals(getOption4(), that.getOption4()) && Objects.equals(getOption5(), that.getOption5()) && Objects.equals(getOption6(), that.getOption6()) && Objects.equals(getAnswer(), that.getAnswer()) && Objects.equals(getImage(), that.getImage());
}

@Override
public int hashCode(){
    return Objects.hash(getQuestionId(), getType(), getTimestamp(), getUid(), getQuestion(), getOption1(), getOption2(), getOption3(), getOption4(), getOption5(), getOption6(), getAnswer(), getImage());
}

}

毕竟,我的回收站视图没有更新任何项目。即使它没有在回收站视图中显示任何项目。

我错过了什么?谁能帮我解决这个问题。

4

0 回答 0