2012-01-14 2 views
0

확인란의 선택 여부를 결정하는 데 문제가 있습니다. 이것은 동적으로 생성되기 때문입니다. 각 동적 체크 박스에 대해 OnCheckedChangeListener()를 만들려고했으나 마지막 동적 체크 상자의 OnCheckedChangeListener() 만 실행되는 것으로 확인되었습니다. 제가 원하는 것은 그들이 확인 확인 여부와 관계없이 각 동적 확인란을 결정할 수 있어야하는 것입니다,동적으로 생성 된 체크 박스에서 체크 된 속성을 검색하는 방법은 무엇입니까?

public class yc_SubmitAttendance_ClassMode extends Activity { 

Context myContext = this; 
myDbAdapter myDB = null; 
Cursor myCur = null; 
Spinner mySpin = null; 

TableLayout tl = null; 

CheckBox myCB = null; 


public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.yc_studattend_classmode_layout); 

    mySpin = (Spinner)findViewById(R.id.spinClassModeSelectModGrp); 

    myDB = new myDbAdapter(myContext); 

    tl = (TableLayout)findViewById(R.id.classModeTableLayout); 
      //retrieve dynamic data 
      myCur = myDB.retrieveAttnForSelectedMod(mySpin.getItemAtPosition(arg2).toString()); 

      myCur.moveToFirst(); 

      for(byte rowNo=0;rowNo<myCur.getCount();rowNo++) 
      { 
       TableRow tr = new TableRow(myContext); 

       /*Some codes intentionally left missing */ 

       myCB = new CheckBox(myContext); 
       myCB.setId(rowNo); 

       //Setting a onCheckedListener on each of the checkbox generated 

       myCB.setOnCheckedChangeListener(new OnCheckedChangeListener(){ 

        public void onCheckedChanged(CompoundButton arg0, 
          boolean arg1) { 
         // TODO Auto-generated method stub 

         myCur.moveToPosition(myCB.getId()); 

         if(myCB.isChecked()) 
         { 
          Toast.makeText(myContext,"Attendance deducted to " + ((myCur.getFloat(2)) - 2) + "%", Toast.LENGTH_SHORT).show(); 
         } 
         else 
         { 
          Toast.makeText(myContext,"Attendance reverting to " + (myCur.getFloat(2)) + "%", Toast.LENGTH_SHORT).show(); 
         } 

        } 

       }); 

       //Insert the checkbox onto this row 
       tr.addView(myCB); 

       //Append this row to the TableLayout 
       tl.addView(tr, new TableLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)); 

       myCur.moveToNext(); 

      } //end of for-loop 
     } // end of onCreate 
}//end of class 

을하지만 :

가 여기 내 코드입니다 (그것이 도움이 될 것입니다 희망).

영 차이

답변

0

대체가이 코드 : 문제는()는 myCB.isChecked을 사용하고 있었다는

  myCB.setOnCheckedChangeListener(new OnCheckedChangeListener(){ 

       public void onCheckedChanged(CompoundButton thisCheckBox, 
         boolean isChecked) { 
        // TODO Auto-generated method stub 

        myCur.moveToPosition(thisCheckBox.getId()); 

        if(isChecked) 
        { 
         Toast.makeText(myContext,"Attendance deducted to " + ((myCur.getFloat(2)) - 2) + "%", Toast.LENGTH_SHORT).show(); 
        } 
        else 
        { 
         Toast.makeText(myContext,"Attendance reverting to " + (myCur.getFloat(2)) + "%", Toast.LENGTH_SHORT).show(); 
        } 

       } 

대신 두 번째 매개 변수 "ARG1"는 확인란이 선택 여부되었는지 여부를 확인합니다. myCB는 루프를 반복 할 때마다 할당하므로 항상 마지막 CheckBox를 가리 킵니다.

+0

안녕하세요 Grazahd, 빠른 응답에 감사드립니다. 그것은 지금 챔피언처럼 작동하고 ** onCheckedChangeListener() **에 대한 나의 이해를 명확히했습니다. 가치있는 어림짐작. –

관련 문제