gpt4 book ai didi

android - 在自定义 ListView 中选中复选框并保持其状态?

转载 作者:太空狗 更新时间:2023-10-29 14:20:47 26 4
gpt4 key购买 nike

我使用下面的代码来保持复选框状态,但它不起作用。我只想选中两个复选框。

public class CACompareList extends ArrayAdapter<CompareListData>{

public static int count = 0;
public LayoutInflater inflater;
public static ArrayList<CompareListData> selectedId;
public ArrayList<CompareListData> listObjects;
Context contex;
public CACompareList(Context context, int textViewResourceId,
ArrayList<CompareListData> objects) {
super(context, textViewResourceId, objects);
this.inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
this.listObjects = objects;
selectedId = new ArrayList<CompareListData>();
this.contex = context;
}
public static class ViewHolder
{
TextView txtViewLoanName;
TextView txtViewHtmlString;
CheckBox chkSelected;
}

@Override
public View getView(final int position, View convertView, ViewGroup parent) {
final ViewHolder holder;
if(convertView==null)
{
holder = new ViewHolder();
convertView = inflater.inflate(R.layout.row_comparelist, null);

holder.txtViewLoanName= (TextView) convertView.findViewById(R.id.rowcomparelist_tv_loanname);
holder.txtViewHtmlString= (TextView) convertView.findViewById(R.id.rowcomparelist_tv_loandetail);
holder.chkSelected= (CheckBox) convertView.findViewById(R.id.rowcomparelist_chk_selected);
convertView.setTag(holder);
}
else{
holder=(ViewHolder)convertView.getTag();
}

final CompareListData data = this.listObjects.get(position);

holder.txtViewLoanName.setText(this.listObjects.get(position).getLoanName());
holder.txtViewHtmlString.setText(Html.fromHtml(this.listObjects.get(position).getHtmlString()));


holder.chkSelected.setOnCheckedChangeListener(new OnCheckedChangeListener() {

@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {

//listObjects.get(position).setSelected(isChecked);
if(isChecked){
count++;

}else{
count--;
selectedId.remove(data);
}
if(count >= 3)// it will allow 3 checkboxes only
{
Toast.makeText(contex, "Select only two Loan", Toast.LENGTH_LONG).show();
buttonView.setChecked(false);
count--;
}
else
{
selectedId.add(data);
buttonView.setSelected(isChecked);
int getPosition = (Integer) buttonView.getTag(); // Here we get the position that we have set for the checkbox using setTag.
listObjects.get(getPosition).setSelected(buttonView.isChecked()); // Set the value of checkbox to maintain its state.
Log.e("Selected Position is:", String.valueOf(getPosition));
}
}
});
holder.chkSelected.setTag(position);
holder.chkSelected.setSelected(this.listObjects.get(position).getSelected());
Log.e("Position is:", String.valueOf(position));
return convertView;
}

}

当我选中第一个复选框,然后向下和向上滚动时,我得到不同的结果,我的第一个复选框未选中,而另一个复选框被选中。

我也检查了 listView 项目点击监听器,但它也没有帮助我。

lvLoanLiat.setOnItemClickListener(new OnItemClickListener() {

@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
if(IN_EDIT_MODE == false){
CompareListData data = (CompareListData)arg0.getItemAtPosition(arg2);

CheckBox cBox = (CheckBox) arg1.findViewById(R.id.rowcomparelist_chk_selected);
if(cBox.isChecked()){
count--;
selectedId.remove(data);
}else{
count++;
}
if(count >= 3)// it will allow 3 checkboxes only
{
Toast.makeText(CompareListActivity.this, "Select only two Loan", Toast.LENGTH_LONG).show();
cBox.setChecked(false);
count--;
}
else
{
selectedId.add(data);
cBox.setSelected(true);
data.setSelected(true);
Log.e("Selected Position is:", String.valueOf(arg2));
}
}

}
});

下面是屏幕截图,我只检查了第一项,如下面的屏幕截图所示。 enter image description here

然后我上下滚动,然后得到如下图所示的结果。

enter image description here

谢谢。

最佳答案

在 getView() 中你应该先取消设置监听器

holder.chkSelected.setOnCheckedChangeListener(null);
holder.chkSelected.setSelected(isSelected);

this也许可以帮助你。注意第 622 行。

关于android - 在自定义 ListView 中选中复选框并保持其状态?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17161986/

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