2017-11-23 11 views
0

setOnCheckedChangeListener를 구현하는 bindView 메서드가 있습니다. 그러나이 메서드를 초기화 할 때 onCheckedChanged 메서드가 체계적으로 호출됩니다. 어떻게 이것을 피할 수 있습니까? 내 코드는 다음과 같습니다.onCheckedChanged 초기화시 시스템 호출시

public void bindView(View view, Context context, Cursor cursor) { 
    super.bindView(view, context, cursor); 

    alarm_activation = cursor.getColumnIndexOrThrow(mydb.ALARMS_ACTIVATED); 
    activationInt = cursor.getInt(alarm_activation); 
    alarm_id_ind = cursor.getColumnIndexOrThrow("rowid"); 
    alarm_id_int = cursor.getInt(alarm_id_ind); 
    StrLog = String.valueOf(alarm_id_int); 

    Log.e("Row ID", StrLog); 
    alarm_activated = (ToggleButton)view.findViewById(R.id.alarm_activated); 
    alarm_activated.setTag(alarm_id_int); 

    if (activationInt == 1) { 
     alarm_activated.setChecked(true); 
     alarm_activated.getBackground().setColorFilter(Color.BLUE, PorterDuff.Mode.MULTIPLY); 
     activateAlarm(alarm_id_int); 
    } else { 
     alarm_activated.setChecked(false); 
    } 

    alarm_activated.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { 
     public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { 
      if (isChecked) { 
       buttonView.getBackground().setColorFilter(Color.BLUE, PorterDuff.Mode.MULTIPLY); 
       row = (Integer) buttonView.getTag(); 
       mydb.activateAlarm(row); 
       activateAlarm(alarm_id_int); 
      } else { 
       buttonView.getBackground().setColorFilter(Color.LTGRAY, PorterDuff.Mode.MULTIPLY); 
       row = (Integer) buttonView.getTag(); 
       mydb.deactivateAlarm(row); 
      } 
     } 
    }); 
} 

고맙습니다.

J

+3

가능한 중복을 [트리거 자동 피할 수

buttonView.isPressed() 

을 확인하여 후 당신은 트리거되는 리스너를 확인하여이를 방지 할 수 있습니다 onCheckedChanged 자동 전화] (https://stackoverflow.com/questions/27641705/oncheckedchanged-called-automatically) –

+0

Ruan 감사합니다. 이것은 실제로 중복 질문입니다. 아래의 솔루션에 내 의견에 표시된대로 실제로 솔루션을 보았지만, 나는 이미 "if (isChecked)"조건을 가지고 있기 때문에 처음에는 isPressed()가 중복 될 것이라고 생각했지만 실제로는 그렇지 않은 반면에 내가 지금 isChecked 절을 잘못 해석하고 있다는 것을 깨닫게되었습니다. 다시 감사합니다. J – JF0001

답변

1

보기를 누르면 여부 그것은 checkedchange 리스너에게의

+0

Ashif에게 감사드립니다. 나는 다른 스레드에서 그 솔루션을 보았지만, 이미 "if (isChecked)"조건을 가지고 있었기 때문에 원래는 isPressed()가 중복 될 것이라고 생각했지만 실제로는 그렇지 않다. isChecked 절 (현재 점검 중 ...). 다시 한번 감사드립니다. J – JF0001

+0

대단히 환영합니다! 해피 코딩 –