复选框上的Listview滚动问题

时间:2019-07-02 08:56:52

标签: java android checkbox android-listview android-checkbox

我的listview包含复选框存在问题,当未选中复选框且视图被回收时,选中的复选框将变为选中状态。有什么帮助吗?我见过很多相同的话题,但似乎找不到我的问题的答案。谢谢。

这是getView的代码。

public View getView(int i, View convertView, ViewGroup viewGroup) {
        View mView = convertView;
        String betid = mData.get(i).get("betid");
        ViewHolder holder ;
        ListView listview = findViewById(R.id.lvMain);
        if (mView == null) {
            Context context = viewGroup.getContext();
            LayoutInflater inflater = LayoutInflater.from(context);
            mView = inflater.inflate(R.layout.row_layout, null,false);
            holder = new ViewHolder();
            holder.tx_number = (TextView) mView.findViewById(R.id.tx_number);
            holder.tx_amount = (TextView) mView.findViewById(R.id.tx_amount);
            holder.tx_counter = (TextView) mView.findViewById(R.id.tx_counter);
            holder.checkBox = (CheckBox) mView.findViewById(R.id.checkmark);
            holder.checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                @RequiresApi(api = Build.VERSION_CODES.N)
                @Override
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                    String[] b;
                    if (buttonView.isChecked()) {
                        EditText xxx = (EditText)findViewById(R.id.editText);
                        String amtX = xxx.getText().toString();
                        int toMultiply;
                        if(amtX.equals("")){
                            toMultiply = 1;
                        }else{
                            toMultiply = Integer.parseInt(amtX);
                        }
                        System.out.println(toMultiply);
                        String yy = editText.getText().toString().trim();
                        checked.add((Integer) holder.checkBox.getTag());
                        String item = listview.getItemAtPosition(i).toString();
                        String[] a = item.split(", ");
                        b = a[1].split("=");
                        String[] sep = a[0].split("=");
                        String betnumber = sep[1];
                        String betamount= b[1];
                        final String sorted = betnumber.chars().sorted().mapToObj(c -> Character.valueOf((char)c).toString()).collect(Collectors.joining());
                        if (doubleChecker(sorted)){
                            answer = (Integer.parseInt(betamount) * toMultiply / 3);
                            holder.tx_counter.setText(valueOf(answer));
                        }else{
                            if(yy.equals("")){
                                answer = (Integer.parseInt(betamount) * toMultiply / 6);
                                holder.tx_counter.setText(valueOf(answer));
                            }else{
                                answer = ((Integer.parseInt(betamount) * toMultiply -  Integer.parseInt(yy)) / 6);
                                holder.tx_counter.setText(valueOf(answer));
                            }
                        }
                        holder.tx_counter.setBackgroundColor(getResources().getColor(R.color.bluelim));
                        holder.tx_amount.setBackgroundColor(getResources().getColor(R.color.bluelim));
                        holder.tx_number.setBackgroundColor(getResources().getColor(R.color.bluelim));
                    }
                    else {
                        holder.tx_counter.setBackgroundColor(Color.WHITE);
                        holder.tx_amount.setBackgroundColor(Color.WHITE);
                        holder.tx_number.setBackgroundColor(Color.WHITE);
                        holder.tx_counter.setText("0");
                        checked.remove((Integer) holder.checkBox.getTag());
                    }
                }
            });
            holder.checkBox.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (((CheckBox) v).isChecked()) {
                        holder.checkBox.setChecked(true);
                    }else{
                        holder.checkBox.setChecked(false);
                    }
                }
            });
            mView.setTag(holder);
            holder.checkBox.setTag(i);
        } else {
            holder = (ViewHolder) mView.getTag();
            ((ViewHolder)mView.getTag()).checkBox.setTag(i);
        }
        if (betid != null) {
            String betnumber = mData.get(i).get("betnumber");
            String amountTarget = mData.get(i).get("amountTarget");
            holder.tx_amount.setText(amountTarget);
            holder.tx_number.setText(betnumber);
            holder.tx_counter.setText("0");
        }
        ViewHolder holde2r = (ViewHolder) mView.getTag();
        for (int k = 0; k < checked.size(); k++) {
            if (checked.get(k) == i) {
                holde2r.checkBox.setChecked(true);
            }
            else if (checked.get(k) != i) {
                holde2r.checkBox.setChecked(false);
            }
        }
        return mView;
    }
    private class ViewHolder {
        TextView tx_number;
        TextView tx_amount;
        TextView tx_counter;
        CheckBox checkBox;
    }
}

很抱歉,冗长的代码,我只是认为需要将其放在此处,因为我不知道是什么原因引起的。

0 个答案:

没有答案