gpt4 book ai didi

java - 为什么我的 android 应用程序在我向数据库填写一个简单的问题表单时返回到以前的 Activity ?

转载 作者:行者123 更新时间:2023-11-29 22:35:50 24 4
gpt4 key购买 nike

我正在尝试构建一个测验应用程序,管理员将在其中创建管理 Activity 中的问题,这些问题将在另一个 Activity 中显示为测验考试。当我尝试通过单击管理 Activity 中的创建问题按钮来填写表格时,我的应用程序将返回到之前的 Activity (管理登录 Activity )这是 AdminActivity 的代码:

public class AdminActivity extends AppCompatActivity {
Button btnCreateQuestion, btnReadQuestion, btnDeleteQuestion;
EditText editTextQuestion, editTextOption1, editTextOption2, editTextOption3, editTextOption4,
editTextAnswerNo,editTextChapterNo;
QuizDbHelper db;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_admin);
btnCreateQuestion = findViewById(R.id.btnCreateQuestion);
btnReadQuestion = findViewById(R.id.btnReadQuestions);
btnDeleteQuestion = findViewById(R.id.btnDeleteQuestions);
editTextQuestion = findViewById(R.id.editTextQuestion);
editTextOption1 = findViewById(R.id.editTextOption1);
editTextOption2 = findViewById(R.id.editTextOption2);
editTextOption3 = findViewById(R.id.editTextOption3);
editTextOption4 = findViewById(R.id.editTextOption4);
editTextAnswerNo = findViewById(R.id.editTextAnswerNo);
editTextChapterNo = findViewById(R.id.editTextChapterNo);
db = new QuizDbHelper(this);

btnCreateQuestion.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String question = editTextQuestion.getText().toString();
String option1 = editTextOption1.getText().toString();
String option2 = editTextOption2.getText().toString();
String option3 = editTextOption3.getText().toString();
String option4 = editTextOption4.getText().toString();
Integer answerNo = Integer.parseInt(editTextAnswerNo.getText().toString());
String chapterName = editTextAnswerNo.getText().toString();
if( question != "" && option1 != "" && option2 != "" && option3 != "" && option4 != "" && answerNo!= null && chapterName != ""){
Question q = new Question(question,option1,option2,option3,option4,answerNo,chapterName);
db.addQuestion(q);
Toast.makeText(AdminActivity.this,"Question Added", Toast.LENGTH_SHORT).show();
}else{
Toast.makeText(AdminActivity.this,"Please fill up", Toast.LENGTH_SHORT).show();
}

}
});
btnReadQuestion.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
ArrayList<Question> allQuestions = db.getAllQuestions();
if(allQuestions.size()< 1){
Toast.makeText(AdminActivity.this,"No questions in the Database", Toast.LENGTH_SHORT).show();
}else {
StringBuffer buffer = new StringBuffer();
for (Question q: allQuestions) {
buffer.append("Question : " + q.getQuestion() + "\n" );
buffer.append("Option1: " + q.getOption1() + "\n");
buffer.append("Option2: " + q.getOption2() + "\n");
buffer.append("Option3: " + q.getOption3() + "\n");
buffer.append("Option4: " + q.getOption4() + "\n");
buffer.append("Answer No: " + q.getAnswerNo() + "\n");
buffer.append("Option4: " + q.getChapterName() + "\n\n");
}

AlertDialog.Builder builder = new AlertDialog.Builder(AdminActivity.this);
builder.setCancelable(true);
builder.setTitle("Questions");
builder.setMessage(buffer.toString());
builder.show();
}
}
});
btnDeleteQuestion.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {

}
});
}}

有什么线索吗?提前致谢。

最佳答案

检查您的 Activity 类的 onCreate 函数,您是否已经初始化了您的 EditTextButton

此外,您必须按以下方式更改您的条件:

if(!question.equalsIgnoreCase("") && !option1.equalsIgnoreCase("") && !option2.equalsIgnoreCase("") && !option3.equalsIgnoreCase("") && !option4.equalsIgnoreCase("") && answerNo != 0 && !chapterName.equalsIgnoreCase("")

因为,== 测试引用相等性(它们是否是同一个对象)。

equalsIgnoreCase() 在比较两个字符串时忽略大小写。

关于java - 为什么我的 android 应用程序在我向数据库填写一个简单的问题表单时返回到以前的 Activity ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59517699/

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