简体   繁体   中英

isSelected method always returns false

I'm using isSelected for a checkbox. The debugging shows that the method returns false nevermind if it's selected or not.

The part from the xml :

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

and the code:

@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 always recieves a false for some reason...

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

Try: chwatch.isChecked();

instead of isSelected.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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