包含checkedtextview的自定义Listview



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

这是我的自定义列表项:

<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());
}
});
[...]

也许我使用了错误的组件来达到目标?有什么想法可以用这种方式或更好的方式做吗?

谢谢你的帮助!

少数建议:

  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. 可选。在adapter类中搜索并实现ViewHolder Pattern,以提高ListView项的加载速度。

最新更新