以下是getView方法
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
ViewHolder viewHolder = null;
if (convertView == null) {
LayoutInflater inflator = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflator.inflate(R.layout.filters, null);
viewHolder = new ViewHolder();
viewHolder.text = (TextView) convertView.findViewById(R.id.label);
viewHolder.checkbox = (CheckBox) convertView.findViewById(R.id.check);
final ViewHolder finalViewHolder = viewHolder;
checkBoxState(viewHolder);
convertView.setTag(viewHolder);
convertView.setTag(R.id.label, viewHolder.text);
convertView.setTag(R.id.check, viewHolder.checkbox);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
viewHolder.checkbox.setTag(position);
viewHolder.text.setText(list.get(position).getName());
viewHolder.checkbox.setChecked(list.get(position).isSelected());
return convertView;
}
private void checkBoxState(ViewHolder viewHolder) {
viewHolder.checkbox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
int getPosition = (Integer) buttonView.getTag();
list.get(getPosition).setSelected(buttonView.isChecked());
}
});
}
有一个名为getDomain的按钮。以下是该按钮的监听器。
private void domainListListener() {
domainListBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mv.setVisibility(View.VISIBLE);
if (adapter != null) {
adapter.clear();
} else {
}
setDomainAdapter();
}
});
}
还有一个名为getCountry的按钮,以下是该按钮的监听器,
private void countryListListener() {
countryListBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
lv.setVisibility(View.VISIBLE);
if (adapter != null) {
adapter.clear();
} else {
}
setAdapter(getCountries(), adapter);
}
});
}
如果用户点击国家/地区按钮,则列表视图将包含带复选框的国家/地区,如果用户点击该域,则列表视图将包含带复选框的域。
问题是复选框的状态未保存。例如,
我该如何解决这个问题?
以下是我用来设置适配器的代码,
private void setDomainAdapter() {
adapter = new FilterAdapter(getApplicationContext(), getDomains());
lv.setAdapter(adapter);
lv.setOnItemClickListener(Filtrate.this);
adapter.notifyDataSetChanged();
}
private void setAdapter(List<Model> countries, ArrayAdapter<Model> adapter) {
adapter = new FilterAdapter(getApplicationContext(), countries);
lv.setAdapter(adapter);
lv.setOnItemClickListener(Filtrate.this);
adapter.notifyDataSetChanged();
}
更新: 以下是我的getCountries和getDomains方法
private List<Model> getCountries() {
list.clear();
list.add(new Model("China"));
list.add(new Model("USA"));
list.add(new Model("India"));
list.add(new Model("Malaysia"));
list.add(new Model("Singapore"));
return list;
}
private List<Model> getDomains() {
list.clear();
list.add(new Model("Finance Law"));
list.add(new Model("Labour Law"));
list.add(new Model("Industrial Law"));
return list;
}