简体   繁体   English

通过按钮调用方法

[英]Calling a Method from button click

I'm new to java and I'm trying to run a method once a button is clicked and I am unsure if I'm on the right track. 我是java的新手,一旦单击按钮,我就试图运行一个方法,并且不确定是否在正确的轨道上。 I am selecting 10 questions from a Sqlite database and would like to cycle through each question 0-9 everytime the button is clicked. 我正在从Sqlite数据库中选择10个问题,并且每次单击按钮时都希望在0-9个问题之间循环。 Once all 10 questions are asked I will move to another activity. 提出所有10个问题后,我将转到另一项活动。 I have the app displaying the first question fine but I am having trouble calling the showQuestions method and increasing the questionNum int by one when the button is clicked and passing this onto the method. 我的应用程序可以很好地显示第一个问题,但是在单击按钮并将其传递给方法时,我无法调用showQuestions方法,并且将questionNum int增加一个。 Any help would be appreciated! 任何帮助,将不胜感激!

This is the showQuestions method which I am trying to call. 这是我试图调用的showQuestions方法。

public void showQuestions(Cursor cursor) {
    cursor.moveToPosition(questionNum);
    while (cursor.moveToNext()) {
        // Collect String Values from Query
        StringBuilder questiontxt = new StringBuilder("");
        StringBuilder answertxt1 = new StringBuilder("");
        StringBuilder answertxt2 = new StringBuilder("");
        StringBuilder answertxt3 = new StringBuilder("");
        StringBuilder answertxt4 = new StringBuilder("");
        String question = cursor.getString(2);
        String answer = cursor.getString(3);
        String option1 = cursor.getString(4);
        String option2 = cursor.getString(5);
        String option3 = cursor.getString(6);
        questiontxt.append(question).append("");
        answertxt1.append(answer).append("");
        answertxt2.append(option1).append("");
        answertxt3.append(option2).append("");
        answertxt4.append(option3).append("");
    }
}

This is the code for the button which I am working on. 这是我正在处理的按钮的代码。 There are 4 buttons. 有4个按钮。

public void onClick(View v) {
    switch (v.getId()) {

        case R.id.option1_button:
        if (questionNum<9) {
            questionNum ++;
        }
        Questions.this.showQuestions(null);
        break;
    }

And this is the XML code for the buttons. 这是按钮的XML代码。

<Button
    android:id="@+id/option1_button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_above="@+id/option3_button"/>

OnClick Listener for the button 按钮的OnClick侦听器

View option1_button = findViewById(R.id.option1_button);
option1_button.setOnClickListener(this);

Database Query and Allocation to Cursor 数据库查询和游标分配

//Get the questions and allocate them to the Cursor
public Cursor getQuestions() {
//Select Query 
String loadQuestions = "SELECT * FROM questionlist ORDER BY QID LIMIT 10";
SQLiteDatabase db = questions.getReadableDatabase();
Cursor cursor = db.rawQuery(loadQuestions, null);
startManagingCursor(cursor);
return cursor;
}

Thanks in advance. 提前致谢。

Questions.this.showQuestions(null);

Why you pass null to the showQuestions method? 为什么将null传递给showQuestions方法?

Let me guess - it crashes with NullPointerException . 让我猜测-它由于NullPointerException而崩溃。

Did you tried showQuestions(getQuestions()) 您是否尝试过showQuestions(getQuestions())

EDIT: Some general suggestions: 编辑:一些一般建议:

  • don't load the question every time when the button is clicked. 不要在每次单击按钮时都加载问题。

  • make some Question class containing all needed information for a question 制作一些Question类,其中包含Question的所有必需信息

  • when onCreate is invoked, open the database and load all needed questions in some Collection , like ArrayList<Question> 调用onCreate时,打开数据库并在某个Collection加载所有需要的问题,例如ArrayList<Question>

  • Every time when the user clicks a button, get the next Question element from the Collection and show it 每次用户单击按钮时,从Collection获取下一个Question元素并将其显示

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM