jeudi 30 janvier 2020

STRIKE_THRU_TEXT_FLAG not working as Expected

This is my RecyclerView Adaptor Class

public class TodoAdaptor extends RecyclerView.Adapter<TodoAdaptor.SingleTodoView> {

    private CheckBox checkbox;
    private TextView title, dueDate;
    private Context context;
    private ArrayList<SingleTodo> todoList;
    private onItemCLickListener itemCLickListener;

    public TodoAdaptor(Context context, ArrayList<SingleTodo> todoList, onItemCLickListener onItemClickListener) {
        this.context = context;
        this.todoList = todoList;
        this.itemCLickListener = onItemClickListener;
    }


    @NonNull
    @Override
    public SingleTodoView onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.single_todo, parent, false);
        return new SingleTodoView(v, itemCLickListener);
    }

    @Override
    public void onBindViewHolder(@NonNull SingleTodoView holder, int position) {
        SingleTodo singleTodo = todoList.get(position);
        checkbox.setChecked(singleTodo.isComplete());
        title.setText(singleTodo.getTitle());
         if (singleTodo.isComplete()) {
            title.setPaintFlags(title.getPaintFlags() | Paint.STRIKE_THRU_TEXT_FLAG);
            Toast.makeText(context, "IsCompleted", Toast.LENGTH_SHORT).show();
        } else {
            title.setPaintFlags(title.getPaintFlags() & (~Paint.STRIKE_THRU_TEXT_FLAG));
            Toast.makeText(context, "Not IsCompleted", Toast.LENGTH_SHORT).show();
        }
        if (singleTodo.getDueDate().isEmpty()) {
            dueDate.setVisibility(View.GONE);
        } else {
            dueDate.setText(singleTodo.getDueDate());
        }
    }

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

    class SingleTodoView extends RecyclerView.ViewHolder {

        private SingleTodoView(@NonNull View itemView, final onItemCLickListener itemCLickListener) {
            super(itemView);
            checkbox = itemView.findViewById(R.id.todo_list_completed_checkbox);
            title = itemView.findViewById(R.id.todo_list_title);
            dueDate = itemView.findViewById(R.id.todo_list_due_date);
            title.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    itemCLickListener.onTextClickListener(getAdapterPosition());
                }
            });
            checkbox.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    itemCLickListener.onCheckboxClickListener(getAdapterPosition());
                }
            });
        }
    }

    public interface onItemCLickListener {
        void onTextClickListener(int position);
        void onCheckboxClickListener(int position);
    }
}

This is My Adaptor

 todoAdaptor = new TodoAdaptor(this, singleTodoArrayList, new TodoAdaptor.onItemCLickListener() {
            @Override
            public void onTextClickListener(int position) {
                singleTodo = singleTodoArrayList.get(position);
                singleTodo.setTitle("This is Test");
                singleTodoArrayList.set(position, singleTodo);
                todoAdaptor.notifyItemChanged(position);
            }

            @Override
            public void onCheckboxClickListener(int position) {
                singleTodo = singleTodoArrayList.get(position);
                singleTodo.setComplete(!singleTodo.isComplete());
                singleTodoArrayList.set(position, singleTodo);
                todoAdaptor.notifyItemChanged(position);
            }
        });

and This is my SingleTodo Class

package com.example.simpletodo.classes;

public class SingleTodo {
    private int id;
    private String title;
    private String dueDate;
    private boolean isComplete;

    public SingleTodo(int id, String title, String dueDate, boolean isComplete) {
        this.id = id;
        this.title = title;
        this.dueDate = dueDate;
        this.isComplete = isComplete;
    }

    public int getId() {
        return id;
    }

    public String getTitle() {
        return title;
    }

    public String getDueDate() {
        return dueDate;
    }

    public boolean isComplete() {
        return isComplete;
    }

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

    public void setTitle(String title) {
        this.title = title;
    }

    public void setDueDate(String dueDate) {
        this.dueDate = dueDate;
    }

    public void setComplete(boolean complete) {
        isComplete = complete;
    }
}

Whenever I check the checkbox, Text of that item should paint Strikethrough and Its not working as Expected

I have 3 Items in the list, when i click checkbox Item get Checked and Text Strikethrough works but when i again click checkbox checkbox stay checked (although Object is being Updated as it should be) and strikethrought text revert to normal, and i have to click checkbox twice again for check box to get unchecked

other issue is that when i checked on item and then i try to check other item, Current Item text get replaced by one of the other checked item Text. Images :
Default:
Default
When i click the Checkbox Once:
Clicked on Checkbox Once
When i Click the Checkbox Again:
Checkbox Checked Twice
When i Checked multiple Items one by one(I have not changed position of any item before checking they was in default order and after checking --in order-- this is what i get)
multiple Checkbox Issue
Any Help is Appreciated. I just want my code to work as Expected.




Aucun commentaire:

Enregistrer un commentaire