gpt4 book ai didi

android - 带有复选框的自定义 ListView -滚动时互换检查状态

转载 作者:搜寻专家 更新时间:2023-11-01 08:15:04 24 4
gpt4 key购买 nike

我使用带有复选框的自定义 ListView 。在滚动 ListView 时,选中的状态会互换如何控制互换并将其设置为默认值,即我选择的选中项目

引用代码

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;
}

最佳答案

我终于找到了上述问题的答案。在此代码中,当我们滚动 ListView

时,检查值和 TextView 值没有改变

步骤

  1. customlistview 使用 ArrayAdapter 而不是 BaseAdapter
  2. 为复选框选中/取消选中和 TextView 获取/设置编写一个 bojo 类
  3. 按照我在下面规定的那样专注于 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;
}

关于android - 带有复选框的自定义 ListView -滚动时互换检查状态,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5538867/

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