gpt4 book ai didi

java - 在 Android 中使用 OnCheckedChangedListener 在复选框中设置字符串值会返回空值

转载 作者:行者123 更新时间:2023-12-01 11:51:29 25 4
gpt4 key购买 nike

我有一个 Checkbox,当选中或未选中 Checkbox 时,它会设置 String 的值。但我的 String 返回空值。我已在 ActivityPublic class 中声明了该字符串。为什么返回 null?

我希望将字符串插入到数据库中,仅供引用。

     public class ClassFormCreate extends Activity
{
SQLiteDatabase db;
String Name, Desc;
String LabText = null;
String LecText = null;
CheckBox Lab, Lec;

@Override
protected void onCreate(Bundle savedInstanceState)
{

super.onCreate(savedInstanceState);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.classes_addform);
db=openOrCreateDatabase("ClassRecords",MODE_WORLD_READABLE, null);

}

public void AddClassFunction (View view)
{

EditText NameText = (EditText)findViewById(R.id.NameText);
EditText DescText = (EditText)findViewById(R.id.DescriptionText);
Lab = (CheckBox)findViewById(R.id.LabCheck);
Lec = (CheckBox)findViewById(R.id.RoomCheck);


Name = NameText.getText().toString();
Desc = DescText.getText().toString();

Lab.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener()
{
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b)
{
if(b)
{
LabText = "withlab";

}
else
{
LabText = "without";
}
}
});

Lec.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener()
{
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b)
{
if (b)
{
LecText = "corner";
}

else
{
LecText = "notcorner";
}
}
});

Toast toast2 = Toast.makeText(getApplicationContext(), LecText, Toast.LENGTH_SHORT);
toast2.show();


if (Name.equals(""))
{
Toast toast = Toast.makeText(getApplicationContext(), "Please insert a Class Name", Toast.LENGTH_SHORT);
toast.show();
}

else if (Desc.equals(""))
{
Toast toast = Toast.makeText(getApplicationContext(), "Please insert a Description", Toast.LENGTH_SHORT);
toast.show();
}
else
{

db.execSQL("INSERT INTO MasterClasslist (Name, Desc, SeatPlan, Lab) VALUES ('" + Name + "','" + Desc + "', '" + LecText +"' , '" + LabText + "');");

Cursor c2 = db.rawQuery("SELECT * FROM MasterClasslist", null);
c2.moveToLast();

final String IDcontainer = c2.getString(c2.getColumnIndex("ClassID"));



db.execSQL("CREATE TABLE '" + IDcontainer + "'(StudentID INTEGER NOT NULL UNIQUE, FirstName TEXT, LastName TEXT, pQuiz INTEGER DEFAULT '0', pAttend INTEGER DEFAULT '0', pRecite INTEGER DEFAULT '0'," +
"pProject INTEGER DEFAULT '0', pHomework INTEGER DEFAULT '0', pOthers INTEGER DEFAULT '0', pExam INTEGER DEFAULT '0'," +
"mQuiz INTEGER DEFAULT '0', mAttend INTEGER DEFAULT '0', mRecite INTEGER DEFAULT '0'," +
"mProject INTEGER DEFAULT '0', mHomework INTEGER DEFAULT '0', mOthers INTEGER DEFAULT '0', mExam INTEGER DEFAULT '0'," +
"fQuiz INTEGER DEFAULT '0', fAttend INTEGER DEFAULT '0', fRecite INTEGER DEFAULT '0'," +
"fProject INTEGER DEFAULT '0', fHomework INTEGER DEFAULT '0', fOthers INTEGER DEFAULT '0', fExam INTEGER DEFAULT '0'," +
"pGrade INTEGER DEFAULT '0', mGrade INTEGER DEFAULT '0', fGrade INTEGER DEFAULT '0', semGrade INTEGER DEFAULT '0' );");

Toast toast = Toast.makeText(getApplicationContext(), "Class Added", Toast.LENGTH_SHORT);
toast.show();

Intent ClassesMasterListIntent = new Intent(getApplicationContext(), ClassesMasterList.class);

startActivity(ClassesMasterListIntent);

overridePendingTransition(R.anim.transition, R.anim.right2left);

finish();
}

}

}

最佳答案

这是因为你没有初始化LabText和LecText。 Toast 消息是您的 Activity 生命周期的一部分。这意味着,无论您是否单击复选框,它们都会被执行。碰巧的是,在执行时,LabText 和 LecText 为空。

解决方案是初始化它们属性:

String LabText = "", LecText= "";

如果您希望在单击复选框时显示 Toast,请放置:

Toast.makeText(this,LabText, Toast.LENGTH_SHORT).show();

Toast.makeText(this,LecText, Toast.LENGTH_SHORT).show();

在您的OnCheckedChangeListeners内。

编辑后的代码:

Lab = (CheckBox)findViewById(R.id.LabCheck);
Lec = (CheckBox)findViewById(R.id.RoomCheck);

if(Lab.isChecked()) {
LabText = "withlab";
} else {
LabText = "without";
}

if (Lec.isChecked()) {
LecText = "corner";
} else {
LecText = "notcorner";
}

Toast.makeText(this,LabText, Toast.LENGTH_SHORT).show();

Toast.makeText(this,LecText, Toast.LENGTH_SHORT).show();

关于java - 在 Android 中使用 OnCheckedChangedListener 在复选框中设置字符串值会返回空值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28791390/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com