从数据库获取值以设置复选框
我想构建一个复选框,如果语言的 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
更多
发布评论
评论(2)
在
.equals()
比较中使用"1"
而不是1
怎么样?如果
1
为 true 并且0
为 false,您还可以将字段作为 int 检索:Edit - 您可能希望传递实际的光标,因为您的数据库设置与设置 setMultipleChoiceItems() 期望:
What about using
"1"
instead of1
in the.equals()
comparison?If
1
is true and0
is false, you could also retrieve the field as an int:Edit - you might want to pass the actual cursor, since your database setup matches the setup setMultipleChoiceItems() is expecting:
您最好将 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.