如何从ItemTouchHelper更新recyclerview数据?

问题描述 投票:0回答:1

我对任务有一个recyclerview。当用户轻扫元素时​​,我想在该任务中更改一个参数(变量“ isChecked”从false更改为true-这表示任务已完成)。

我在我的活动中创建了新的ItemTouchHelper实例:

 new ItemTouchHelper(new ItemTouchHelper.SimpleCallback(0, ItemTouchHelper.LEFT | ItemTouchHelper.RIGHT) {
        @Override
        public boolean onMove(@NonNull RecyclerView recyclerView, @NonNull RecyclerView.ViewHolder viewHolder, @NonNull RecyclerView.ViewHolder target) {
            return false;
        }

        @Override
        public void onSwiped(@NonNull RecyclerView.ViewHolder viewHolder, int direction) {
            Sorted current = adapter.getSortedAtPosition(viewHolder.getAdapterPosition());
            int time = current.getSortedDuration() + current.getSortedTimeBegin();
            int hour = Calendar.getInstance().get(Calendar.HOUR_OF_DAY);
            int minute = Calendar.getInstance().get(Calendar.MINUTE);
            if (hour > time/60){
                //change
                Toast.makeText(ShowSortedActivity.this, "check1", Toast.LENGTH_SHORT).show();
            }
            else if (hour == time/60){
                if (minute > time % 60){
                    //change
                    Toast.makeText(ShowSortedActivity.this, "check2", Toast.LENGTH_SHORT).show();
                }
                else{
                    //do nothing
                    Toast.makeText(ShowSortedActivity.this, "uncheck1", Toast.LENGTH_SHORT).show();
                }
            }
            else{
                //do nothing
                Toast.makeText(ShowSortedActivity.this, "uncheck2", Toast.LENGTH_SHORT).show();
            }
        }
    }).attachToRecyclerView(showSorted);
}

这里我正在获取“已排序”类的任务,并检查任务的结束时间是否小于当天的当前时间。如果是这样,我想更改该变量。

我的适配器类:

public class SortedAdapter extends RecyclerView.Adapter<SortedAdapter.SortedViewHolder> {

private List<Sorted> list = new ArrayList<>();

@NonNull
@Override
public SortedViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
    View itemView = LayoutInflater.from(parent.getContext())
            .inflate(R.layout.tasks2_layout , parent, false);
    return new  SortedAdapter.SortedViewHolder(itemView);
}

@Override
public void onBindViewHolder(@NonNull final SortedViewHolder holder, final int position) {
    final Sorted data = list.get(position);
    holder.title.setText(data.getSortedName());
    holder.date.setText(data.getSortedDate());
    holder.category.setText(String.valueOf(data.getSortedCategory()));
    holder.attach.setText(String.valueOf(data.isSortedAttach()));
    holder.to.setText(String.valueOf(toTime(data.getSortedDuration() + data.getSortedTimeBegin())));
    holder.from.setText(String.valueOf(toTime(data.getSortedTimeBegin())));
    holder.isChecked.setText(String.valueOf(data.isChecked()));    
}

public void setSortedData(List<Sorted> sortedList){
    this.list = sortedList;
    notifyDataSetChanged();
}

public Sorted getSortedAtPosition(int position){
    return list.get(position);
}

public void setSorted(Sorted data){

}

@Override
public int getItemCount() {
    return list.size();
}

static class SortedViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{       

    private TextView title;
    private TextView date;
    private TextView from;
    private TextView to;
    private TextView category;
    private TextView attach;
    private TextView isChecked;

    SortedViewHolder(@NonNull View itemView) {
        super(itemView);
        title = itemView.findViewById(R.id.tv_title1);
        date = itemView.findViewById(R.id.tv_date1);
        from = itemView.findViewById(R.id.tv_from3);
        to = itemView.findViewById(R.id.tv_to3);
        category = itemView.findViewById(R.id.tv_category1);
        attach = itemView.findViewById(R.id.tv_attach1);
        isChecked = itemView.findViewById(R.id.tv_isChecked);
    }       
}

private static String toTime(int a) {
    String s = "";
    int b = a/60;
    int c = a%60;
    if (c < 10) {
        s =  b + " : " + 0 + c;
    }
    else {
        s =  b + " : " + c;
    }
    return s;
 }
}  

排序类别:

@Entity
public class Sorted {
@PrimaryKey(autoGenerate = true)
public int id;

public String name;

public int timeBegin;

public int duration;

public int category;

public boolean attach;

public String date;

public String categoryChart;

public boolean checked;

public Sorted(String name, int timeBegin, int duration, int category, boolean attach, String date, String categoryChart, boolean checked) {
    this.name = name;
    this.timeBegin = timeBegin;
    this.duration = duration;
    this.category = category;
    this.attach = attach;
    this.date = date;
    this.categoryChart = categoryChart;
    this.checked = checked;
}

public void setSortedId(int id) {
    this.id = id;
}

public String getSortedName() {
    return name;
}

public int getSortedTimeBegin() {
    return timeBegin;
}

public int getSortedDuration() {
    return duration;
}

public int getSortedCategory() {
    return category;
}

public boolean isSortedAttach() {
    return attach;
}

public String getSortedDate() {
    return date;
}

public String getSortedCategoryChart() {
    return categoryChart;
}

public boolean isChecked() {
    return checked;
}

public void setChecked(boolean checked) {
    this.checked = checked;
}
}

我的问题是我不太了解该怎么做。有什么方法可以更新数据,而不是创建新任务?还是我需要删除获得的任务并插入新的任务,并更改一个参数?也许我可以用更新列表的方式来做? :

public void setSortedData(List<Sorted> sortedList){
this.list = sortedList;
notifyDataSetChanged();
 } 

或者也许我不需要处理我的适配器,只需要处理数据库? (我正在使用Room)。在此先感谢您的帮助。如果需要,我将向该问题添加更多信息。

java android android-recyclerview recycler-adapter
1个回答
1
投票

在更改用户界面的方法onSwipe中,您只需要调用某些方法来为模型current设置并调用adapter.notifyItemChange(viewHolder.getAdapterPosition()),而无需创建新对象或新列表

我知道您正在使用Room,不要忘记将其更新到数据库。另外,如果您使用RxJava或Flow和LiveData,则需要做的一件事情是更新实体。会议室自动为您更新新列表

© www.soinside.com 2019 - 2024. All rights reserved.