【问题标题】:Having problems while trying to implement a CheckBox in a custom todolist using a custom adapter?尝试使用自定义适配器在自定义 todolist 中实现 CheckBox 时遇到问题?
【发布时间】:2015-08-16 08:06:48
【问题描述】:

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

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;
}

【问题讨论】:

  • 当您点击复选框时,它们看起来是否被选中?
  • @JayeshElamgodil 没有任何反应,一个空框,当我点击它们时没有任何反应。

标签: android listview checkbox android-listview


【解决方案1】:

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

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

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

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

【讨论】:

  • 我只实现了 android clickable true 和 OnCheckedChangeListener 并且成功了,非常感谢。
【解决方案2】:

我建议你在适配器类的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)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-08-02
    相关资源
    最近更新 更多