1

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

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

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;
  }
4

2 回答 2

0

请修改 Checked changed 方法为

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

希望它有效...

于 2015-09-16T04:43:33.370 回答
0

这解决了它,希望它可以帮助遇到我遇到同样问题的人。

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);
        }

        }
        });
于 2015-09-16T16:26:34.360 回答