持有 android.widget.Switch 的 ViewHolder,Switch 未响应侦听器

ViewHolder holding an android.widget.Switch, Switch not responding to listener

好吧,我有一个持有人,持有一堆除了开关之外不会改变的元素(很明显)。

我减少了代码,使其更易于阅读,我认为这将使我能够在日志中看到正在更改的开关状态(从适配器动态生成)。

public View getView(final int position, View convertView, ViewGroup parent) {
    final ViewHolder holder;
        holder = new ViewHolder();
        holder.communicationfilter = (Switch)convertView.findViewById(R.id.communicationfilter);

    holder.communicationfilter.setChecked(!kid.isBlocked());
    holder.communicationfilter.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        Log.v(LOG_TAG, "Switch State="+isChecked);
    }               
    });
    convertView.setTag(holder);
    return convertView;
  }

请将Checked修改方法修改为

if(isChecked){
Log.v(LOG_TAG, "Switch State="+isChecked);
}else{
Log.v(LOG_TAG, "Switch State="+isChecked);
}

希望它有用...

这已经解决了,希望它能帮助遇到同样问题的人。

holder.communicationfilter.setOnCheckedChangeListener(new OnCheckedChangeListener() {
    @Override
    public void onCheckedChanged(CompoundButton buttonView,
    boolean isChecked) {
        if (isChecked) {
            Toast.makeText(mContext, "The switch is ON",Toast.LENGTH_SHORT).show();
            Log.i(LOG_TAG,"switch checked for" + holder.nameTxtView);
        } else {    
            Toast.makeText(mContext,"The switch is OFF", Toast.LENGTH_SHORT).show();
            Log.i(LOG_TAG,"switch unchecked" + holder.nameTxtView);
        }

        }
        });