简体   繁体   English

复选框取消选中,列表视图的行值不会更新

[英]checkbox uncheck and row values of listview are not updated

Confusing at it may seem, I am confused of what is happening too. 看起来很困惑,我对发生的事情感到困惑。 I have an application in which there is a listview that displays values and when a row is pressed, the third value of the row will display a value. 我有一个应用程序,其中有一个显示值的列表视图,当按下一行时,该行的第三个值将显示一个值。

Example is: third value is 30 and when it's pressed, it should be divided by 6, so the answer should be 5. 例如:第三个值是30,当它被按下时,应该除以6,所以答案应该是5。

But when I scroll in the listview, the checkbox becomes unchecked and the third value of the row goes back to its old value (30). 但是当我在列表视图中滚动时,复选框变为未选中状态,行的third value of the row将返回其旧值(30).

Is there any way to keep the checkbox from being checked and the value of the row preserved when clicked? 是否有任何方法可以防止选中复选框,并在单击时保留行的值?

Here's the snippet of my code. 这是我的代码片段。

listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @RequiresApi(api = Build.VERSION_CODES.N)
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            CheckBox checkBox = (CheckBox)view.findViewById(R.id.checkmark);
            TextView tv3 = (TextView)view.findViewById(R.id.tx_counter);
            EditText editText = (EditText)findViewById(R.id.editText3);
            String yy = editText.getText().toString().trim();
            String shitts = listView.getItemAtPosition(position).toString();
            try {
                String[] a = shitts.split(", ");
                String[] b = a[1].split("=");
                String[] sep = a[0].split("=");
                String betnumber = sep[1];
                String betamount= b[1];
                if (view != null) {
                    checkBox.setChecked(!checkBox.isChecked());
                    if(checkBox.isChecked()){
                        //sort number
                        final String sorted = betnumber.chars().sorted().mapToObj(c -> Character.valueOf((char)c).toString()).collect(Collectors.joining());
                        System.out.println(sorted);
                        //check if double digit
                        Boolean checker = doubleChecker(sorted);
                        if (checker == true){
                            Toast.makeText(getApplicationContext(),"DOUBLE DIGIT", LENGTH_SHORT).show();
                            int answer = Integer.parseInt(betamount) / 3;
                            tv3.setText(String.valueOf(answer));
                        }else{
                            Toast.makeText(getApplicationContext(),"NOT DOUBLE DIGIT", LENGTH_SHORT).show();
                            int answer;
                            if(yy.equals("")){
                                 answer = Integer.parseInt(betamount) / 6;
                                 tv3.setText(String.valueOf(answer));
                            }else{
                                answer = (Integer.parseInt(betamount) - Integer.parseInt(yy)) / 6;
                                tv3.setText(String.valueOf(answer));
                            }
                        }
                        //TODO save to array to send

                    }else{
                        //TODO mistake RETURN tv3 to old value
                    }
                }
            }catch (Exception e){
            }
        }
    });

Here's my adapter. 这是我的适配器。

    class MyAdapter extends BaseAdapter {
    private ArrayList<HashMap<String, String>> mData;
    public MyAdapter(ArrayList<HashMap<String, String>> mData2) {
        this.mData = mData2;
    }
    @Override
    public int getCount() {
        return mData.size();
    }
    @Override
    public Object getItem(int i) {
        return this.mData.get(i);
    }
    @Override
    public long getItemId(int i) {
        return i;
    }
    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        view = getLayoutInflater().inflate(R.layout.row_layout, null);
        TextView tx_number = (TextView) view.findViewById(R.id.tx_number);
        TextView tx_amount = (TextView) view.findViewById(R.id.tx_amount);
        TextView tx_counter = (TextView) view.findViewById(R.id.tx_counter);
        String betid = mData.get(i).get("betid");
        if(betid!=null){
            String betnumber = mData.get(i).get("betnumber");
            String amountTarget = mData.get(i).get("amountTarget");
            String amountRamble = mData.get(i).get("amountRamble");
            tx_number.setText(betnumber);
            tx_amount.setText(amountTarget);
            tx_counter.setText(amountRamble);
        }
        return view;
    }
}

Replace your adapter code with: 将适配器代码替换为:

public class MyAdapter extends BaseAdapter {
    private ArrayList<HashMap<String, String>> mData;

    public MyAdapter(ArrayList<HashMap<String, String>> mData2) {
        this.mData = mData2;
    }

    @Override
    public int getCount() {
        return mData.size();
    }

    @Override
    public Object getItem(int i) {
        return this.mData.get(i);
    }

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

    @Override
    public View getView(int i, View convertView, ViewGroup viewGroup) {
        View mView = convertView;

        String betid = mData.get(i).get("betid");

        ViewHolder holder ;

        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);
            mView.setTag(holder);
        } else {
            holder = (ViewHolder) mView.getTag();
        }


        if (betid != null) {
            String betnumber = mData.get(i).get("betnumber");
            String amountTarget = mData.get(i).get("amountTarget");
            String amountRamble = mData.get(i).get("amountRamble");
            holder.tx_number.setText(betnumber);
            holder.tx_amount.setText(amountTarget);
            holder.tx_counter.setText(amountRamble);
        }
        return mView;
    }

    private static class ViewHolder {
        TextView tx_number;
        TextView tx_amount;
        TextView tx_counter;
    }
}

I added new code with ViewHolder . 我在ViewHolder添加了新代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM