我想建立复选框,这让光标从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 initialValues3 =新ContentValues();
initialValues3.put(KEY_LANG,英语);
initialValues3.put(KEY_ACT,1);
db.insert(LANGS_TABLE,空,initialValues3);
initialValues3.clear();
initialValues3.put(KEY_LANG,法国);
initialValues3.put(KEY_ACT,0);
db.insert(LANGS_TABLE,空,initialValues3);
initialValues3.clear();
initialValues3.put(KEY_LANG,德国);
initialValues3.put(KEY_ACT,0);
db.insert(LANGS_TABLE,空,initialValues3);
initialValues3.clear();
initialValues3.put(KEY_LANG,意大利);
initialValues3.put(KEY_ACT,0);
db.insert(LANGS_TABLE,空,initialValues3);
initialValues3.clear();
initialValues3.put(KEY_LANG,西班牙);
initialValues3.put(KEY_ACT,0);
db.insert(LANGS_TABLE,空,initialValues3);
initialValues3.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,新DialogButtonClickHandler())
。创建();}
公共类DialogSelectionClickHandler实现DialogInterface.OnMultiChoiceClickListener
{
@覆盖
公共无效的onClick(DialogInterface对话框中,单击诠释,布尔选择)
{
Log.i(MEooooo,_options [点击] +选择:+选择);
}
}
公共类DialogButtonClickHandler实现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){
修改 - 您可能希望通过实际的光标,因为你的数据库设置的设置一致 setMultipleChoiceItems()是期待:
新AlertDialog.Builder(本)
.setTitle(选择语言)
.setMultiChoiceItems(光标,KEY_ACT,KEY_LANG,新DialogSelectionClickHandler())
.setPositiveButton(OK,新DialogButtonClickHandler())
。创建();
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();