简体   繁体   中英

How to update recyclerview data from ItemTouchHelper?

I have a recyclerview of tasks. When user will swipe an element I want to change one parameter(variable "isChecked" from false to true - that will mean the task is completed) in that task.

I created new ItemTouchHelper instance in my activity:

 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);
}

Here I'm getting the task of class "Sorted" and check if the ending time of the task is less than the current time of the day. If so, I want to change the variable.

My adapter class:

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;
 }
}  

Sorted class:

@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;
}
}

My problem is that I don't really understand how to do it. Is there any way to update the data, not create a new task? Or do I need to delete the task I got and insert a new one, with one parameter changed? Maybe I could do it the same way I update the list? :

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

Or maybe I don't need to deal with my adapter, only with database? (I'm using Room). Thanks in advance for help. If it's needed, I will add more information to the question.

In method onSwipe to change UI you only need call some method to set for model current and call adapter.notifyItemChange(viewHolder.getAdapterPosition()) no need create new object or new list

I know you using Room don't forget update it to database. Additional if you using RxJava or Flow and LiveData one thing you need is update entity. Room auto update new list for you

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM