isSelected方法始终返回false

时间:2013-12-03 16:29:04

标签: java android

我正在使用isSelected作为复选框。 调试显示该方法返回false nevermind,如果它被选中。

xml中的部分:

 <CheckBox
        android:id="@+id/checkBox1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Watched"
        android:layout_margin="10sp" />

和代码:

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.editmovie);

        final DBHandler myMovies = new DBHandler(this);
        final EditText edname = (EditText)findViewById(R.id.editText1);
        final EditText eddetails = (EditText)findViewById(R.id.editText2);
        final EditText edurl = (EditText)findViewById(R.id.editText3);
        final CheckBox chwatched = (CheckBox)findViewById(R.id.checkBox1);
        Button btn = (Button)findViewById(R.id.button1);
        Intent intent = getIntent();
        getId = intent.getStringExtra("id");
        if (getId != null)
        {
            Cursor cursor = myMovies.getMovie(getId);
            cursor.moveToFirst();
            edname.setText(cursor.getString(1));
            eddetails.setText(cursor.getString(2));
            edurl.setText(cursor.getString(3));
            String iswatched = cursor.getString(4);
            if ((iswatched.equals("true")
                chwatched.setSelected(true);
            else
                chwatched.setSelected(false);
            flag=true;  
        }

        btn.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                Intent intent = new Intent(EditMovie.this , MainActivity.class);
                String name = edname.getText().toString();
                String details = eddetails.getText().toString();
                String url = edurl.getText().toString();        
                boolean wt = chwatched.isSelected();
                String watched = String.valueOf(wt);    
                if (flag)
                    myMovies.updateMovie(new Movie(name, details, url, watched));
                else
                    myMovies.addMovie(new Movie(name, details, url, watched));

                startActivity(intent);

            }
        });
    }

}

wt因某种原因总是收到错误......

2 个答案:

答案 0 :(得分:3)

尝试使用checkBox.isChecked()checkBox.setChecked(true/false)

答案 1 :(得分:2)

尝试:       chwatch.isChecked();

而不是isSelected。