我有一个recyclerview和多个复选框

vxf3dgd4  于 2021-07-08  发布在  Java
关注(0)|答案(1)|浏览(471)

我有一个recyclerview,有多个带有日期的复选框,如果我单击10-05-2020复选框,则要选中该复选框,然后要选中所有其他小于给定日期的复选框,如10-01-2020、10-02-2020、10-03-2020等等。请帮帮我,我是新来的安卓。
公共类paymentadapter扩展了recyclerview.adapter<paymentadapter.viewholder>{

Context context;
private List<DataModel> ListTerbaru;
private List<String> duedateList;
private List<DateModel> multiProdList;
TextView paidAmountTv;
static float totalAmount = 0.00f, subtotalAmount = 0.00f;

private Calendar calendar;
private SimpleDateFormat dateFormat;
private String date;

Set<String> duplicateList;
HashSet<String> hashSet = new HashSet<String>();

int j = 0;

static ArrayList<String> pfd_sidlist = new ArrayList<>();
static ArrayList<Integer> adapter_positionlist = new ArrayList<>();
public static ArrayList<String> comp_idlist = new ArrayList<>();
public static ArrayList<String> component_namelist = new ArrayList<>();
public static ArrayList<String> fee_typelist = new ArrayList<>();
public static ArrayList<String> fee_durationlist = new ArrayList<>();
public static ArrayList<String> deferred_amountlist = new ArrayList<>();
public static ArrayList<String> prog_idlist = new ArrayList<>();
public static ArrayList<String> program_namelist = new ArrayList<>();
public static ArrayList<String> assign_idlist = new ArrayList<>();
public static ArrayList<String> plan_idlist = new ArrayList<>();
public static ArrayList<String> plan_namelist = new ArrayList<>();
public static ArrayList<String> account_bucketlist = new ArrayList<>();
public static ArrayList<String> amount_tlist = new ArrayList<>();
public static ArrayList<String> multi_amount_tlist = new ArrayList<>();
public static ArrayList<Integer> indexList = new ArrayList<>();
List<Boolean> isselectList;
List<String> amountList;
private int loop_counter;

String previousDate = "";
private static String pfd_sid, base_prodid, comp_id, component_name, fee_type, fee_duration, due_date, deferred_amount, prog_id, program_name, assign_id, plan_id, plan_name, account_bucket, amount_t, mAmount, payNextdueFee, payOverDueFee;

public PaymentAdapter(Context context, List<DataModel> ListTerbaru, TextView paidAmountTv, List<String> duedateList, List<Boolean> isselectList, List<String> amountList, String payNextdueFee, String payOverDueFee
        , int loop_counter, List<DateModel> multiProdList) {
    this.context = context;
    this.ListTerbaru = ListTerbaru;
    this.paidAmountTv = paidAmountTv;
    this.duedateList = duedateList;
    this.isselectList = isselectList;
    this.amountList = amountList;
    totalAmount = 0.00f;
    PaymentAdapter.payOverDueFee = payOverDueFee;
    PaymentAdapter.payNextdueFee = payNextdueFee;
    this.multiProdList = multiProdList;
    this.loop_counter = loop_counter;
    duplicateList = new HashSet<String>();
    hashSet.addAll(duedateList);

    pfd_sidlist.clear();
    comp_idlist.clear();
    component_namelist.clear();
    fee_typelist.clear();
    fee_durationlist.clear();
    deferred_amountlist.clear();
    prog_idlist.clear();
    program_namelist.clear();
    assign_idlist.clear();
    plan_idlist.clear();
    plan_namelist.clear();
    account_bucketlist.clear();
    amount_tlist.clear();
    multi_amount_tlist.clear();
    indexList.clear();
    adapter_positionlist.clear();

    if (loop_counter == 1) {
        j = 0;
    }
}

@RequiresApi(api = Build.VERSION_CODES.N)
@Override
public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {

    View view;
    view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.cards_layout, viewGroup, false);

    Collections.sort(duedateList);
    for (String date : duedateList) {
        Log.d("selectedDueDate", "all due date selected " + date);
        if (!getCurrentDate().contains(date)) {
            Log.d("DeepakSHukla", "This is Deepak Shukla From CoolG");
        }
    }
    try {
        Log.d("DeepakSHukla", "UNix time stamp " + convertDateToUnixTimeStamp());
    } catch (Exception e) {
        e.printStackTrace();
    }
    for (String date : duedateList) {
        Log.d("DeepakSHuklaMkp", "UNix time stamp " + changeDateToUnix(date));

    }
    return new VHItem(view);
}

