从数据库获取值以设置复选框

发布于 2024-10-20 20:55:28 字数 2973 浏览 8 评论 0原文

我想构建一个复选框,如果语言的 KEY_ACT = 1 则从 getLang() 获取光标,则复选框为 true

这是我的 getLang()

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

并且我有这样的数据库

         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();

,因此当显示复选框时,必须显示英语,因为 KEY_ACT= 1

这是我的复选框代码

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);
        }
    }

,但它没有显示数据库中的 KEY_ACT

我应该做什么?

/// 编辑以在“警报”对话框上显示有关我的复选框的更多信息

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;
        }
    }
}

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;
        }
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

恋你朝朝暮暮 2024-10-27 20:55:28

.equals() 比较中使用 "1" 而不是 1 怎么样?

如果 1 为 true 并且 0 为 false,您还可以将字段作为 int 检索:

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

Edit - 您可能希望传递实际的光标,因为您的数据库设置与设置 setMultipleChoiceItems() 期望:

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

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();
誰ツ都不明白 2024-10-27 20:55:28

您最好将 KEY_ACT 存储为布尔值或 int 不是吗?这样,使用复选框可以更流畅地工作。

您是否确定调用 c.moveToNext() 或 c.moveToFirst() 来获取游标中的记录?

不要忘记在完成光标操作后调用 c.close(),以保持应用程序整洁。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文