如何根据 Select 查询的结果更新 TextView 中的文本
How to Update Text in TextView According to the Result of 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' 变量并再次执行该方法,它将使用新值更新视图。
顺便说一下,我正在使用按钮来显示选项。
数据库位于 MS SQL 服务器中。
请指导。
您应该在初始化视图之前添加第一个 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;
}
}
}
我是 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' 变量并再次执行该方法,它将使用新值更新视图。
顺便说一下,我正在使用按钮来显示选项。 数据库位于 MS SQL 服务器中。 请指导。
您应该在初始化视图之前添加第一个 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;
}
}
}