这是我CustomObdRowAdapter.java
我在顶部添加了一个“Select All”行,当用户选择它时,应该检查当前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
}
}
答案 0 :(得分:1)
// 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);
答案 1 :(得分:0)
boolean isAllTrue=false;
@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);
}
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()){
isAllTrue=true;
// Select all items in this listView
}else{
isAllTrue=false;
// Unselect all items in this listView
}
notifyDataSetChanged();
}
}
convertView.setTag(holder); //始终将此设置标记放在if else // of tag之外 holder.name.setChecked(isAllTrue); } // getView函数的结尾
在上面的代码中,他们首先要做的两件事是更改viewholder的设置标签以查看并将其放在函数的末尾,然后返回convertView,第二次创建boolean for allTrue,当所有项目选中时单击listner调用然后通知采用者重绘。