大家好,我在将删除线添加到textview时遇到问题。这就是我现在正在做的:

  private void initControls() {

    tvNote = (TextView) findViewById (R.id.textView1);
    tvNote.setVisibility(View.GONE);

    fillData();

}

private void fillData() {

      dbConnect();

      // Array list of sync data items
      ArrayList<SyncDataItems> itemList = new ArrayList<SyncDataItems>();

      List<Todos> todos = dbHelper.getAllTodos();

      SyncDataItems _items;

      for (Todos cn : todos) {
          String log = "ID: " + cn.getID() + " , Todo: " + cn.getTodo();
          Log.e("Todo List ", log);
          _items = new SyncDataItems(cn.getTodo(), cn.getTodo(), true);
          itemList.add(_items);
      }

      dataAdapter = new MyCustomAdapter(this, R.layout.item_info, itemList);
      listView = (ListView) findViewById(R.id.listView1);
      // Assign adapter to ListView
      listView.setAdapter(dataAdapter);

      if(dataAdapter.getCount() == 0) {
          tvNote.setVisibility(View.VISIBLE);
          tvNote.setText("No Records Yet");
      }


}

  private class MyCustomAdapter extends ArrayAdapter<SyncDataItems> {

      private ArrayList<SyncDataItems> itemList;

      public MyCustomAdapter(Context context, int textViewResourceId,

      ArrayList<SyncDataItems> itemList) {
       super(context, textViewResourceId, itemList);
           this.itemList = new ArrayList<SyncDataItems>();
           this.itemList.addAll(itemList);
      }

      private class ViewHolder {
           TextView code;
           CheckBox name;
      }

      @Override
      public View getView(int position, View convertView, ViewGroup parent) {

            holder = null;

            Log.v("ConvertView", String.valueOf(position));

            if (convertView == null) {

                LayoutInflater vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);

                convertView = vi.inflate(R.layout.item_info, null);

                holder = new ViewHolder();
                holder.code = (TextView) convertView.findViewById(R.id.code);
                holder.name = (CheckBox) convertView.findViewById(R.id.checkBox1);


                convertView.setTag(holder);

                holder.name.setOnClickListener(new View.OnClickListener() {
                    public void onClick(View v) {
                        CheckBox cb = (CheckBox) v;
                        SyncDataItems _item = (SyncDataItems) cb.getTag();

                        _item.setSelected(cb.isChecked());


                        holder.name.setOnCheckedChangeListener(new OnCheckedChangeListener() {
                            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {

                                if ( isChecked ) {
                                    Log.e("TAG", "Checked!");
                                    holder.code.append("Checked!");
                                    holder.code.setPaintFlags(Paint.STRIKE_THRU_TEXT_FLAG | Paint.ANTI_ALIAS_FLAG);
                                } else {
                                    holder.code.setPaintFlags(holder.code.getPaintFlags() & (~ Paint.STRIKE_THRU_TEXT_FLAG));
                                    Log.e("TAG", "Unchecked!");
                                }
                            }
                        });

                    }
                });

            } else {
                holder = (ViewHolder) convertView.getTag();
            }

            final SyncDataItems item = itemList.get(position);

            holder.code.setText(item.getItem());
            holder.name.setChecked(item.isSelected());

            holder.name.setTag(item);



            return convertView;
        }

}


但是,当我选中或取消选中复选框时,删除线效果不起作用。有任何想法吗?您的帮助将不胜感激。谢谢。

最佳答案

您可以使用 :

holder.code.setPaintFlags(tv.getPaintFlags() & (~ Paint.STRIKE_THRU_TEXT_FLAG));

10-08 19:34