首页 > 解决方案 > 复选框选择和取消选择

问题描述

package com.example.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.util.SparseBooleanArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.TextView;
import android.widget.Toast;

import com.example.aayushchaubey.meetdax.R;
import com.google.android.gms.tasks.Task;
import com.google.firebase.firestore.QuerySnapshot;

import java.util.ArrayList;
import java.util.HashMap;

public class DifferentGenderServicesAdapter2 extends RecyclerView.Adapter {

    private Task<QuerySnapshot> task;
    String id, servicename;
    ArrayList<String> serviceArray = new ArrayList<>();
    ArrayList<Integer> costArray = new ArrayList<>();
    ArrayList<ArrayList<String>> stylistIdArray = new ArrayList<>();
    ArrayList<Integer>durationArray=new ArrayList<>();

    TextView serviceTv, durationTv;
    int count = 0;
    String strService;
    ArrayList<String> keyArr = new ArrayList<>();
    ArrayList<String>serviceDocumentId=new ArrayList<>();
    ArrayList<String>selectedServiceId=new ArrayList<>();

    ArrayList<String>selectedServiceArray=new ArrayList<>();

    ArrayList<Integer>selectedServiceCost=new ArrayList<>();
    ArrayList<Integer>durationArr=new ArrayList<>();
    String serviceId,strStylist,strDuration;
    ArrayList<String>stylist=new ArrayList<>();
    ArrayList<ArrayList<String>>selectedStylistId=new ArrayList<>();

    public DifferentGenderServicesAdapter2(Task<QuerySnapshot> task, ArrayList<String> id, TextView servicesTv, TextView durationTv) {
        this.task = task;
        this.serviceTv = servicesTv;
        this.durationTv = durationTv;
        this.serviceDocumentId=id;
        count = task.getResult().size();
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.services_item, parent, false);
        return new DifferentGenderServicesAdapter2.listViewHolder(view);
    }

    @Override
    public void onBindViewHolder(final RecyclerView.ViewHolder holder, final int position) {
        try {
            ((listViewHolder) holder).servicenameTv.setText(task.getResult().getDocuments().get(position).get("name").toString());
            ((listViewHolder) holder).serviceCost.setText(task.getResult().getDocuments().get(position).get("price").toString());

            servicename = task.getResult().getDocuments().get(position).get("name").toString();
            Integer price = task.getResult().getDocuments().get(position).getLong("price").intValue();
            serviceArray.add(servicename);
            costArray.add(price);

            ArrayList<HashMap<String,String>>stylistArr=new ArrayList<>();
            stylistArr=(ArrayList<HashMap<String, String>>)task.getResult().getDocuments().get(position).get("services");
            ArrayList<String> stylistsId = new ArrayList<String>();

            stylistsId.clear();

            for (int i=0;i<stylistArr.size();i++){
                strStylist=stylistArr.get(i).get("stylist").toString();
                strDuration=stylistArr.get(i).get("duration").toString();
                stylistsId.add(strStylist);
                durationArray.add(Integer.parseInt(strDuration));
            }
            stylistIdArray.add(stylistsId);

            ((listViewHolder) holder).checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                    if(((listViewHolder) holder).checkBox.isChecked()){
                        String service=serviceArray.get(position);
                        selectedServiceArray.add(service);
                        serviceTv.setText(selectedServiceArray.toString().replaceAll("\\[|\\]", ""));

                    }else {
                        selectedServiceArray.remove(position);
                        if (selectedServiceArray.size()== 0) {
                            serviceTv.setText("0");
                        } else {
                            serviceTv.setText(selectedServiceArray.toString());
                        }
                    }
                }
            });

            } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public ArrayList<String> getServiceId() {
        return selectedServiceId;
    }

    public ArrayList<String> getServices() {
        return selectedServiceArray;
    }

    public Integer getCostArray() {
        Integer cost = null;
        if(selectedServiceCost.size()>1){
            cost=selectedServiceCost.get(0)+selectedServiceCost.get(1);
        }else {
            cost=selectedServiceCost.get(0);
        }
        return cost;
    }

    public Integer getDurationArr() {
        Integer duration=null;
        if(durationArr.size()>1){
            duration=durationArr.get(0)+durationArr.get(1);
        }else {
            duration=durationArr.get(0);
        }
        return duration;
    }
    public ArrayList<ArrayList<String>> getSelectedStylistId() {
        return selectedStylistId;
    }

    @Override
    public int getItemCount() {
        return count;
    }
    private class listViewHolder extends RecyclerView.ViewHolder {

        private CheckBox checkBox;
        private TextView servicenameTv, serviceCost;
        int count = 0;
        String strService;

        public listViewHolder(final View itemView) {
            super(itemView);
            try {
                servicenameTv = (TextView) itemView.findViewById(R.id.damerServiceTV);
                serviceCost = (TextView) itemView.findViewById(R.id.damerRsTV);
                checkBox = (CheckBox) itemView.findViewById(R.id.checkBox);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}

这是我用来填充数组和复选框的回收适配器。我想为选定的项目设置文本。如果我取消选择项目,我想从 textview 中删除文本。当我选择一个项目时,选择和取消选择工作正常。如果我有更多多于一个,或者如果我选择并取消选择多次它不起作用给出索引错误

标签: javaandroid

解决方案


像这样做 : -

if(checkbox.isChecked){
    String service=serviceArray.get(getAdapterPosition)
    selectedServiceArray.add(service);
    holder.serviceNameTv.setText(selectedServiceArray.toString);
}
else{
    selectedServiceArray.remove(getAdapterPosition);
if (selectedServiceArray.size = 0) {
 holder.serviceNameTv.setText("0");
} else{
 holder.serviceNameTv.setText(selectedServiceArray.toString);
}

推荐阅读