尝试使用自定义适配器在自定义待办事项列表中实现 CheckBox 时遇到问题?

Having problems while trying to implement a CheckBox in a custom todolist using a custom adapter?

我正在通过可编辑的文本向列表视图添加数据,列表视图是定制的,每一行都包含一个文本视图和一个复选框,现在我希望每当我点击每行的复选框时显示提示消息我正在自定义适配器上实现 View.OnClickListener 并通过 OnClick 方法设置 if else 条件,以便可以显示我的 toast 消息,但这里的问题是在填充列表视图时复选框不起作用总之,我认为问题可能是 convertview 可能膨胀不正确或复选框可能使用错误的视图,有人可以帮我解决这个问题吗?

public class todoFragment extends ListFragment{

private EditText mToDoField;
private Button mAdd;
UsersAdapter mAdapter;
private TextView todoTextView;
private CheckBox todoCheckBox;


@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    getActivity().setTitle(R.string.todo_title);
}

public class UsersAdapter extends ArrayAdapter<String> implements View.OnClickListener{

    public Context context;
    public ArrayList<String> values;

    public UsersAdapter(Context context, ArrayList<String> values) {
        super(context, 0, values);

        this.context = context;
        this.values = values;
    }

   @Override
    public void onClick(View view) {

        todoCheckBox.setOnClickListener(this);

        if (todoCheckBox.isChecked()){
            Toast.makeText(getContext(), "CheckBox is clicked.", Toast.LENGTH_LONG).show();
        }
        else{
            Toast.makeText(getContext(), "CheckBox is not clicked.", Toast.LENGTH_LONG).show();
        }
    }

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

        LayoutInflater inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        convertView = LayoutInflater.from(getContext()).inflate(R.layout.todo_list, parent, false);

        todoTextView = (TextView) convertView.findViewById(R.id.todo_TextView);
        todoCheckBox = (CheckBox) convertView.findViewById(R.id.todo_CheckBox);

        todoTextView.setText(values.get(position));

        return convertView;
    }
}



@TargetApi(9) // remember this for isEmpty()
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View v = inflater.inflate(R.layout.fragment_todo, container, false);

    ArrayList<String> todoList = new ArrayList<String>();
    mAdapter = new UsersAdapter(getActivity(), todoList);
    ListView listViewToDo = (ListView) v.findViewById (android.R.id.list);
    listViewToDo.setAdapter(mAdapter);

    mToDoField = (EditText) v.findViewById(R.id.todo_editText);

    mAdd = (Button)v.findViewById(R.id.add_button);
    mAdd.setOnClickListener(new View.OnClickListener() {
        @Override
            public void onClick(View view) {

            String toDo = mToDoField.getText().toString().trim();

            if (toDo.isEmpty()){

            }

            mAdapter.add(toDo);

            mToDoField.setText("");
        }
    });

    return v;
}

我建议您在 Adapter class 的 getView() 中实现 setOnCheckedChangeListener,在其中为 ListRow 定义 CheckBox。

喜欢,(可能是下面代码中的代码格式错误)

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

        LayoutInflater inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        convertView = LayoutInflater.from(getContext()).inflate(R.layout.todo_list, parent, false);

        todoTextView = (TextView) convertView.findViewById(R.id.todo_TextView);
        todoCheckBox = (CheckBox) convertView.findViewById(R.id.todo_CheckBox);

        todoTextView.setText(values.get(position));

        todoCheckBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

        @Override
        public void onCheckedChanged(CompoundButton buttonView,boolean isChecked)                                                                                                      {
          Toast.makeText(getContext(), position+" CheckBox Status: "+isChecked, Toast.LENGTH_LONG).show();
        }
       }
     );     
        return convertView;
    }
}

并从适配器中删除 public void onClick(View view)

我认为以下两件事应该可以帮助您使复选框可点击

->>在todo_list布局的父ViewGroup中添加android:descendantFocusability="blocksDescendants"

->> 在您的 todo_CheckBox 视图定义中添加 android:clickable="true"

一次,您可以开始看到点击屏幕上的复选框,因为 user370305 正确地提到了复选框上的检查是使用 OnCheckedChangeListener 而不是 OnCliclListener