我使用自定义列表视图和复选框。滚动列表视图时,检查状态互换如何控制互换并设置默认值,即我选中的选中项目
代码供您参考
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
if (convertView == null)
{
convertView=lInflater.inflate(R.layout.subtag, null);
holder=new viewHolder();
holder.check=(CheckBox)convertView.findViewById(R.id.subtag_checkCatagory);
holder.check.setOnCheckedChangeListener(new OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
// TODO Auto-generated method stub
if(isChecked)
{
System.out.println("checked item:"+buttonView.getText());
temp.add(buttonView.getText().toString());
}
else
{
temp.remove(buttonView.getText().toString());
}
}
});
convertView.setTag(holder);
}
else
{
holder = (viewHolder) convertView.getTag();
}
holder.check.setText(list.get(position));
return convertView;
}
答案 0 :(得分:4)
最后我找到了上述问题的答案。在此代码中,当我们滚动TextView
ListView
值不会发生变化
BaseAdapter
使用ArrayAdapter
代替 customlistview getview()
<强>代码强>
public View getView(int position, View convertView, ViewGroup parent) {
View view = null;
if (convertView == null) {
LayoutInflater inflator = context.getLayoutInflater();
view = inflator.inflate(R.layout.subtag, null);
final ViewHolder viewHolder = new ViewHolder();
viewHolder.text = (TextView) view.findViewById(R.id.subtag_textCatagory);
viewHolder.check = (CheckBox) view.findViewById(R.id.subtag_checkCatagory);
viewHolder.text.setOnClickListener(this);
viewHolder.check.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView,boolean isChecked) {
TagBojo element = (TagBojo) viewHolder.check.getTag();
element.setSelected(buttonView.isChecked());
System.out.println(element.getListName());
});
view.setTag(viewHolder);
viewHolder.check.setTag(list.get(position));
} else {
view = convertView;
((ViewHolder) view.getTag()).check.setTag(list.get(position));
}
ViewHolder holder = (ViewHolder) view.getTag();
holder.text.setText(list.get(position).getListName());
holder.check.setChecked(list.get(position).getSelected());
return view;
}