从数据库设置复选框获得价值 [英] get value from database to set check box

查看:101
本文介绍了从数据库设置复选框获得价值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想建立复选框,这让光标从getLang()如果语言KEY_ACT = 1,则复选框是真的

这是我getLang()

 公开光标getLang(){
    返回db.query(LANGS_TABLE,新的String [] {
            KEY_LANG_ID,
            KEY_LANG,
            KEY_ACT},
            NULL,NULL,NULL,NULL,NULL,NULL);
}
 

和我有一个这样的数据库

  ContentValues​​ initialValues​​3 =新ContentValues​​();
        initialValues​​3.put(KEY_LANG,英语);
        initialValues​​3.put(KEY_ACT,1);
        db.insert(LANGS_TABLE,空,initialValues​​3);
        initialValues​​3.clear();
        initialValues​​3.put(KEY_LANG,法国);
        initialValues​​3.put(KEY_ACT,0);
        db.insert(LANGS_TABLE,空,initialValues​​3);
        initialValues​​3.clear();
        initialValues​​3.put(KEY_LANG,德国);
        initialValues​​3.put(KEY_ACT,0);
        db.insert(LANGS_TABLE,空,initialValues​​3);
        initialValues​​3.clear();
        initialValues​​3.put(KEY_LANG,意大利);
        initialValues​​3.put(KEY_ACT,0);
        db.insert(LANGS_TABLE,空,initialValues​​3);
        initialValues​​3.clear();
        initialValues​​3.put(KEY_LANG,西班牙);
        initialValues​​3.put(KEY_ACT,0);
        db.insert(LANGS_TABLE,空,initialValues​​3);
        initialValues​​3.clear();
 

所以当复选框显示,英国必须证明选择,因为KEY_ACT = 1

这是我的复选框,code

 保护无效printSelectedLanguage(){
    INT I = 0;
    db.open();
    光标C = db.getLang();
    对于(i = 0; I< _options.length-1;我++)
    {
        如果(c.getString(c.getColumnIndex(DBAdapter.KEY_ACT))。等于(1)){
            _selections [我] =真;
        }
        如果(_selections [我] ==真){
            db.setLang_Act第(i + 1,1);
        }否则如果(_selections [我] == FALSE){
            db.setLang_Act第(i + 1,0);
        }
    }
 

但它不显示从数据库中KEY_ACT

我应该怎么办?

///编辑为展示更多关于我的复选框警报对话框

 保护对话框onCreateDialog(INT ID)
{
    返回
    新AlertDialog.Builder(本)
        .setTitle(选择语言)
        .setMultiChoiceItems(_options,_selections,新DialogSelectionClickHandler())
        .setPositiveButton(OK,新DialogBu​​ttonClickHandler())
        。创建();}
  公共类DialogSelectionClickHandler实现DialogInterface.OnMultiChoiceClickListener
{
    @覆盖
    公共无效的onClick(DialogInterface对话框中,单击诠释,布尔选择)
    {
        Log.i(MEooooo,_options [点击] +选择:+选择);

    }
}

公共类DialogBu​​ttonClickHandler实现DialogInterface.OnClickListener
{
    @覆盖
    公共无效的onClick(DialogInterface对话,诠释点击)

    {
        开关(点击)
        {
            案例DialogInterface.BUTTON_POSITIVE:


                printSelectedLanguage();
                打破;
            案例DialogInterface.BUTTON_NEGATIVE:

                printSelectedLanguage();
                打破;
        }
    }
}
 

解决方案

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

如果 1 的真实, 0 是假的,你也可以检索字段为int:

 如果(c.getInt(c.getColumnIndex(DBAdapter.KEY_ACT))== 1){
 

修改 - 您可能希望通过实际的光标,因为你的数据库设置的设置一致<一href="http://developer.android.com/reference/android/app/AlertDialog.Builder.html#setMultiChoiceItems%28android.database.Cursor,%20java.lang.String,%20java.lang.String,%20android.content.DialogInterface.OnMultiChoiceClickListener%29"相对=nofollow> setMultipleChoiceItems()是期待:

 新AlertDialog.Builder(本)
    .setTitle(选择语言)
    .setMultiChoiceItems(光标,KEY_ACT,KEY_LANG,新DialogSelectionClickHandler())
    .setPositiveButton(OK,新DialogBu​​ttonClickHandler())
    。创建();
 

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

解决方案

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

这篇关于从数据库设置复选框获得价值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