gpt4 book ai didi

java - 将全选行添加到 ListView 中

转载 作者:行者123 更新时间:2023-12-01 09:37:24 25 4
gpt4 key购买 nike

这是我的CustomObdRowAdapter.java我在顶部添加了一个“全选”行,当用户选择它时,应检查当前 listView 中的所有项目,但我应该如何在自定义行适配器中实现它?

private class ViewHolder{
CheckBox name;
}
@Override
public View getView(final int position, View convertView, final ViewGroup parent) {
ViewHolder holder = null;
LayoutInflater settingInflater = LayoutInflater.from(getContext());
if (convertView == null) {
convertView = settingInflater.inflate(R.layout.custom_row, parent, false);
holder = new ViewHolder();
holder.name = (CheckBox) convertView.findViewById(R.id.checkBox1);
convertView.setTag(holder);


}else{
holder = (ViewHolder)convertView.getTag();
}
holder.name.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
CheckBox cb = (CheckBox) v;
RowCheckbox rowCommandCheckbox = (RowCheckbox) cb.getTag();

if (cb.getText() == ifAllSelectStr){
if (cb.isChecked()){
// Select all items in this listView
}else{
// Unselect all items in this listView
}
}

最佳答案

// select all
for (int i = 0; i < adapter.getCount(); i++) {
list.setItemChecked(i, true);
}

// unselect all
for (int i = 0; i < adapter.getCount(); i++) {
list.setItemChecked(i, false);
}

您可能需要从适配器外部调用它

getListView().setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);

关于java - 将全选行添加到 ListView 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38750651/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com