public class ViewHolder extends RecyclerView.ViewHolder {
    public ViewHolder(View view) {
        super(view);

    }
}

public String getCurrentDate() {
    calendar = Calendar.getInstance();

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
        dateFormat = new SimpleDateFormat("dd/MM/yyyy");
        date = dateFormat.format(calendar.getTime());
        //Log.d("CurrentDateIsEqual","CurrentDate "+date);
    }
    return date;
}

@TargetApi(Build.VERSION_CODES.N)
@Override
public void onBindViewHolder(final ViewHolder viewHolder, final int i) {
    final DataModel clickedDataItem = ListTerbaru.get(i);
    Log.e("TAG", "date" + base_prodid + "====" + payOverDueFee);
    // Log.e("Response","multiProdList==="+multiProdList.size());
    paidAmountTv.setText("" + FeePaymentActivity.totalAmount);

    final Calendar c = Calendar.getInstance();
    SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");

    if (clickedDataItem != null) {

        ((VHItem) viewHolder).top.setVisibility(clickedDataItem.getVisibilty());
        ((VHItem) viewHolder).dueDateText.setText(clickedDataItem.getDueDate());
        // ((VHItem) viewHolder).academicYear.setText(clickedDataItem.getFeeDuration());

        for (String listDate : duedateList) {
           /* Log.d("DeepakSHuklaMkp", "UNix time stamp " + changeDateToUnix(listDate));
            Log.d("listDate", "UNix time stamp " + listDate);

            if (convertDateToUnixTimeStamp() > changeDateToUnix(((VHItem) viewHolder).dueDateText.getText().toString().trim())) {

                if (!((VHItem) viewHolder).dueDateText.getText().toString().trim().equals(getCurrentDate())) {
                    ((VHItem) viewHolder).checkbox.setChecked(true);
                    ((VHItem) viewHolder).checkbox.setEnabled(false);
                    FeePaymentActivity.totalAmount = FeePaymentActivity.totalAmount + Float.parseFloat(clickedDataItem.getBalanceAmount());

                    paidAmountTv.setText("" + FeePaymentActivity.totalAmount);
                }
            }*/

            ((VHItem) viewHolder).componentName.setText(clickedDataItem.getComponentName() + " (" + clickedDataItem.getFeeType() + ") - ");
            ((VHItem) viewHolder).balanceAmount.setText("₹ " + clickedDataItem.getBalanceAmount());

            if (clickedDataItem.getSelected()) {

                ((VHItem) viewHolder).checkbox.setChecked(true);

                ((VHItem) viewHolder).checkbox.setEnabled(false);
                ((VHItem) viewHolder).balanceAmount.setTextColor(Color.parseColor("#05B313"));
                ((VHItem) viewHolder).top.setBackgroundResource(R.drawable.btncorner);

                pfd_sid = clickedDataItem.getPfdSid();
                comp_id = clickedDataItem.getCompId();
                component_name = clickedDataItem.getComponentName();
                fee_type = clickedDataItem.getFeeType();
                fee_duration = clickedDataItem.getFeeDuration();
                // due_date = dateModelItem.getDueDate();
                deferred_amount = clickedDataItem.getDeferredAmount();
                prog_id = clickedDataItem.getProgId();
                program_name = clickedDataItem.getProgramName();
                assign_id = clickedDataItem.getAssignId();
                plan_id = clickedDataItem.getPlanId();
                plan_name = clickedDataItem.getPlanName();
                account_bucket = clickedDataItem.getAccountBucket();
                amount_t = clickedDataItem.getBalanceAmount();

                if (j >= 0) {
                    for (int z = 0; z < loop_counter; z++) {
                        if (j == loop_counter) {

                        } else {
                            indexList.add(j);
                            onSelectedValue(j);
                            j++;

                        }
                    }

                }

            }

            ((VHItem) viewHolder).checkbox.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    int pos = ((VHItem) viewHolder).getAdapterPosition();
                    Log.d("selectedDueDate", "due date selected " + clickedDataItem.getDueDate());
                    Log.d("selectedDueDate", "due date selected hhhh " + duedateList.get(pos));
                    Collections.sort(duedateList);
                    for (String date : duedateList) {
                        String monthYear = date.substring(3);
                        Log.d("selectedDueDatesss", "all due date selected monthYear " + monthYear + "pos " + pos);

                        if (ListTerbaru.get(pos).getDueDate().contains(date)) {
                            Log.d("selectedDueDate", "true ");

                        }
                    }
                    if (i != 0) {
                        for (int j = 0; j < ListTerbaru.size(); j++) {
                            Log.d("selectedDueDatenn", "value of j " + ListTerbaru.get(j));

                        }
                    }

                    if (((VHItem) viewHolder).checkbox.isChecked()) {
                        ((VHItem) viewHolder).balanceAmount.setTextColor(Color.parseColor("#05B313"));
                        ((VHItem) viewHolder).top.setBackgroundResource(R.drawable.btncorner);
                        FeePaymentActivity.totalAmount = FeePaymentActivity.totalAmount + Float.parseFloat(clickedDataItem.getBalanceAmount());

                        paidAmountTv.setText("" + FeePaymentActivity.totalAmount);

                        pfd_sid = clickedDataItem.getPfdSid();
                        comp_id = clickedDataItem.getCompId();
                        component_name = clickedDataItem.getComponentName();
                        fee_type = clickedDataItem.getFeeType();
                        fee_duration = clickedDataItem.getFeeDuration();
                        deferred_amount = clickedDataItem.getDeferredAmount();
                        prog_id = clickedDataItem.getProgId();
                        program_name = clickedDataItem.getProgramName();
                        assign_id = clickedDataItem.getAssignId();
                        plan_id = clickedDataItem.getPlanId();
                        plan_name = clickedDataItem.getPlanName();
                        account_bucket = clickedDataItem.getAccountBucket();
                        amount_t = clickedDataItem.getBalanceAmount();

                        pfd_sidlist.add(pfd_sid);
                        comp_idlist.add(comp_id);
                        component_namelist.add(component_name);
                        fee_typelist.add(fee_type);
                        fee_durationlist.add(fee_duration);
                        deferred_amountlist.add(deferred_amount);
                        prog_idlist.add(prog_id);
                        program_namelist.add(program_name);
                        assign_idlist.add(assign_id);
                        plan_idlist.add(plan_id);
                        plan_namelist.add(plan_name);
                        account_bucketlist.add(account_bucket);
                        amount_tlist.add(amount_t);
                        adapter_positionlist.add(Integer.parseInt(pfd_sid));
                        indexList.add(i);

                        Log.e("ArrayList isChecked=", "Componet_namelist====" + adapter_positionlist.toString() + "totalAmount==" + amount_tlist.toString() + "position==" + pos);

                    } else {
                        ((VHItem) viewHolder).balanceAmount.setTextColor(Color.parseColor("#2E2E2D"));
                        ((VHItem) viewHolder).top.setBackgroundResource(R.drawable.gradientbtn);
                        FeePaymentActivity.totalAmount = FeePaymentActivity.totalAmount - Float.parseFloat(clickedDataItem.getBalanceAmount());
                        paidAmountTv.setText("" + FeePaymentActivity.totalAmount);

                        pfd_sid = clickedDataItem.getPfdSid();

                        int adapterValue = adapter_positionlist.indexOf(Integer.parseInt(pfd_sid));

                        pfd_sidlist.remove(adapterValue);
                        comp_idlist.remove(adapterValue);
                        component_namelist.remove(adapterValue);
                        fee_typelist.remove(adapterValue);
                        fee_durationlist.remove(adapterValue);
                        deferred_amountlist.remove(adapterValue);
                        prog_idlist.remove(adapterValue);
                        program_namelist.remove(adapterValue);
                        assign_idlist.remove(adapterValue);
                        plan_idlist.remove(adapterValue);
                        plan_namelist.remove(adapterValue);
                        account_bucketlist.remove(adapterValue);
                        adapter_positionlist.remove(adapterValue);
                        amount_tlist.remove(adapterValue);

                        Log.e("ArrayList uncheck=", "Componet_namelist====" + adapter_positionlist.toString() + "totalAmount==" + amount_tlist.toString() + "position==" + pos);

                    }
                }
            });
        }
    }

}

