2017-12-16 11 views
0

CustomAdapterで複数のスイッチを実装しました。Android、リストビューの複数スイッチの問題

下記のカスタムアダプターコード。

sche_swt = (Switch)convertView.findViewById(R.id.ctschedule); 
    loc_swt = (Switch)convertView.findViewById(R.id.ctlocation); 

    sche_swt.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { 
     @Override 
     public void onCheckedChanged(CompoundButton compoundButton, boolean is_checked) { 
      if (compoundButton.getId() == R.id.ctschedule) { 
       if (is_checked == true) { 
        contactItemList.get(position).setSchedule(true); 
       } else { 
        contactItemList.get(position).setSchedule(false); 
       } 
      } 
     } 
    }); 

    if (contactItemList.get(position).getScheduleInt() == 1) { 
     sche_swt.setChecked(true); 
    } 
    else 
     sche_swt.setChecked(false); 

    loc_swt.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { 
     @Override 
     public void onCheckedChanged(CompoundButton compoundButton, boolean is_checked) { 
      if (compoundButton.getId() == R.id.ctlocation) { 
       if (is_checked == true) { 
        contactItemList.get(position).setLocation(true); 
       } 
       else { 
        contactItemList.get(position).setLocation(false); 
       } 
      } 
     } 
    }); 

    if (contactItemList.get(position).getLocationInt() == 1) { 
     loc_swt.setChecked(true); 
    } 
    else 
     loc_swt.setChecked(false); 


    return convertView; 
} 

1行は

name 
phone_number 
switch 1 | switch 2 

のように見えると私は、例えば、スイッチ

の状態を保存:ように

1 line = switch(false) | switch(true) 
2 line = switch(false) | switch(true) 
3 line = switch(true) | switch(false) 

...と。私は私のアプリを終了した後

は、再実行リストビューの結果は

1 line = switch(true) | switch(false) 
2 line = switch(true) | switch(false) 
3 line = switch(true) | switch(false) 

何が問題になっていますように見えます?

答えて

0

リスナーでposition変数を使用するか、setCheckedメソッドを呼び出すことで問題が発生する可能性があります。次のように試してみてください。私はスケジュール切り替えのためだけに行って、位置スイッチも同じようにしてください。

sche_swt = (Switch)convertView.findViewById(R.id.ctschedule); 
loc_swt = (Switch)convertView.findViewById(R.id.ctlocation); 

// setting tag 
sche_swt.setTag(position) 
loc_swt.setTag(position) 

// you need to remove the listener because when you call setChecked() 
// it will invoke the listener unnecessarily and it might affect the 
// state of the view. 
sche_swt.setOnCheckedChangeListener(null); 
if (contactItemList.get(position).getScheduleInt() == 1) { 
    sche_swt.setChecked(true); 
} 
else 
    sche_swt.setChecked(false); 

sche_swt.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { 
    @Override 
    public void onCheckedChanged(CompoundButton compoundButton, boolean is_checked) { 
     if (compoundButton.getId() == R.id.ctschedule) { 
      // using the tag to get the position. 
      int position = compoundButton.getTag(); 
      if (is_checked == true) { 
       contactItemList.get(position).setSchedule(true); 
      } else { 
       contactItemList.get(position).setSchedule(false); 
      } 
     } 
    } 
}); 


return convertView; 

あなたはタグが何であるかわからない場合は

または私はそれではなく位置を使用し、なぜ、この https://stackoverflow.com/a/5291891/1749223 .Iを読んでは、上記の修正問題を願っています。

関連する問題