如何根据选择查询的结果更新TextView中的文本

时间:2018-07-13 15:08:22

标签: java android sql-server database select-query

我是Android新手,正在开发一个简单的测验应用程序。

有3个选项和1个问题。

问题和选项存储在数据库中。

应用程序启动时我要执行的操作将显示第一个问题和答案,单击正确或错误答案会更新问题和选项。均值显示下一个问题和选项。

因此,现在我可以在屏幕上成功显示第一个问题和相关选项,但是单击时不会显示下一个问题。

当我尝试查找问题时,我发现我正在使用的代码仅在执行查询后才显示第一个值,而结果却有更多行。

我尝试将getstring()中的索引值从1更改为2和0,但是没有结果。

所以我的问题是如何完成这项任务。

这是我的代码。

package com.example.android.quiz_app_androidmodule;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.os.AsyncTask;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;



public class MainActivity extends AppCompatActivity {
    ConnectionClass connectionClass;
    TextView Question, Score;
    Button btnlogin, btnA, btnB, btnC, btnStart;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        connectionClass = new ConnectionClass();

        btnlogin = (Button) findViewById(R.id.btnStart);

        btnA = (Button) findViewById(R.id.button1);
        btnB = (Button) findViewById(R.id.button2);
        btnC = (Button) findViewById(R.id.button3);
        Question = (TextView) findViewById(R.id.textViewQuestion);
        Score = (TextView) findViewById(R.id.textViewScore);


        btnlogin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
             DoLogin doLogin = new DoLogin();
                doLogin.execute("");

                btnA.setVisibility(View.VISIBLE);
                btnB.setVisibility(View.VISIBLE);
                btnC.setVisibility(View.VISIBLE);
                Question.setVisibility(View.VISIBLE);
                Score.setVisibility(View.VISIBLE);
                btnlogin.setVisibility(View.GONE);


            }
        });


    }






    String z = "";
    String zA = "";
    String zB="";
    String zC="";
    int id = 1;
    int points;

    public class DoLogin extends AsyncTask<String, String, String> {

        Boolean isSuccess = false;


        @Override
        protected void onPostExecute(String r) {


            if (isSuccess) {
              Question.setText(z);
                btnA.setText(zA);
                btnB.setText(zB);
                btnC.setText(zC);
            }

        }

        @Override
        protected String doInBackground(String... params) {

            try {
                Connection con = connectionClass.CONN();
                if (con == null) {
                    z = "Error in connection with SQL server";
                } else {
                    String query = "select question from Question where quizID >= '0'";
                    Statement stmt = con.createStatement();
                    ResultSet rs = stmt.executeQuery(query);

                    String queryA = "select optionOne from Options where quizID >= '0'";
                    Statement stmtA = con.createStatement();
                    ResultSet rsA = stmtA.executeQuery(queryA);

                    String queryB = "select optionTwo from Options where quizID >= '0'";
                    Statement stmtB = con.createStatement();
                    ResultSet rsB = stmtB.executeQuery(queryB);


                    String queryC = "select optionThree from Options where quizID >= '0'";
                    Statement stmtC = con.createStatement();
                    ResultSet rsC = stmtC.executeQuery(queryC);
                    if (rs.next() | rsA.next() | rsB.next() | rsC.next()) {
                        z = rs.getString(id).toString();
                        zA=rsA.getString(id).toString();
                        zB=rsB.getString(id).toString();
                        zC=rsC.getString(id).toString();
                        isSuccess = true;
                    } 
                      else {
                        z = "Invalid Credentials";
                        isSuccess = false;
                    }
                }
            } catch (Exception ex) {
                isSuccess = false;
                z = "Exceptions";
            }
           return z;
        }

    }
}

我正在尝试尽可能简单地使用代码来访问数据。

请让我知道是否有人需要更多信息。

我的逻辑是: 我将更新'id'变量,然后再次执行该方法,它将使用新值更新视图。

顺便说一句,我正在使用Buttons显示选项。 数据库位于MS SQL Server中。 请指导。

1 个答案:

答案 0 :(得分:0)

您应该在初始化视图之前添加第一个AsyncTask调用,然后在每个下一个按钮上单击,然后将id递增1。

package com.example.android.quiz_app_androidmodule;

import android.support.v7.app.AppCompatActivity;import android.os.Bundle;
import android.os.AsyncTask;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;



public class MainActivity extends AppCompatActivity {
ConnectionClass connectionClass;
TextView Question, Score;
Button btnlogin, btnA, btnB, btnC, btnStart;

DoLogin doLogin ;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    connectionClass = new ConnectionClass();

    btnlogin = (Button) findViewById(R.id.btnStart);

    btnA = (Button) findViewById(R.id.button1);
    btnB = (Button) findViewById(R.id.button2);
    btnC = (Button) findViewById(R.id.button3);
    Question = (TextView) findViewById(R.id.textViewQuestion);
    Score = (TextView) findViewById(R.id.textViewScore);

    doLogin.execute(id);

    btnlogin.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

        //Make sure your first id is 1;
            id = id+1;
            dologin = = new DoLogin()
            doLogin.execute(id);

            btnA.setVisibility(View.VISIBLE);
            btnB.setVisibility(View.VISIBLE);
            btnC.setVisibility(View.VISIBLE);
            Question.setVisibility(View.VISIBLE);
            Score.setVisibility(View.VISIBLE);
            btnlogin.setVisibility(View.GONE);


        }
    });


}






String z = "";
String zA = "";
String zB="";
String zC="";
int id = 0;
int points;

public class DoLogin extends AsyncTask<String, String, String> {

    Boolean isSuccess = false;


    @Override
    protected void onPostExecute(String r) {


        if (isSuccess) {
          Question.setText(z);
            btnA.setText(zA);
            btnB.setText(zB);
            btnC.setText(zC);
        }

    }

    @Override
    protected String doInBackground(String... params) {

    String id = params[0];

        try {
            Connection con = connectionClass.CONN();
            if (con == null) {
                z = "Error in connection with SQL server";
            } else {
                String query = "select question from Question where quizID >= "+id;
                Statement stmt = con.createStatement();
                ResultSet rs = stmt.executeQuery(query);

                String queryA = "select optionOne from Options where quizID >= "+id;
                Statement stmtA = con.createStatement();
                ResultSet rsA = stmtA.executeQuery(queryA);

                String queryB = "select optionTwo from Options where quizID >= "+id;
                Statement stmtB = con.createStatement();
                ResultSet rsB = stmtB.executeQuery(queryB);


                String queryC = "select optionThree from Options where quizID >= "+id;
                Statement stmtC = con.createStatement();
                ResultSet rsC = stmtC.executeQuery(queryC);
                if (rs.next() | rsA.next() | rsB.next() | rsC.next()) {
                    z = rs.getString(id).toString();
                    zA=rsA.getString(id).toString();
                    zB=rsB.getString(id).toString();
                    zC=rsC.getString(id).toString();
                    isSuccess = true;
                } 
                  else {
                    z = "Invalid Credentials";
                    isSuccess = false;
                }
            }
        } catch (Exception ex) {
            isSuccess = false;
            z = "Exceptions";
        }
       return z;
    }

}
}