【问题标题】:RecyclerView Color Change Deletion BugRecyclerView换色删除Bug
【发布时间】:2020-09-27 03:26:30
【问题描述】:

Assignments Pre-Deletion

After Deletion

我正在创建一个学校规划器应用程序,并将我正在从房间数据库读取的作业显示到 RecyclerView 中。每个项目行都有分配名称、类型和截止日期,以及一个删除按钮。如果作业过期,则每个项目行的背景变为红色,如果作业今天到期,则变为绿色,如果作业将在未来(今天过去)到期,则为默认颜色。当颜色(红色或绿色)的作业位于具有不同颜色的作业之上时,我遇到的错误就会发生。当带颜色的分配被删除时,下面的分配将更改为先前删除的分配的颜色。如果重新启动应用程序或启动另一个 Activity,然后再次拉起 RecyclerView,则分配将恢复到正确的颜色。

[更新] 不知道这是否会影响问题,但我的 item_row.xml RecyclerView 文件不使用 CardView

这是我改变颜色的方法:

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        //Fetching the database table at the current position from database list
        AssignmentTable assignmentTable = assignmentList.get(position);
        //Instantiating the views
        String due = "Due: "+assignmentTable.dueDate;
        String type = assignmentTable.type;
        holder.txtItemName.setText(assignmentTable.name);
        holder.txtItemDate.setText(due);
        holder.txtItemType.setText(type);
        //Setting the checkboxes to their saved state
        if(assignmentTable.isCompleted == true){
            holder.checkBox.setChecked(true);
        }else {
            holder.checkBox.setChecked(false);
        }
        //Fetching current due date and parsing
        Date currentDate = getCurrentDate();
        String dueDateAsString = assignmentTable.dueDate;
        Date dueDateOfCurrentAssignment = parseDate(dueDateAsString);
        //changing the background color programmatically
        if(currentDate.equals(dueDateOfCurrentAssignment)){
            holder.txtItemDate.setText("Due: Today"); //changes due date
            holder.itemView.setBackgroundColor(Color.parseColor("#228b22")); // changes color to green
            Log.d("REACHED", "GREEN COLOR");
        }
        if(dueDateOfCurrentAssignment.before(currentDate)){
            holder.itemView.setBackgroundColor(Color.parseColor("#8b0000")); // turns the background red
            holder.txtItemDate.setTextColor(Color.parseColor("#ff7a7a")); // turns text bright red
            Log.d("REACHED", "RED COLOR");
        }
        //Logging fields
        String assignment = "\nName: " + assignmentTable.name + "\nDue Date: " + assignmentTable.dueDate 
           + "\nType: " + assignmentTable.type + "\nCompleted: " + assignmentTable.isCompleted;
        Log.d("AT END OF ONBIND", "\n" + assignment);

    }```

and here is the method called on the delete buttons onClick. 
    @Override
    public void onClick(View view) {
        //Getting database table at the specified position to delete
        int position = getAdapterPosition();
        AssignmentTable assignmentTable = assignmentList.get(position);
        ItemRepository itemRepository = new ItemRepository(view.getContext());
        //Deleting from the database
        itemRepository.deleteItem(assignmentTable);
        itemRepository.updateItem(assignmentTable);
        notifyDataSetChanged();
        Toast.makeText(view.getContext(),"Deleted!",Toast.LENGTH_SHORT).show();
    }```

【问题讨论】:

  • 请张贴一些你的问题的截图。

标签: java android debugging android-recyclerview


【解决方案1】:

当您在删除方法中调用 notifyDataSetChanged 时。你也应该打电话给notifyItemRemoved(position)

以下是文档中的说明:

通知任何已注册的观察者先前位于位置的项目已从数据集中删除。

希望一切顺利!

【讨论】:

  • 感谢您的建议,但不幸的是这似乎不起作用
  • 你如何确定recyclerview的大小?你在 getItemCount 中的代码是什么?
  • 删除项目时,您是否将其从列表中删除(从中获取表格)?
  • @Override public int getItemCount() { return assignmentList.size(); } 这是我在 getItemCount 中的代码
  • 我将assignmentList.remove(position); 添加到onClick();,但这没有帮助
【解决方案2】:

已解决问题!原来我只需要在 OnBindViewHolder(); 方法中添加一个额外的 else 语句

    @Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
    //Fetching the database table at the current position from database list
    AssignmentTable assignmentTable = assignmentList.get(position);
    //Instantiating the views
    String due = "Due: "+assignmentTable.dueDate;
    String type = assignmentTable.type;
    holder.txtItemName.setText(assignmentTable.name);
    holder.txtItemDate.setText(due);
    holder.txtItemType.setText(type);
    //Setting the checkboxes to their saved state
    if(assignmentTable.isCompleted == true){
        holder.checkBox.setChecked(true);
    }else {
        holder.checkBox.setChecked(false);
    }
    //Fetching current due date and parsing
    Date currentDate = getCurrentDate();
    String dueDateAsString = assignmentTable.dueDate;
    Date dueDateOfCurrentAssignment = parseDate(dueDateAsString);
    //changing the background color programmatically
    if(currentDate.equals(dueDateOfCurrentAssignment)){
        holder.txtItemDate.setText("Due: Today"); //changes due date
        holder.txtItemDate.setTextColor(Color.parseColor("#ff0000"));
        holder.itemView.setBackgroundColor(Color.parseColor("#228b22")); // changes color to green
        Log.d("REACHED", assignmentTable.name + " GREEN COLOR");
    }else if(dueDateOfCurrentAssignment.before(currentDate)){
        holder.itemView.setBackgroundColor(Color.parseColor("#8b0000")); // turns the background red
        holder.txtItemDate.setTextColor(Color.parseColor("#ff7a7a")); // turns text bright red
        Log.d("REACHED", assignmentTable.name + " RED COLOR");
    }else{
        holder.itemView.setBackgroundColor(Color.parseColor("#191970"));
        holder.txtItemDate.setTextColor(Color.parseColor("#ff0000"));
        Log.d("REACHED", assignmentTable.name + " REGULAR BACKGROUND");
    }
    //Logging fields
    String assignment = "\nName: " + assignmentTable.name + "\nDue Date: " + assignmentTable.dueDate + "\nType: " +
            assignmentTable.type + "\nCompleted: " + assignmentTable.isCompleted;
    Log.d("AT END OF ONBIND", "\n" + assignment);

}

【讨论】:

    猜你喜欢
    • 2016-07-07
    • 1970-01-01
    • 2015-01-07
    • 2012-12-25
    • 1970-01-01
    • 2018-03-15
    • 2023-03-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多