samedi 2 juillet 2016

Getting wrong itemId in adapter

I have a list of checkLsitItems which I am showing in recyclerview with a custom adapter. I have checkbox for the list item. I want to set the status on a checkListItem as 1 or 0 based on check box is checked or unchecked.

For this I have implemented the code, but when I try to update when check box is checked it's showing checkListItemId as 31 everytime, if any checkbox is checked the checkListItemId is 31.

adapter:

    public class CheckListItemAdapter extends RecyclerView.Adapter<CheckListItemAdapter.MyViewHolder>{

    private List<CheckListItem> checkListItems;
    int pendingItems,completedItems;
    Context context;
    private String status;

    private String checkListItemId;
    public CheckListItem item;

    private final OnItemClickListener listener;

    public interface OnItemClickListener {
        void onItemClick(CheckListItem item);
    }
    public class MyViewHolder extends RecyclerView.ViewHolder {
        public TextView title, budget;
        public RelativeLayout parent;
        private CheckBox checkBox;


        public MyViewHolder(View view) {
            super(view);
            title = (TextView) view.findViewById(R.id.title);
            budget =(TextView) view.findViewById(R.id.budget);
            checkBox = (CheckBox) view.findViewById(R.id.checkBox);

        }


        public void bind(final CheckListItem item, final OnItemClickListener listener) {

            itemView.setOnClickListener(new View.OnClickListener() {
                @Override public void onClick(View v) {
                    listener.onItemClick(item);
                }
            });
        }
    }


    public CheckListItemAdapter(List<CheckListItem> checkListItems,Context context,OnItemClickListener listener) {
        this.checkListItems = checkListItems;
        this.context = context;
        this.listener = listener;
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.checklist_item, parent, false);

        return new MyViewHolder(itemView);
    }

    @Override
    public void onBindViewHolder(MyViewHolder holder, int position) {

        item = checkListItems.get(position);
        holder.title.setText(item.getTitle());
        holder.budget.setText(item.getBudget());

        checkListItemId = item.getCheckListItemId();

        status = item.getStatus();

        if(status.equals("1"))
        {
            holder.checkBox.setChecked(true);
        }
        else {
            holder.checkBox.setChecked(false);
        }

        holder.checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {

                if (isChecked) {

                    status = "1";

                    HashMap<String, String> params = new HashMap<String, String>();


                    params.put("checklistItemId", item.getCheckListItemId());
                    params.put("budget",item.getBudget());
                    params.put("text", item.getTitle());
                    params.put("time_due", item.getDateTime());
                    params.put("reminder",item.getReminder());
                    params.put("status", status);


                    new UpdateCheckListItemsAsyncTask(context).execute(params);

                    Intent intent1 = ((Activity) context).getIntent();
                    intent1.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
                    ((Activity) context).finish();
                    ((Activity) context).startActivity(intent1);

                } else

                {
                    status = "0";

                    HashMap<String, String> params = new HashMap<String, String>();

                    params.put("checklistItemId",checkListItemId);
                    params.put("budget",item.getBudget());
                    params.put("text",item.getTitle());
                    params.put("time_due",item.getDateTime());
                    params.put("reminder",item.getReminder());
                    params.put("status", status);

                    new UpdateCheckListItemsAsyncTask(context).execute(params);

                    Intent intent1 = ((Activity) context).getIntent();
                    intent1.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
                    ((Activity) context).finish();
                    ((Activity) context).startActivity(intent1);
                }

            }

        });


        holder.bind(checkListItems.get(position), listener);

        item.setCompletedItem(completedItems);
        item.setPendingItem(pendingItems);

    }

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

}

Weird don't know what's going wrong, can any one help please?

Thank you.




Aucun commentaire:

Enregistrer un commentaire