[英]Custom Listview containing checkedtextview
I implement custom listview containing a checked textview.我实现了包含选中的文本视图的自定义列表视图。 My wish is to change the state of the checkbox on click, but it seems not to be simple, as I thought it would be.我的愿望是在单击时更改复选框的状态,但这似乎并不像我想象的那样简单。 Additionally I would like to check the checkboxes of items, that are stored in database, how could it be done?另外我想检查存储在数据库中的项目的复选框,怎么做? At the moment I have an activity which shows the elements, handles the click on the checkbox (not the list item!), but can´t change the checkbox status by items stored in database.目前我有一个显示元素的活动,处理对复选框的点击(不是列表项!),但不能通过存储在数据库中的项目更改复选框状态。
This is my custom list item:这是我的自定义列表项:
<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>
This is my adapter:这是我的适配器:
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());
});
}
}
And this is my activity:这是我的活动:
[...]
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());
}
});
[...]
Maybe I am using wrong components to reach my target?也许我使用错误的组件来达到我的目标? Any Ideas how to do it this way or in a better way?任何想法如何以这种方式或更好的方式做到这一点?
Thanks for your help!谢谢你的帮助!
Few suggestions:几点建议:
Add a boolean variable to the CustomElement
model to track if item is checked or not checked.将布尔变量添加到CustomElement
模型以跟踪项目是否已选中。 Add private boolean isChecked;
添加private boolean isChecked;
and generate getter and setter for it.并为其生成 getter 和 setter。
In adapter class, use public Object getItem(int position)
to return item in list, and not null
.在适配器类中,使用public Object getItem(int position)
返回列表中的项目,而不是null
。 Change to return elems.get(position);
更改为return elems.get(position);
In adapter class, replace toggle(name)
with:在适配器类中,将toggle(name)
替换为:
name.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { elem.setChecked(!elem.isChecked()); // toggle name.setChecked(elem.isChecked()); } });
In your activity class, to access the updated list, use this:在您的活动类中,要访问更新的列表,请使用以下命令:
for (int i = 0; i < mAdapter.getCount(); i++) { CustomElement element = (CustomElement) mAdapter.getItem(i); if (element.isChecked()) {...} else {...} }
Optional.可选的。 Search and implement ViewHolder Pattern
in adapter class to improve the loading speed of ListView items.在adapter类中搜索并实现ViewHolder Pattern
,以提高ListView item的加载速度。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.