简体   繁体   English

我的复选框似乎不是CheckBox的instanceof

[英]my checkbox doesn't seem instanceof CheckBox

Working on my thesis I'm gettin a warning in LogCat that I think might affect my code. 根据我的论文,我在LogCat中发出警告,我认为这可能会影响我的代码。

I receive a JSON with some data and I have to bild a layout with checkbox and radiobutton (in RadioGroup) programmatically. 我收到一个包含一些数据的JSON,我必须以编程方式使用checkbox和radiobutton(在RadioGroup中)进行布局。 Later I can vote this survey and here is my problem cause when I getChildAt() in my layout 后来我可以投票这个调查,这是我的问题因为我在我的布局中getChildAt()

if(child instanceof CheckBox)

doesn't work and I can't understand why. 不起作用,我不明白为什么。

Here is my code: 这是我的代码:

 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
            }

The Checkbox class is a TextView subclass, so your code will never enter the else if block, which is what the warning is saying. Checkbox类是TextView子类,因此您的代码将永远不会输入else if块,这就是警告的意思。

    android.widget.TextView
       ↳android.widget.Button
           ↳android.widget.CompoundButton
               ↳android.widget.CheckBox

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM