【问题标题】:Custom Listview containing checkedtextview包含checkedtextview的自定义Listview
【发布时间】:2026-01-18 15:00:02
【问题描述】:

我实现了包含已检查文本视图的自定义列表视图。我的愿望是在单击时更改复选框的状态,但这似乎并不像我想象的那样简单。另外我想检查存储在数据库中的项目的复选框,怎么做?目前我有一个显示元素的活动,处理对复选框的单击(不是列表项!),但不能通过存储在数据库中的项目更改复选框状态。

这是我的自定义列表项:

<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:padding="5dp">

    <CheckedTextView
            android:id="@+id/name"
            android:layout_width="409dp"
            android:layout_height="30dp"
            android:checkMark="?android:attr/listChoiceIndicatorMultiple"
            android:paddingStart="5dp"
            android:textSize="20sp"
            android:textStyle="bold"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

    <TextView
            android:id="@+id/type"
            android:layout_width="409dp"
            android:layout_height="34dp"
            android:paddingStart="5dp"
            android:textSize="18sp"
            android:textStyle="italic"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/name" />

</androidx.constraintlayout.widget.ConstraintLayout>

这是我的适配器:

public class CustomAdapter extends BaseAdapter
{
    private List<CustomElement> elems;
    private LayoutInflater inflater;

    public HueBulbAdapter(Context ctx, List<CustomElement> elems)
    {
        this.elems = elems;
        inflater = LayoutInflater.from(ctx);
    }

    @Override
    public int getCount()
    {
        return elems.size();
    }

    @Override
    public Object getItem(int position)
    {
        return null;
    }

    @Override
    public long getItemId(int position)
    {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent)
    {
        ConstraintLayout result = (ConstraintLayout) inflater.inflate(R.layout.custom_elems, parent, false);
        CheckedTextView name = result.findViewById(R.id.name);
        TextView type = result.findViewById(R.id.type);
        CustomElement elem = elems.get(position);
        name.setText(elem.getName());
        type.setText(elem.getType());
        result.setTag(position);
        toggle(name);
        return result;
    }

    private void toggle(CheckedTextView ctv) {
        ctv.setOnClickListener(v -> {
            ctv.setChecked(!ctv.isChecked());
        });
    }
}

这是我的活动:

[...]
elemsView.setOnItemClickListener((parent, view, position, id) ->
        {
            if (!selected.containsKey(elemList.get(position).getName()))
            {
                selected.put(elemList.get(position).getName(), elemList.get(position));
            } else
            {
                selected.remove(elemList.get(position).getName());
            }
        });
[...]

也许我使用了错误的组件来达到我的目标?任何想法如何以这种方式或更好的方式做到这一点?

感谢您的帮助!

【问题讨论】:

    标签: android listview checkbox checkedtextview


    【解决方案1】:

    一些建议:

    1. CustomElement 模型添加一个布尔变量,以跟踪项目是否被选中。 添加private boolean isChecked;并为其生成getter和setter。

    2. 在适配器类中,使用public Object getItem(int position) 来返回列表中的项目,而不是null。 改为return elems.get(position);

    3. 在适配器类中,将toggle(name) 替换为:

      name.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
          elem.setChecked(!elem.isChecked());  // toggle
          name.setChecked(elem.isChecked());
        }
      });
      
    4. 在您的活动类中,要访问更新的列表,请使用:

      for (int i = 0; i < mAdapter.getCount(); i++) {
        CustomElement element = (CustomElement) mAdapter.getItem(i);
        if (element.isChecked()) {...} else {...}
      }
      
    5. 可选。在适配器类中搜索并实现ViewHolder Pattern,提高ListView项的加载速度。

    【讨论】: