gpt4 book ai didi

android - 复选框无法与 ListView 一起正常工作

转载 作者:行者123 更新时间:2023-11-29 15:26:31 25 4
gpt4 key购买 nike

我正在为 listview 的每个 Iten 实现带有 listview 的复选框。我遇到的问题是当我单击任何一个复选框然后一些其他未单击的复选框也被随机单击时。

public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub

View vi = convertView;
final int as=position;
if (convertView == null) { // if it's not recycled, initialize some
// attributes
LayoutInflater inflater = (LayoutInflater) activity
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
vi = inflater.inflate(R.layout.grid_item, null);
holder = new ViewHolder();

holder.textheader = (TextView) vi.findViewById(R.id.item1);
holder.textcpu = (TextView) vi.findViewById(R.id.item2);
holder.text_modified_date = (TextView) vi.findViewById(R.id.item4);
holder.text_cpu = (TextView) vi.findViewById(R.id.item3);
holder.text_checkbox=(CheckBox)vi.findViewById(R.id.checkBox1);
vi.setTag(holder);

holder.text_checkbox.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if (holder.text_checkbox.isClickable()) {
itemChecked.set(as, true);
packages=as;
} else if (!holder.text_checkbox.isClickable()) {
itemChecked.set(as, false);
}
String packag=app_package.get(as);
Log.d("package listttttttttt", packag);
}
});


} else {
holder = (ViewHolder) vi.getTag();


}

最佳答案

试试下面的代码

public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub

View vi = convertView;
final int as=position;
if (convertView == null) { // if it's not recycled, initialize some
// attributes
LayoutInflater inflater = (LayoutInflater) activity
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
vi = inflater.inflate(R.layout.grid_item, null);
holder = new ViewHolder();

holder.textheader = (TextView) vi.findViewById(R.id.item1);
holder.textcpu = (TextView) vi.findViewById(R.id.item2);
holder.text_modified_date = (TextView) vi.findViewById(R.id.item4);
holder.text_cpu = (TextView) vi.findViewById(R.id.item3);
holder.text_checkbox=(CheckBox)vi.findViewById(R.id.checkBox1);
vi.setTag(holder);

} else {
holder = (ViewHolder) vi.getTag();
}


holder.text_checkbox.setOnCheckedChangeListener(new OnCheckedChangeListener()
{
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
{
if(isChecked)
{
// do your stuff
itemChecked.set(as, true);
packages=as;
}
else
{
// do your stuff
itemChecked.set(as, false);
}

}
});

if(itemChecked.size() != 0 && itemChecked.get(position))
holder.text_checkbox.setChecked(true);
else
holder.text_checkbox.setChecked(false);

关于android - 复选框无法与 ListView 一起正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12085661/

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