public void onSelectedValue(int j) {
    try {
        pfd_sidlist.add(FeePaymentActivity.pfd_sidlist.get(j));
        int autoselected = Integer.parseInt(FeePaymentActivity.pfd_sidlist.get(j));
        adapter_positionlist.add(autoselected);
        comp_idlist.add(FeePaymentActivity.comp_idlist.get(j));
        component_namelist.add(FeePaymentActivity.component_namelist.get(j));
        fee_typelist.add(FeePaymentActivity.fee_typelist.get(j));
        fee_durationlist.add(FeePaymentActivity.fee_durationlist.get(j));
        deferred_amountlist.add(FeePaymentActivity.deferred_amountlist.get(j));
        prog_idlist.add(FeePaymentActivity.prog_idlist.get(j));
        program_namelist.add(FeePaymentActivity.program_namelist.get(j));
        assign_idlist.add(FeePaymentActivity.assign_idlist.get(j));
        plan_idlist.add(FeePaymentActivity.plan_idlist.get(j));
        plan_namelist.add(FeePaymentActivity.plan_namelist.get(j));
        account_bucketlist.add(FeePaymentActivity.account_bucketlist.get(j));
        amount_tlist.add(FeePaymentActivity.amount_tlist.get(j));

        Log.e("ArrayList Selected=", "Componet_namelist====" + adapter_positionlist.toString() + "totalAmount==" + FeePaymentActivity.account_bucketlist.toString());
    } catch (Exception e) {
        e.printStackTrace();
    }

}

