2015-01-04 46 views
2

處理我的論文我在LogCat中發出警告,我認爲這可能會影響我的代碼。我的複選框似乎不是複選框的實例

我收到了一些JSON數據,我必須用複選框和單選按鈕(在RadioGroup中)以編程方式顯示佈局。後來,我可以投票本次調查中,這裏是我的問題,因爲當我getChildAt()在我的佈局

if(child instanceof CheckBox) 

不工作,我不明白爲什麼。

這裏是我的代碼:

ll = (LinearLayout) findViewById(R.id.llSurvey); //OnCreate 

    /*Creating CheckBox*/ 
      String votes = jObj.getString("votes"); 
      String label = jObj.getString("label"); 
      String usid = jObj.getString("USID");  
      if(uType.equals("multi")){ 
       CheckBox cb = new CheckBox(SingleSurvey.this); 
       cb.setText(label + " (" + votes + ")"); 
       cb.setId(s+1000); 
       ll.addView(cb);   
       Log.i("MULTI_TYPE", "usid: " + usid + " id: " + cb.getId()); 
      } 
      s++; 

    /*Voting*/ 

      for(int i = 0; i < ll.getChildCount(); i++){ 
       View child = ll.getChildAt(i); 
       int p = child.getId(); 
       Log.i("CHILD ID", "id: " + p); 

       if(child instanceof TextView){ 
        continue; 
       } 

       else if(child instanceof CheckBox){ 
        Log.i("INSTANCE OF CB", "checkbox"); 
        CheckBox cb = (CheckBox) child; 
        if(cb.isChecked()){ 
         int cbId = cb.getId(); 
         usid = rb_usid[cbId]; 
         Log.i("CHECKBOX", "usid: " + usid); 
        } 
       } 

       //some other stuff 
      } 
+2

好的。我要打我。 – user58923 2015-01-04 12:04:41

回答

4

Checkbox類是TextView子類,所以您的代碼將不會進入else if塊,這是什麼警告說。

android.widget.TextView 
     ↳android.widget.Button 
      ↳android.widget.CompoundButton 
       ↳android.widget.CheckBox 
相關問題