下面的列表复选框被选中,如果我检查列表视图顶部的复选框 [英] Checkbox below the list are checked, if i check any checkboxes on top of the listview
本文介绍了下面的列表复选框被选中,如果我检查列表视图顶部的复选框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
什么是破天:: 的
列表视图我能够选中该复选框,当我滚动复选框在列表中向下下面的列表元素被检查
我怎样才能解决这个!的
{UPDATED code}
AdptSearchFilterCategories.java
公共类AdptSearchFilterCategories延伸ArrayAdapter< ModelFilterCategories> { 私人上下文的背景下;
清单< ModelFilterCategories> mdlLst;
公共AdptSearchFilterCategories(Activity上下文,列表< ModelFilterCategories> mdlLst){
超(背景下,R.layout.adpt_searchfilter_category,mdlLst);
this.context =背景;
this.mdlLst = mdlLst;
}
静态类ViewHolder {
保护复选框chkBxCatId;
} @覆盖
公共查看getView(INT位置,查看convertView,父母的ViewGroup){
ViewHolder viewHolder = NULL;
如果(convertView == NULL){
LayoutInflater mInflater =(LayoutInflater)context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
convertView = mInflater.inflate(R.layout.adpt_searchfilter_category,NULL); viewHolder =新ViewHolder();
viewHolder.chkBxCatId =(复选框)convertView.findViewById(R.id.chkBxCatId);
viewHolder.chkBxCatId.setOnCheckedChangeListener(新CompoundButton.OnCheckedChangeListener(){
@覆盖
公共无效onCheckedChanged(CompoundButton buttonView,布尔器isChecked){
INT为getPosition =(整数)buttonView.getTag(); //这里我们得到我们为使用setTag复选框的位置。
mdlLst.get(为getPosition).setCheckboxState(buttonView.isChecked()); //设置复选框的值,以保持其状态。
}
});
convertView.setTag(viewHolder);
}其他{
viewHolder =(ViewHolder)convertView.getTag();
} viewHolder.chkBxCatId.setTag(位置); //这一行是很重要的。
viewHolder.chkBxCatId.setText(mdlLst.get(位置).getCategoryName()); 返回convertView;
}}
解决方案
复选框的复制到其他国家的观点,因为该适配器重用convertview。
- 我建议增加一个
器isChecked
变量的模式
对象。 -
在获取视图的方法,你应该使用这样的:
viewHolder.checkbox.setChecked(list.get(位置).isChecked);
-
不要使用
setOnCheckedChangeListener
,因为这个:viewHolder.checkbox.setChecked(list.get(位置).isChecked);
将调用方法onCheckedChanged。
- 实现OnClickListener上
viewHolder.checkbox
,当你得到的单击事件只是切换的布尔状态(使用Model.isChecked =!模型。器isChecked
),然后notifyDataSetChanged
。
What is Happening:: Listview i am able to check the checkbox, when i scroll the list elements below the checkboxes down the list are are checked
How can i resolve this !
{UPDATED CODE}
AdptSearchFilterCategories.java
public class AdptSearchFilterCategories extends ArrayAdapter<ModelFilterCategories> {
private Context context;
List<ModelFilterCategories> mdlLst;
public AdptSearchFilterCategories(Activity context, List<ModelFilterCategories> mdlLst) {
super(context, R.layout.adpt_searchfilter_category, mdlLst);
this.context = context;
this.mdlLst = mdlLst;
}
static class ViewHolder {
protected CheckBox chkBxCatId;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder viewHolder = null;
if (convertView == null) {
LayoutInflater mInflater = (LayoutInflater) context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
convertView = mInflater.inflate(R.layout.adpt_searchfilter_category, null);
viewHolder = new ViewHolder();
viewHolder.chkBxCatId = (CheckBox) convertView.findViewById(R.id.chkBxCatId);
viewHolder.chkBxCatId.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
int getPosition = (Integer) buttonView.getTag(); // Here we get the position that we have set for the checkbox using setTag.
mdlLst.get(getPosition).setCheckboxState(buttonView.isChecked()); // Set the value of checkbox to maintain its state.
}
});
convertView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
viewHolder.chkBxCatId.setTag(position); // This line is important.
viewHolder.chkBxCatId.setText(mdlLst.get(position).getCategoryName());
return convertView;
}
}
解决方案
The state of the check box copied to others view because the adapter is reusing the convertview.
- I suggest adding an
isChecked
variable to theModel
object. In the "get view" method, you should use this:
viewHolder.checkbox.setChecked(list.get(position).isChecked);
Don't use
setOnCheckedChangeListener
, because this:viewHolder.checkbox.setChecked(list.get(position).isChecked);
will invoke the method onCheckedChanged.
- Implement OnClickListener on
viewHolder.checkbox
, and when you get the click event just switch the boolean state (usingModel.isChecked = !Model.isChecked
) and thennotifyDataSetChanged
.
这篇关于下面的列表复选框被选中,如果我检查列表视图顶部的复选框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文