本文介绍了该OnCheckChanged监听器仅适用于第一个复选框中customlistview的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
该侦听复选框在我的自定义列表视图只对第一个复选框工作。我觉得这事做的位置getView()。我安装我的code这个问题,请建议我一个工作围绕这个问题。
公开查看getView(最终诠释的立场,观点convertView,ViewGroup中父){
最后ViewHolder持有人;
LayoutInflater充气= context.getLayoutInflater();
如果(convertView == NULL)
{
convertView = inflater.inflate(R.layout.custom_list,NULL);
持有人=新ViewHolder();
holder.txtViewTitle =(TextView中)convertView.findViewById(R.id.title_text);
holder.txtViewDescription =(TextView中)convertView.findViewById(R.id.description_text);
holder.cb =(复选框)convertView.findViewById(R.id.cb);
convertView.setTag(保持器);
holder.cb.setOnCheckedChangeListener(新CompoundButton.OnCheckedChangeListener(){
字符串通道= holder.txtViewTitle.getText()的toString()。
@覆盖
公共无效onCheckedChanged(CompoundButton buttonView,布尔器isChecked){
如果(位置== 0)
{
//作品
}
其他
如果(位置== 1)
{
//不起作用
}
});
}
其他
{
支架=(ViewHolder)convertView.getTag();
}
holder.txtViewTitle.setText(标题[位置]);
holder.txtViewDescription.setText(介绍[位置]);
holder.txtViewDescription.setFocusable(假);
holder.txtViewTitle.setFocusable(假);
返回convertView;
}
解决方案
convertView是temlate的每个项目,只有在第一次调用空,你必须添加侦听器这样的每个项目:
公开查看getView(最终诠释的立场,观点convertView,ViewGroup中父){
// TODO自动生成方法存根
最后ViewHolder持有人;
LayoutInflater充气= context.getLayoutInflater();
如果(convertView == NULL)
{
convertView = inflater.inflate(R.layout.custom_list,NULL);
持有人=新ViewHolder();
holder.txtViewTitle =(TextView中)convertView.findViewById(R.id.title_text);
holder.txtViewDescription =(TextView中)
convertView.findViewById(R.id.description_text);
holder.cb =(复选框)convertView.findViewById(R.id.cb);
convertView.setTag(保持器);
} 其他 {
支架=(ViewHolder)convertView.getTag();
}
holder.cb.setOnCheckedChangeListener(新CompoundButton.OnCheckedChangeListener(){
字符串通道= holder.txtViewTitle.getText()的toString()。
@覆盖
公共无效onCheckedChanged(CompoundButton buttonView,布尔器isChecked){
// TODO自动生成方法存根
如果(位置== 0)
{
//作品
}否则,如果(位置== 1){
//不起作用
}
});
holder.txtViewTitle.setText(标题[位置]);
holder.txtViewDescription.setText(介绍[位置]);
holder.txtViewDescription.setFocusable(假);
holder.txtViewTitle.setFocusable(假);
返回convertView;
}
The listener for checkbox in my custom listview works only for the first checkbox. I think this has something to do with the position in getView(). I'm attaching my code with this question please suggest me a work around for this problem.
public View getView(final int position, View convertView, ViewGroup parent) {
final ViewHolder holder;
LayoutInflater inflater = context.getLayoutInflater();
if(convertView==null)
{
convertView = inflater.inflate(R.layout.custom_list, null);
holder = new ViewHolder();
holder.txtViewTitle = (TextView) convertView.findViewById(R.id.title_text);
holder.txtViewDescription = (TextView)convertView.findViewById(R.id.description_text);
holder.cb=(CheckBox) convertView.findViewById(R.id.cb);
convertView.setTag(holder);
holder.cb.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
String Channel=holder.txtViewTitle.getText().toString();
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if(position==0)
{
//works
}
else
if(position==1)
{
//doesn't work
}
});
}
else
{
holder=(ViewHolder)convertView.getTag();
}
holder.txtViewTitle.setText(title[position]);
holder.txtViewDescription.setText(description[position]);
holder.txtViewDescription.setFocusable(false);
holder.txtViewTitle.setFocusable(false);
return convertView;
}
解决方案
convertView is a temlate for every item and only at the first call null, you have to add the Listener for each item like this:
public View getView(final int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
final ViewHolder holder;
LayoutInflater inflater = context.getLayoutInflater();
if(convertView==null)
{
convertView = inflater.inflate(R.layout.custom_list, null);
holder = new ViewHolder();
holder.txtViewTitle = (TextView) convertView.findViewById(R.id.title_text);
holder.txtViewDescription = (TextView)
convertView.findViewById(R.id.description_text);
holder.cb=(CheckBox) convertView.findViewById(R.id.cb);
convertView.setTag(holder);
} else {
holder=(ViewHolder)convertView.getTag();
}
holder.cb.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
String Channel=holder.txtViewTitle.getText().toString();
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
// TODO Auto-generated method stub
if(position==0)
{
//works
}else if(position==1){
//doesn't work
}
});
holder.txtViewTitle.setText(title[position]);
holder.txtViewDescription.setText(description[position]);
holder.txtViewDescription.setFocusable(false);
holder.txtViewTitle.setFocusable(false);
return convertView;
}
这篇关于该OnCheckChanged监听器仅适用于第一个复选框中customlistview的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!