private void getChar() {
    int charValue = previousDate.charAt(0);
    previousDate = String.valueOf((char) (charValue + 1));
}

@Override
public long getItemId(int position) {
    return position;
}

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

}

@Override
public int getItemViewType(int position) {
    return position;

}

@TargetApi(Build.VERSION_CODES.N)
private class VHItem extends ViewHolder {
    TextView componentName, balanceAmount, dueDateText, academicYear;
    public CheckBox checkbox;
    RelativeLayout top;
    public int position = 0;

    VHItem(View v) {
        super(v);

        dueDateText = v.findViewById(R.id.due_date);
        academicYear = v.findViewById(R.id.academic_year);

        componentName = v.findViewById(R.id.textViewVersion);
        balanceAmount = v.findViewById(R.id.amount);
        top = v.findViewById(R.id.top);

        checkbox = v.findViewById(R.id.check_box);

    }

}

@RequiresApi(api = Build.VERSION_CODES.N)
private long convertDateToUnixTimeStamp() {
    long unixTimeStamp = 0;
    final java.text.SimpleDateFormat dateFormat = new java.text.SimpleDateFormat("MM-yyyy", Locale.getDefault());
    String currentDate = dateFormat.format(new Date());
    Log.d("TimeStampee", "Current Date = " + currentDate);
    try {
        DateFormat formatter = new java.text.SimpleDateFormat("MM-yyyy", Locale.getDefault());
        Date date = (Date) formatter.parse(currentDate);
        assert date != null;
        unixTimeStamp = date.getTime();
        Log.d("TimeStamp", "long unixTimeStamp = " + unixTimeStamp);
        Log.d("TimeStamp", "TimeStamp = " + date.getTime());
    } catch (ParseException e) {
        e.printStackTrace();
        int i = e.getErrorOffset();
        Log.d("TimeStamp", "ParseException = " + i);
    }

    return unixTimeStamp;
}

public long changeDateToUnix(String date) {
    long unixTimeStamp = 0;
    try {
        DateFormat formatter = new java.text.SimpleDateFormat("MM-yyyy", Locale.getDefault());

        Date newDate = (Date) formatter.parse(date);
        assert newDate != null;
        unixTimeStamp = newDate.getTime();
        Log.d("TimeStamp", "long unixTimeStamp = " + unixTimeStamp);
        Log.d("TimeStamp", "TimeStamp = " + newDate.getTime());
    } catch (ParseException e) {
        e.printStackTrace();
        int i = e.getErrorOffset();
        Log.d("TimeStamp", "ParseException = " + i);
    }

    return unixTimeStamp;
}

}

nmpmafwu

nmpmafwu1#

你的适配器类相当混乱,所以我建议用我的方式来解决。我们的想法是使用 field 按住选定的 date ,然后在 onBindViewHolder .

private long selectedDateUnix;

 @Override
public void onBindViewHolder(CategoryViewHolder holder, int position) {
    ...
    holder.chDate.setChecked(item.getDate()<=selectedDateUnix);

}

现在每次 checkBox 的选中状态更改,更改 selectedDateUnix 相应地打电话 notifyDataSetChanged() .

class MyViewHolder extends RecyclerView.ViewHolder {
    public CheckBox checkBox;
    ...
    public CategoryViewHolder(View view) {
        super(view);
        ...
        checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                if (b) {
                    selectedDateUnix = items.get(getAdapterPosition()).getDate();
                    notifyDataSetChanged();
                } else {
                    // add the proper logic
                }
            }
        });
    }
}

相关问题