praew_z
praew_z

Reputation: 175

get value from database to set check box

I want to build checkbox which get cursor from getLang() if KEY_ACT of language = 1 then checkbox is true

This is my getLang()

public Cursor getLang() {
    return db.query(LANGS_TABLE, new String[] { 
            KEY_LANG_ID,
            KEY_LANG,
            KEY_ACT},
            null, null, null, null, null, null);
}

and I have database like this

         ContentValues initialValues3 = new ContentValues();
        initialValues3.put(KEY_LANG, "English");
        initialValues3.put(KEY_ACT, "1");
        db.insert(LANGS_TABLE, null, initialValues3);
        initialValues3.clear();
        initialValues3.put(KEY_LANG, "French");
        initialValues3.put(KEY_ACT, "0");
        db.insert(LANGS_TABLE, null, initialValues3);
        initialValues3.clear();
        initialValues3.put(KEY_LANG, "German");
        initialValues3.put(KEY_ACT, "0");
        db.insert(LANGS_TABLE, null, initialValues3);
        initialValues3.clear();
        initialValues3.put(KEY_LANG, "Italy");
        initialValues3.put(KEY_ACT, "0");
        db.insert(LANGS_TABLE, null, initialValues3);
        initialValues3.clear();
        initialValues3.put(KEY_LANG, "Spanish");
        initialValues3.put(KEY_ACT, "0");
        db.insert(LANGS_TABLE, null, initialValues3);
        initialValues3.clear();

so when checkbox is show, the English must be show the select because KEY_ACT=1

and this is my checkbox code

protected void printSelectedLanguage(){
    int i = 0;
    db.open();
    Cursor c = db.getLang();
    for(i = 0; i < _options.length-1; i++ )
    {
        if (c.getString(c.getColumnIndex(DBAdapter.KEY_ACT)).equals(1)){
            _selections[i]=true;
        }
        if ( _selections[i]==true ) {
            db.setLang_Act(i+1, 1);
        }else if ( _selections[i]==false ){
            db.setLang_Act(i+1, 0);
        }
    }

but It's doesn't show the KEY_ACT from database

What should I do?

/// edit for show more about my check box on Alert dialog

protected Dialog onCreateDialog( int id ) 
{
    return 
    new AlertDialog.Builder( this )
        .setTitle( "Select language" )
        .setMultiChoiceItems( _options, _selections, new DialogSelectionClickHandler() )
        .setPositiveButton( "OK", new DialogButtonClickHandler() )
        .create();}
  public class DialogSelectionClickHandler implements DialogInterface.OnMultiChoiceClickListener
{
    @Override
    public void onClick( DialogInterface dialog, int clicked, boolean selected )
    {
        Log.i( "MEooooo", _options[ clicked ] + " selected: " + selected );

    }
}

public class DialogButtonClickHandler implements DialogInterface.OnClickListener
{
    @Override
    public void onClick( DialogInterface dialog, int clicked )

    {
        switch( clicked )
        {
            case DialogInterface.BUTTON_POSITIVE:


                printSelectedLanguage();
                break;
            case DialogInterface.BUTTON_NEGATIVE:

                printSelectedLanguage();
                break;
        }
    }
}

Upvotes: 1

Views: 2081

Answers (2)

Matthew
Matthew

Reputation: 44919

What about using "1" instead of 1 in the .equals() comparison?

If 1 is true and 0 is false, you could also retrieve the field as an int:

if (c.getInt(c.getColumnIndex(DBAdapter.KEY_ACT)) == 1) {

Edit - you might want to pass the actual cursor, since your database setup matches the setup setMultipleChoiceItems() is expecting:

new AlertDialog.Builder( this )
    .setTitle( "Select language" )
    .setMultiChoiceItems(cursor, KEY_ACT, KEY_LANG, new DialogSelectionClickHandler())
    .setPositiveButton( "OK", new DialogButtonClickHandler() )
    .create();

Upvotes: 1

biddulph.r
biddulph.r

Reputation: 5266

You would be better off storing KEY_ACT as a boolean or int wouldn't you? That way it works more fluidly with a checkbox.

Have you made sure to call c.moveToNext() or c.moveToFirst() to get the records in the cursor?

Don't forget to call c.close() after you have finished with the cursor as well, to keep your application tidy.

Upvotes: 0

Related Questions