当从ListView中删除项目时,TextView文本上的删除线仍然存在

时间:2015-05-22 00:30:21

标签: android

所以我有一个ListView,其中包含一个复选框和一个TextView。当单击它时,我成功地能够在ListView的TextView对象上删除文本,并在再次单击该对象时取消删除。但是,当我从ListView中删除一个项目时,删除线会静态地附加到该行。

有没有办法让删除线发生在行中的项而不是行本身?

这是我的代码:

public void onItemClick(AdapterView<?> parent, View view, int position, long id){
    BucketItem item = bucketList.get(position);
    TextView goal = (TextView) view.findViewById(R.id.row_text);
    if(item.isDone()){
        item.setDone(false);
        goal.setPaintFlags(goal.getPaintFlags() & ~Paint.STRIKE_THRU_TEXT_FLAG);
    }
    else{
        item.setDone(true);
        goal.setPaintFlags(goal.getPaintFlags() | Paint.STRIKE_THRU_TEXT_FLAG);
    }

    listAdapter.notifyDataSetChanged();
}

public boolean onItemLongClick(AdapterView<?> parent, View view, final int position, long id) {

    new AlertDialog.Builder(this)
        .setIconAttribute(android.R.attr.alertDialogIcon)
        .setTitle("Confirm Delete")
        .setMessage("Are you sure you want to delete this goal?")
        .setPositiveButton("Yes", new DialogInterface.OnClickListener(){
            @Override
            public void onClick(DialogInterface dialog, int which) {
                bucketList.remove(position);
                listAdapter.notifyDataSetChanged();
            }
        })
        .setNegativeButton("No", null)
        .show();

    return true;
}

适配器:

public class BucketAdapter extends ArrayAdapter<BucketItem> {

private ArrayList<BucketItem> bucketList;
private Context context;

public BucketAdapter(Context context, int textViewResourceId, ArrayList<BucketItem> bucketList) {

    super(context, textViewResourceId, bucketList);

    this.context = context;
    this.bucketList = bucketList;
}

private class ViewHolder {

    CheckBox done;
    TextView goal;
}

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

    ViewHolder holder = null;

    if (convertView == null) {

        LayoutInflater vi = (LayoutInflater) this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = vi.inflate(R.layout.row, null);

        holder = new ViewHolder();
        holder.goal = (TextView) convertView.findViewById(R.id.row_text);
        holder.done = (CheckBox) convertView.findViewById(R.id.row_check);
        convertView.setTag(holder);

        holder.done.setOnClickListener(new View.OnClickListener() {  
            public void onClick(View v) {  
                CheckBox cb = (CheckBox) v;  
                BucketItem item = (BucketItem) cb.getTag(); 
                item.setDone(cb.isChecked());
            }  
        });
    } 
    else {
        holder = (ViewHolder) convertView.getTag();
    }

    BucketItem item = bucketList.get(position);
    holder.goal.setText(item.getGoal());
    holder.done.setChecked(item.isDone());
    holder.done.setTag(item);

    return convertView;
}
}

2 个答案:

答案 0 :(得分:0)

应在适配器的getView方法中设置paint标志。然后,当您notifyDataSetChanged时,它可以根据您的支持模型确定状态。

答案 1 :(得分:0)

尝试以下适配器并查看是否有帮助

public class BucketAdapter extends ArrayAdapter<BucketItem> {

    private ArrayList<BucketItem> bucketList;
    private Context context;

    public BucketAdapter(Context context, int textViewResourceId,
            ArrayList<BucketItem> bucketList) {

        super(context, textViewResourceId, bucketList);

        this.context = context;
        this.bucketList = bucketList;
    }

    private class ViewHolder {

        CheckBox done;
        TextView goal;
    }

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

        final ViewHolder holder = null;

        if (convertView == null) {

            LayoutInflater vi = (LayoutInflater) this.context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = vi.inflate(R.layout.row, null);

            holder = new ViewHolder();
            holder.goal = (TextView) convertView
                    .findViewById(R.id.row_text);
            holder.done = (CheckBox) convertView
                    .findViewById(R.id.row_check);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        holder.done
                .setOnCheckedChangeListener(new OnCheckedChangeListener() {

                    @Override
                    public void onCheckedChanged(CompoundButton buttonView,
                            boolean isChecked) {

                        BucketItem item = (BucketItem)getItem(position);
                        item.setDone(isChecked);
                        if (isChecked) {
                            holder.goal.setPaintFlags(holder.goal.getPaintFlags() | Paint.STRIKE_THRU_TEXT_FLAG);
                        }else{
                            holder.goal.setPaintFlags(holder.goal.getPaintFlags() & ~Paint.STRIKE_THRU_TEXT_FLAG);
                        }

                    }
                });

        BucketItem item = bucketList.get(position);
        holder.goal.setText(item.getGoal());
        holder.done.setChecked(item.isDone());
        holder.done.setTag(item);

        return convertView;
    }
}

同时删除listview的onItemClickListener,OnItemLongClickListener应如下所示

        public boolean onItemLongClick(AdapterView<?> parent, View view,
                int position, long id) {
            adapter.remove((BucketItem)parent.getItemAtPosition(position));
            adapter.notifyDataSetChanged();
            return false;
        }

如果它不起作用,请告诉我。