2015-01-04 58 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