如何将OnCheckedChangeListener设置为listview

时间:2017-06-13 13:22:37

标签: android listview checkbox oncheckedchanged

大家好我想在OnCheckedChangeListener文件中声明的CheckBox设置xml代表ListView的单行( item_todo。 xml ),这里是复选框的代码:

<CheckBox
    android:id="@+id/checkBox1"
    android:layout_width="40dp"
    android:layout_height="40dp"
    android:layout_gravity="center_vertical"
    android:text="" />

这里是listview代码的一部分( fragment_bacheca.xml ):

<ListView
        android:id="@+id/listView1"
        android:layout_marginTop="20dp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

然后我使用自定义适配器:`

public class TaskListAdapter extends ArrayAdapter<TaskCell>{

    public TaskListAdapter(Context context, int textViewResourceId, List<TaskCell> objects){
        super(context, textViewResourceId, objects);
    }
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        LayoutInflater inflater = (LayoutInflater) getContext()
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = inflater.inflate(R.layout.item_todo, null);
        TextView task_subject = (TextView) convertView.findViewById(R.id.task_subject);
        TextView task_type = (TextView) convertView.findViewById(R.id.task_type);
        TextView task_data = (TextView) convertView.findViewById(R.id.task_data);
        CheckBox cb = (CheckBox) convertView.findViewById(R.id.checkBox1);

        TaskCell t = getItem(position);
        task_subject.setText(t.getMateria());
        task_type.setText(t.getTipo());
        task_data.setText(t.getData());
        if(t.getFatto().equals("1")) {
            cb.setChecked(true);
            task_subject.setPaintFlags(task_subject.getPaintFlags() | Paint.STRIKE_THRU_TEXT_FLAG);
        }
        else{
            cb.setChecked(false);
        }

        return convertView;
    }

TaskCell todo_item 数据的类。

这里是我称之为监听器的片段代码:

taskView = getActivity().getLayoutInflater().inflate(R.layout.item_todo, null);
    checkbox_task = (CheckBox) taskView.findViewById(R.id.checkBox1);

    checkbox_task.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
            adapter.notifyDataSetChanged();
        }
    });

    list = new LinkedList<>();
    adapter = new TaskListAdapter(getContext(), R.layout.item_todo, list);
    lv.setAdapter(adapter);

问题是没有发生任何事情,并且通过调试我发现永远不会到达onCheckedChanged

任何人都知道为什么?谢谢大家

1 个答案:

答案 0 :(得分:0)

尝试使用ViewHolder模式实现适配器类,如下所示

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    ViewHolder holder;
    // Check if an existing view is being reused, otherwise inflate the view
    if (convertView == null) {
        convertView = LayoutInflater.from(getContext()).inflate(R.layout.item_todo, parent, false);
        holder = new ViewHolder(convertView);
        ...
        ...
        holder.cb.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                buttonView.setChecked(isChecked);
            }
        });
        ... 
        ...
        convertView.setTag(holder);
    } else {
        holder = (ViewHolder) convertView.getTag();
    }

    // Get the data item for this position
    TaskCell t = getItem(position);
    ...
    ...
    if(t.getFatto().equals("1")) {
        holder.cb.setChecked(true);
    } else {
        holder.cb.setChecked(false);
    }
    return convertView;
}

static class ViewHolder {
    ...
    ...
    CheckBox cb;

    ViewHolder(View v) {
        checkBox = (CheckBox) v.findViewById(R.id.checkBox1);
    }
}