如何使用CheckBox在Listview中实现删除线文本?

时间:2019-03-21 13:28:13

标签: android-listview android-checkbox strikethrough

我有一个带有CheckBox和TextView的ListView。但是,当我单击“复选框”时,无法获得删除线文本。但是,当我单击最后一个复选框时,会出现删除线文本。 enter image description here

  

这是我的GetView:

@Override
    public View getView(int position, View convertView, ViewGroup parent) {
         ViewHolder listViewHolder;
        if(convertView == null){
            listViewHolder = new ViewHolder();
            convertView = lInflater.inflate(R.layout.listview_with_checkbox, parent, false);

            listViewHolder.textInListView = (TextView)convertView.findViewById(R.id.textView);
            listViewHolder.checkBox = (CheckBox)convertView.findViewById(R.id.checkBox);
            convertView.setTag(listViewHolder);
        }else{
            listViewHolder = (ViewHolder)convertView.getTag();

        }
        listViewHolder.textInListView.setText(listStorage.get(position).getName());
        listViewHolder.checkBox.setChecked(false);
        listViewHolder.checkBox.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (listViewHolder.checkBox.isChecked()){
                    listViewHolder.textInListView.setPaintFlags(listViewHolder.textInListView.getPaintFlags() | Paint.STRIKE_THRU_TEXT_FLAG);
                    listViewHolder.checkBox.setChecked(true);


                }else{
                    listViewHolder.checkBox.setChecked(false);

                }
            }
        });

        return convertView;
    }
  

这是我的主语

ListView listViewWithCheckBox = (ListView)RootView1.findViewById(R.id.listView);
        List<AimsDiaryCheckBoxBolvanka> listViewItems = new ArrayList<AimsDiaryCheckBoxBolvanka>();

        listViewItems.add(new AimsDiaryCheckBoxBolvanka("Nigeria"));
        listViewItems.add(new AimsDiaryCheckBoxBolvanka("Ghana"));
        listViewItems.add(new AimsDiaryCheckBoxBolvanka("Senegal"));
        listViewItems.add(new AimsDiaryCheckBoxBolvanka("OPA"));
        listViewItems.add(new AimsDiaryCheckBoxBolvanka("KNIGA"));
        listViewWithCheckBox.setAdapter(new AimsDiaryCheckBoxAdapter(getContext(), listViewItems));
        listViewWithCheckBox.setOnItemClickListener(new AdapterView.OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
// make Toast when click
                Toast.makeText(getActivity().getApplicationContext(), "Position " + position, Toast.LENGTH_LONG).show();
            }
        });

1 个答案:

答案 0 :(得分:0)

我认为这与以下几行有关:

ggproto

在实际调用listViewHolder.checkBox.setChecked(true)之前,先看看listViewHolder指向什么,并且正在执行isChecked()。看起来像是时间问题