Java Android:如果数据库中没有值,则隐藏单选按钮
我试图隐藏单选按钮,如果没有值,它就是文本。单选按钮文本的值,我从SQLite数据库获取它,如果其中一个字段为空,我将单选按钮的可见性设置为不可见。然而,当我这样做时,即使其中一个选项中有内容,它也只是隐藏 下面是我的代码 FTR,我没有隐藏option1、option2和pollName,因为它们从不为空 我确信我在if和else语句中做了一些错误的事情,但是我试着去做,但没有找到解决方案 完整代码:Java Android:如果数据库中没有值,则隐藏单选按钮,java,android,database,sqlite,radio-button,Java,Android,Database,Sqlite,Radio Button,我试图隐藏单选按钮,如果没有值,它就是文本。单选按钮文本的值,我从SQLite数据库获取它,如果其中一个字段为空,我将单选按钮的可见性设置为不可见。然而,当我这样做时,即使其中一个选项中有内容,它也只是隐藏 下面是我的代码 FTR,我没有隐藏option1、option2和pollName,因为它们从不为空 我确信我在if和else语句中做了一些错误的事情,但是我试着去做,但没有找到解决方案 完整代码: public class ViewPollActivity extends Activity
public class ViewPollActivity extends Activity {
DatabaseHandler DbHandler;
SharedPreferences settings;
SharedPreferences.Editor editor;
TextView txtPollId, txtPollName;
RadioButton txtOptionName1, txtOptionName2, txtOptionName3, txtOptionName4,
txtOptionName5;
Button btndisplay;
RadioGroup rdgrp;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.view_poll);
txtPollId = (TextView) findViewById(R.id.pollId);
txtPollName = (TextView) findViewById(R.id.pollName);
txtOptionName1 = (RadioButton) findViewById(R.id.radio0);
txtOptionName2 = (RadioButton) findViewById(R.id.radio1);
txtOptionName3 = (RadioButton) findViewById(R.id.radio2);
txtOptionName4 = (RadioButton) findViewById(R.id.radio3);
txtOptionName5 = (RadioButton) findViewById(R.id.radio4);
btndisplay = (Button) findViewById(R.id.display);
rdgrp = (RadioGroup) findViewById(R.id.radioGroup1);
// Shared Preferences
// settings = getSharedPreferences("MYPREFS", 0);
// String username = settings.getString("username", null);
Intent i = getIntent();
String pollId = i.getStringExtra("pollId");
txtPollId.setText(pollId);
DbHandler = new DatabaseHandler(this);
Cursor c = DbHandler.getSinglePoll(pollId);
if (c.moveToFirst()) {
String PollName = c.getString(c.getColumnIndex("pollName"));
String optionName1 = c.getString(c.getColumnIndex("optionName1"));
String optionName2 = c.getString(c.getColumnIndex("optionName2"));
String optionName3 = c.getString(c.getColumnIndex("optionName3"));
String optionName4 = c.getString(c.getColumnIndex("optionName4"));
String optionName5 = c.getString(c.getColumnIndex("optionName5"));
txtPollName.setText(PollName);
txtOptionName1.setText(optionName1);
txtOptionName2.setText(optionName2);
txtOptionName3.setText(optionName3);
txtOptionName4.setText(optionName4);
txtOptionName5.setText(optionName5);
if(optionName3 == null) {
txtOptionName3.setVisibility(View.INVISIBLE);}
if (optionName4 == null) {
txtOptionName4.setVisibility(View.INVISIBLE);}
if(optionName5 == null) {
txtOptionName5.setVisibility(View.INVISIBLE);}
} else {
}
c.close();
DbHandler.close();
btndisplay.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
String selectedRadioValue = ((RadioButton) findViewById(rdgrp
.getCheckedRadioButtonId())).getText().toString();
Toast.makeText(ViewPollActivity.this, selectedRadioValue,
Toast.LENGTH_SHORT).show();
}
});
}// on Create
}// main class
尝试使用isNull方法检查null值
if(OptionName3 ==null ){
txtOptionName3.setVisibility(View.GONE);
}
如果值为null,则哪个语句为true?如果要隐藏多个语句,则必须使用纯if语句,而不是if-else-if语句。Else if只在该块中前面的所有语句都是假的情况下才会发生。同样值得关注的是,if语句中的所有选项名都以大写字母开头,而上面的选项名都以小写字母开头。你们可能引用了不同于你们想象的变量。我编辑了我的代码,那个只是输入了选项名。。。我将其更改为小写o,并如上所述更改了if语句。尽管有这些变化,我还是得到了同样的结果。对不起,它不起作用。我需要将xml文件中这些单选按钮的可见性也设置为不可见吗?我让它工作了,我必须设置if(optionName3==null | | | option3.equals(“”){txtOptionName3.setVisibility(View.GONE);}