我需要在测验应用程序中隐藏一个单选按钮 android
I need to hide a radio button in a quiz app android
我想就我的 android 代码寻求一些帮助。我正在尝试使用 SQLite.Number 的可能答案不同来开发一个测验应用程序,所以如果答案例如是 3 而不是 4(默认情况下 4 是单选按钮的数量)。尝试使用:
if (DataContract.QuestionTable.COLUMN_ANSWER4.isEmpty ()){ radioButton4.setVisibility (View.INVISIBLE); }
但似乎 work.I 并不是 android 编程的新手,这是我的第一个项目,所以每条评论都会很有帮助。谢谢。
考虑到与问题一起发布的代码数量有限,这是一个包含建议技术的演示。
这使用两个 table 来符合标准化方法。 table 表示问题,table 表示答案。每个答案(子级)都有一个 link/map/relationship/association 和一个问题(父级)。为防止参照完整性问题,已使用外键约束。
首先是 问题、答案 和 QuestionWithAnswers 的几个 POJO(即它是答案)
问题POJO
class QuestionPOJO {
long questionId;
String questionText;
public QuestionPOJO(long questionId, String questionText) {
this.questionId = questionId;
this.questionText = questionText;
}
public long getQuestionId() {
return questionId;
}
public void setQuestionId(long questionId) {
this.questionId = questionId;
}
public String getQuestionText() {
return questionText;
}
public void setQuestionText(String questionText) {
this.questionText = questionText;
}
}
AnswerPOJO
class AnswerPOJO {
long answerId;
long questionMap;
String answerText;
boolean correct = false;
public AnswerPOJO(long answerid, long questionMap, String answerText, boolean correct) {
this.answerId = answerid;
this.questionMap = questionMap;
this.answerText = answerText;
this.correct = correct;
}
public long getAnswerId() {
return answerId;
}
public void setAnswerId(long answerId) {
this.answerId = answerId;
}
public long getQuestionMap() {
return questionMap;
}
public void setQuestionMap(long questionMap) {
this.questionMap = questionMap;
}
public boolean isCorrect() {
return correct;
}
public void setCorrect(boolean correct) {
this.correct = correct;
}
public String getAnswerText() {
return answerText;
}
public void setAnswerText(String answerText) {
this.answerText = answerText;
}
}
和QuestionWithAnswerPOJO
class QuestionWithAnswersPOJO {
QuestionPOJO question;
ArrayList<AnswerPOJO> answers = new ArrayList<>();
public QuestionWithAnswersPOJO(QuestionPOJO question, List<AnswerPOJO> answers) {
this.question = question;
this.answers.clear();
this.answers.addAll(answers);
}
public QuestionPOJO getQuestion() {
return question;
}
public void setQuestion(QuestionPOJO question) {
this.question = question;
}
public ArrayList<AnswerPOJO> getAnswers() {
return answers;
}
public void setAnswers(ArrayList<AnswerPOJO> answers) {
this.answers = answers;
}
}
现在是处理所有数据库的 DatabaseHelper activity。包括添加和获取问题和答案的方法,特别是获取属于问题的答案的方法。
class DatabaseHelper extends SQLiteOpenHelper {
public static final String DATABASE_NAME = "questions.db";
public static final int DATABASE_VERSION = 1;
public static final String QUESTION_TABLE = "question";
public static final String QUESTION_ID_COLUMN = BaseColumns._ID;
public static final String QUESTION_TEXT_COLUMN = "question_text";
public static final String ANSWER_TABLE = "answer";
public static final String ANSWER_ID_COLUMN = BaseColumns._ID;
public static final String ANSWER_TEXT_COLUMN = "answer_text";
public static final String ANSWER_QUESTION_MAP_COLUMN = "question_map";
public static final String ANSWER_CORRECT_COLUMN = "correct";
private static volatile DatabaseHelper instance = null;
private DatabaseHelper(Context context) {
super(context,DATABASE_NAME,null,DATABASE_VERSION);
}
private static SQLiteDatabase db = null;
public static DatabaseHelper getInstance(Context context, boolean forceOpen) {
if (instance == null) {
instance = new DatabaseHelper(context);
}
if (forceOpen) {
db = instance.getWritableDatabase();
}
return instance;
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("CREATE TABLE IF NOT EXISTS " + QUESTION_TABLE + "(" +
QUESTION_ID_COLUMN + " INTEGER PRIMARY KEY," +
QUESTION_TEXT_COLUMN + " TEXT" +
");");
db.execSQL("CREATE TABLE IF NOT EXISTS " + ANSWER_TABLE + "(" +
ANSWER_ID_COLUMN + " INTEGER PRIMARY KEY," +
ANSWER_TEXT_COLUMN + " TEXT," +
ANSWER_QUESTION_MAP_COLUMN + " INTEGER REFERENCES " + QUESTION_TABLE + "(" + QUESTION_ID_COLUMN + ") ON DELETE CASCADE ON UPDATE CASCADE," +
ANSWER_CORRECT_COLUMN + " INTEGER DEFAULT 0 /* false */" +
");");
}
@Override
public void onUpgrade(SQLiteDatabase db, int i, int i1) {
}
// Fully comprehensive Question insert
public long insertQuestion(Long questionId, String questionText) {
init_db_AsWriteableDatabase();
long rv = -2;
ContentValues cv = new ContentValues();
if (questionId != null && questionId > 0) {
cv.put(QUESTION_ID_COLUMN,questionId);
}
if (questionText != null && questionText.length() > 0) {
cv.put(QUESTION_TEXT_COLUMN,questionText);
}
if (cv.size() > 0) {
rv = db.insert(QUESTION_TABLE,null,cv);
}
return rv;
}
// Concise Question insert
public long insertQuestion(String questionText) {
return insertQuestion(null,questionText);
}
// Fully comprehensive Answer insert
public long insertAnswer(Long answerId, long questionMap, String answerText, boolean correct) {
init_db_AsWriteableDatabase();
long rv = -2;
ContentValues cv = new ContentValues();
if (answerId != null && answerId > 0) {
cv.put(ANSWER_ID_COLUMN,answerId);
}
if (answerText != null && answerText.length() > 0) {
cv.put(ANSWER_CORRECT_COLUMN,correct);
cv.put(ANSWER_TEXT_COLUMN,answerText);
cv.put(ANSWER_QUESTION_MAP_COLUMN,questionMap);
rv = db.insert(ANSWER_TABLE,null,cv);
}
return rv;
}
// long concise Answer insert
public long insertAnswer(long questionMap, String answerText, boolean correct) {
return insertAnswer(null,questionMap,answerText,correct);
}
public long insertAnswer(long questionMap, String answerText) {
return insertAnswer(questionMap,answerText,false);
}
// short concise Answer insert
private void init_db_AsWriteableDatabase() {
if (db == null) {
db = this.getWritableDatabase();
}
}
@SuppressLint("Range")
public QuestionPOJO getQuestionById(long questionId) {
QuestionPOJO rv = null;
init_db_AsWriteableDatabase();
Cursor csr = db.query(QUESTION_TABLE,null,QUESTION_ID_COLUMN+"=?",new String[]{String.valueOf(questionId)},null,null,null);
if (csr.moveToNext()) {
rv = new QuestionPOJO(csr.getLong(csr.getColumnIndex(QUESTION_ID_COLUMN)),csr.getString(csr.getColumnIndex(QUESTION_TEXT_COLUMN)));
}
csr.close();
return rv;
}
@SuppressLint("Range")
public List<AnswerPOJO> getAnswersForQuestion(long questionId, boolean onlyCorrect) {
init_db_AsWriteableDatabase();
ArrayList<AnswerPOJO> rv = new ArrayList<>();
StringBuilder whereClause = new StringBuilder();
whereClause.append(ANSWER_QUESTION_MAP_COLUMN + "=?");
if (onlyCorrect) {
whereClause.append(" AND ").append(ANSWER_CORRECT_COLUMN);
}
Cursor csr = db.query(ANSWER_TABLE,null,whereClause.toString(),new String[]{String.valueOf(questionId)},null,null,null);
while (csr.moveToNext()) {
rv.add(
new AnswerPOJO(
csr.getLong(csr.getColumnIndex(ANSWER_ID_COLUMN)),
questionId,
csr.getString(csr.getColumnIndex(ANSWER_TEXT_COLUMN)),
csr.getInt(csr.getColumnIndex(ANSWER_CORRECT_COLUMN)) > 0
)
);
}
csr.close();
return rv;
}
}
最后将它们放在一起,activity 具有 4 个单选按钮的布局以及作为 TextView 的相关答案。
所以布局是:-
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!">
</TextView>
<RadioGroup
android:id="@+id/question_radiogroup"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<RadioButton
android:id="@+id/answer1_radioButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
tools:ignore="DuplicateSpeakableTextCheck"></RadioButton>
<TextView
android:id="@+id/answer1_textview"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</TextView>
<RadioButton
android:id="@+id/answer2_radioButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</RadioButton>
<TextView
android:id="@+id/answer2_textview"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</TextView>
<RadioButton
android:id="@+id/answer3_radioButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</RadioButton>
<TextView
android:id="@+id/answer3_textview"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</TextView>
<RadioButton
android:id="@+id/answer4_radioButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</RadioButton>
<TextView
android:id="@+id/answer4_textview"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</TextView>
</RadioGroup>
</LinearLayout>
- 简单到足以显示如何根据答案的数量隐藏按钮(或根据 Buttons/Textviews 的数量限制)
和activity来演示:-
public class MainActivity extends AppCompatActivity {
DatabaseHelper dbHelper;
RadioButton[] radiobuttons = new RadioButton[4];
TextView[] answerTexts = new TextView[4];
QuestionWithAnswersPOJO currentQuestionWithAnswers;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
/*
prepare Radion Buttons and associated TextViews
*/
radiobuttons[0] = this.findViewById(R.id.answer1_radioButton);
radiobuttons[1] = this.findViewById(R.id.answer2_radioButton);
radiobuttons[2] = this.findViewById(R.id.answer3_radioButton);
radiobuttons[3] = this.findViewById(R.id.answer4_radioButton);
answerTexts[0] = this.findViewById(R.id.answer1_textview);
answerTexts[1] = this.findViewById(R.id.answer2_textview);
answerTexts[2] = this.findViewById(R.id.answer3_textview);
answerTexts[3] = this.findViewById(R.id.answer4_textview);
/*
Prepare to use Database
*/
dbHelper = DatabaseHelper.getInstance(this,true);
SQLiteDatabase db = dbHelper.getWritableDatabase();
/* Clear any existing data for consistent results */
db.delete(DatabaseHelper.ANSWER_TABLE,null,null);
db.delete(DatabaseHelper.QUESTION_TABLE,null,null);
/* Add some demo data 1 question with 5 answers (comment out to reduce number of answers)*/
long q1id = dbHelper.insertQuestion("What is 1 + 1");
dbHelper.insertAnswer(q1id,"It is 1");
dbHelper.insertAnswer(q1id,"it is 2", true);
dbHelper.insertAnswer(q1id,"It is 3");
dbHelper.insertAnswer(q1id,"It is 4");
dbHelper.insertAnswer(q1id,"It is something Else"); // will be ignored because only 4 radio buttons
/* Get the question with the answers, however many ot has */
currentQuestionWithAnswers = new QuestionWithAnswersPOJO(dbHelper.getQuestionById(q1id),dbHelper.getAnswersForQuestion(q1id,false));
/* setup (refresh) the radio buttons and textviews */
getAndSetRadioButtons(currentQuestionWithAnswers);
}
/*
Does as it says
*/
private void getAndSetRadioButtons(QuestionWithAnswersPOJO qwa) {
// Clear All
for (int i=0;i < radiobuttons.length; i++) {
radiobuttons[i].setVisibility(View.GONE);
answerTexts[i].setText("");
answerTexts[i].setVisibility(View.GONE);
}
/* show only the least of answers/radio buttons */
int numberToShow = radiobuttons.length;
if (qwa.answers.size() < radiobuttons.length) {
numberToShow = qwa.answers.size();
}
int i = 0;
for(AnswerPOJO a: qwa.answers) {
if (i < numberToShow) {
radiobuttons[i].setVisibility(View.VISIBLE);
answerTexts[i].setVisibility(View.VISIBLE);
answerTexts[i++].setText(a.answerText);
} else {
/* nothing more to do so early break out of the loop */
break;
}
}
}
}
结果
结果 1 - 运行 有 5 个答案 (参见第 5 个答案的评论)
结果 2 - 所有栏 1 的答案都注释掉所以问题只有 1 个答案
使用的代码:-
long q1id = dbHelper.insertQuestion("What is 1 + 1");
dbHelper.insertAnswer(q1id,"It is 1");
//dbHelper.insertAnswer(q1id,"it is 2", true);
//dbHelper.insertAnswer(q1id,"It is 3");
//dbHelper.insertAnswer(q1id,"It is 4");
//dbHelper.insertAnswer(q1id,"It is something Else"); // will be ignored because only 4 radio buttons
显示:-
结果 3 - 只插入了第一个和第五个答案
等等。
代码发布后追加。
附加答案,因为代码现已添加。
你说:-
Tried to use:
if (DataContract.QuestionTable.COLUMN_ANSWER4.isEmpty ()){ radioButton4.setVisibility (View.INVISIBLE); }
but does not seem to work.
如果 DataContract.QuestionTable.COLUMN_ANSWER4 为空,那么将面临更严重的问题,因为它是 列名 .
您应该检查从数据库中检索并放入 currentQuestion 中的列的内容,以便检查问题。
我相信使用(旧代码已注释掉):-
radioButton1.setText(currentQuestion.getAnswer1 ());
radioButton2.setText(currentQuestion.getAnswer2 ());
radioButton3.setText(currentQuestion.getAnswer3 ());
radioButton4.setText(currentQuestion.getAnswer4 ());
if(currentQuestion.getAnswer4() == null || currentQuestion.getAnswer4().isEmpty()) {
radioButton4.setVisibility(View.INVISIBLE);
} else {
radioButton4.setVisibility(View.VISIBLE); // just in case it is INVISIBLE
radioButton4.setText(currentQuestion.getAnswer4());
}
/*
if (DataContract.QuestionTable.COLUMN_ANSWER4.isEmpty ()){
radioButton4.setVisibility (View.INVISIBLE);
} else {
radioButton4.setText(currentQuestion.getAnswer4 ());
}
*/
将解决此问题。例如在第三季度:-
我想就我的 android 代码寻求一些帮助。我正在尝试使用 SQLite.Number 的可能答案不同来开发一个测验应用程序,所以如果答案例如是 3 而不是 4(默认情况下 4 是单选按钮的数量)。尝试使用:
if (DataContract.QuestionTable.COLUMN_ANSWER4.isEmpty ()){ radioButton4.setVisibility (View.INVISIBLE); }
但似乎 work.I 并不是 android 编程的新手,这是我的第一个项目,所以每条评论都会很有帮助。谢谢。
考虑到与问题一起发布的代码数量有限,这是一个包含建议技术的演示。
这使用两个 table 来符合标准化方法。 table 表示问题,table 表示答案。每个答案(子级)都有一个 link/map/relationship/association 和一个问题(父级)。为防止参照完整性问题,已使用外键约束。
首先是 问题、答案 和 QuestionWithAnswers 的几个 POJO(即它是答案)
问题POJO
class QuestionPOJO {
long questionId;
String questionText;
public QuestionPOJO(long questionId, String questionText) {
this.questionId = questionId;
this.questionText = questionText;
}
public long getQuestionId() {
return questionId;
}
public void setQuestionId(long questionId) {
this.questionId = questionId;
}
public String getQuestionText() {
return questionText;
}
public void setQuestionText(String questionText) {
this.questionText = questionText;
}
}
AnswerPOJO
class AnswerPOJO {
long answerId;
long questionMap;
String answerText;
boolean correct = false;
public AnswerPOJO(long answerid, long questionMap, String answerText, boolean correct) {
this.answerId = answerid;
this.questionMap = questionMap;
this.answerText = answerText;
this.correct = correct;
}
public long getAnswerId() {
return answerId;
}
public void setAnswerId(long answerId) {
this.answerId = answerId;
}
public long getQuestionMap() {
return questionMap;
}
public void setQuestionMap(long questionMap) {
this.questionMap = questionMap;
}
public boolean isCorrect() {
return correct;
}
public void setCorrect(boolean correct) {
this.correct = correct;
}
public String getAnswerText() {
return answerText;
}
public void setAnswerText(String answerText) {
this.answerText = answerText;
}
}
和QuestionWithAnswerPOJO
class QuestionWithAnswersPOJO {
QuestionPOJO question;
ArrayList<AnswerPOJO> answers = new ArrayList<>();
public QuestionWithAnswersPOJO(QuestionPOJO question, List<AnswerPOJO> answers) {
this.question = question;
this.answers.clear();
this.answers.addAll(answers);
}
public QuestionPOJO getQuestion() {
return question;
}
public void setQuestion(QuestionPOJO question) {
this.question = question;
}
public ArrayList<AnswerPOJO> getAnswers() {
return answers;
}
public void setAnswers(ArrayList<AnswerPOJO> answers) {
this.answers = answers;
}
}
现在是处理所有数据库的 DatabaseHelper activity。包括添加和获取问题和答案的方法,特别是获取属于问题的答案的方法。
class DatabaseHelper extends SQLiteOpenHelper {
public static final String DATABASE_NAME = "questions.db";
public static final int DATABASE_VERSION = 1;
public static final String QUESTION_TABLE = "question";
public static final String QUESTION_ID_COLUMN = BaseColumns._ID;
public static final String QUESTION_TEXT_COLUMN = "question_text";
public static final String ANSWER_TABLE = "answer";
public static final String ANSWER_ID_COLUMN = BaseColumns._ID;
public static final String ANSWER_TEXT_COLUMN = "answer_text";
public static final String ANSWER_QUESTION_MAP_COLUMN = "question_map";
public static final String ANSWER_CORRECT_COLUMN = "correct";
private static volatile DatabaseHelper instance = null;
private DatabaseHelper(Context context) {
super(context,DATABASE_NAME,null,DATABASE_VERSION);
}
private static SQLiteDatabase db = null;
public static DatabaseHelper getInstance(Context context, boolean forceOpen) {
if (instance == null) {
instance = new DatabaseHelper(context);
}
if (forceOpen) {
db = instance.getWritableDatabase();
}
return instance;
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("CREATE TABLE IF NOT EXISTS " + QUESTION_TABLE + "(" +
QUESTION_ID_COLUMN + " INTEGER PRIMARY KEY," +
QUESTION_TEXT_COLUMN + " TEXT" +
");");
db.execSQL("CREATE TABLE IF NOT EXISTS " + ANSWER_TABLE + "(" +
ANSWER_ID_COLUMN + " INTEGER PRIMARY KEY," +
ANSWER_TEXT_COLUMN + " TEXT," +
ANSWER_QUESTION_MAP_COLUMN + " INTEGER REFERENCES " + QUESTION_TABLE + "(" + QUESTION_ID_COLUMN + ") ON DELETE CASCADE ON UPDATE CASCADE," +
ANSWER_CORRECT_COLUMN + " INTEGER DEFAULT 0 /* false */" +
");");
}
@Override
public void onUpgrade(SQLiteDatabase db, int i, int i1) {
}
// Fully comprehensive Question insert
public long insertQuestion(Long questionId, String questionText) {
init_db_AsWriteableDatabase();
long rv = -2;
ContentValues cv = new ContentValues();
if (questionId != null && questionId > 0) {
cv.put(QUESTION_ID_COLUMN,questionId);
}
if (questionText != null && questionText.length() > 0) {
cv.put(QUESTION_TEXT_COLUMN,questionText);
}
if (cv.size() > 0) {
rv = db.insert(QUESTION_TABLE,null,cv);
}
return rv;
}
// Concise Question insert
public long insertQuestion(String questionText) {
return insertQuestion(null,questionText);
}
// Fully comprehensive Answer insert
public long insertAnswer(Long answerId, long questionMap, String answerText, boolean correct) {
init_db_AsWriteableDatabase();
long rv = -2;
ContentValues cv = new ContentValues();
if (answerId != null && answerId > 0) {
cv.put(ANSWER_ID_COLUMN,answerId);
}
if (answerText != null && answerText.length() > 0) {
cv.put(ANSWER_CORRECT_COLUMN,correct);
cv.put(ANSWER_TEXT_COLUMN,answerText);
cv.put(ANSWER_QUESTION_MAP_COLUMN,questionMap);
rv = db.insert(ANSWER_TABLE,null,cv);
}
return rv;
}
// long concise Answer insert
public long insertAnswer(long questionMap, String answerText, boolean correct) {
return insertAnswer(null,questionMap,answerText,correct);
}
public long insertAnswer(long questionMap, String answerText) {
return insertAnswer(questionMap,answerText,false);
}
// short concise Answer insert
private void init_db_AsWriteableDatabase() {
if (db == null) {
db = this.getWritableDatabase();
}
}
@SuppressLint("Range")
public QuestionPOJO getQuestionById(long questionId) {
QuestionPOJO rv = null;
init_db_AsWriteableDatabase();
Cursor csr = db.query(QUESTION_TABLE,null,QUESTION_ID_COLUMN+"=?",new String[]{String.valueOf(questionId)},null,null,null);
if (csr.moveToNext()) {
rv = new QuestionPOJO(csr.getLong(csr.getColumnIndex(QUESTION_ID_COLUMN)),csr.getString(csr.getColumnIndex(QUESTION_TEXT_COLUMN)));
}
csr.close();
return rv;
}
@SuppressLint("Range")
public List<AnswerPOJO> getAnswersForQuestion(long questionId, boolean onlyCorrect) {
init_db_AsWriteableDatabase();
ArrayList<AnswerPOJO> rv = new ArrayList<>();
StringBuilder whereClause = new StringBuilder();
whereClause.append(ANSWER_QUESTION_MAP_COLUMN + "=?");
if (onlyCorrect) {
whereClause.append(" AND ").append(ANSWER_CORRECT_COLUMN);
}
Cursor csr = db.query(ANSWER_TABLE,null,whereClause.toString(),new String[]{String.valueOf(questionId)},null,null,null);
while (csr.moveToNext()) {
rv.add(
new AnswerPOJO(
csr.getLong(csr.getColumnIndex(ANSWER_ID_COLUMN)),
questionId,
csr.getString(csr.getColumnIndex(ANSWER_TEXT_COLUMN)),
csr.getInt(csr.getColumnIndex(ANSWER_CORRECT_COLUMN)) > 0
)
);
}
csr.close();
return rv;
}
}
最后将它们放在一起,activity 具有 4 个单选按钮的布局以及作为 TextView 的相关答案。
所以布局是:-
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!">
</TextView>
<RadioGroup
android:id="@+id/question_radiogroup"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<RadioButton
android:id="@+id/answer1_radioButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
tools:ignore="DuplicateSpeakableTextCheck"></RadioButton>
<TextView
android:id="@+id/answer1_textview"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</TextView>
<RadioButton
android:id="@+id/answer2_radioButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</RadioButton>
<TextView
android:id="@+id/answer2_textview"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</TextView>
<RadioButton
android:id="@+id/answer3_radioButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</RadioButton>
<TextView
android:id="@+id/answer3_textview"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</TextView>
<RadioButton
android:id="@+id/answer4_radioButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</RadioButton>
<TextView
android:id="@+id/answer4_textview"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</TextView>
</RadioGroup>
</LinearLayout>
- 简单到足以显示如何根据答案的数量隐藏按钮(或根据 Buttons/Textviews 的数量限制)
和activity来演示:-
public class MainActivity extends AppCompatActivity {
DatabaseHelper dbHelper;
RadioButton[] radiobuttons = new RadioButton[4];
TextView[] answerTexts = new TextView[4];
QuestionWithAnswersPOJO currentQuestionWithAnswers;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
/*
prepare Radion Buttons and associated TextViews
*/
radiobuttons[0] = this.findViewById(R.id.answer1_radioButton);
radiobuttons[1] = this.findViewById(R.id.answer2_radioButton);
radiobuttons[2] = this.findViewById(R.id.answer3_radioButton);
radiobuttons[3] = this.findViewById(R.id.answer4_radioButton);
answerTexts[0] = this.findViewById(R.id.answer1_textview);
answerTexts[1] = this.findViewById(R.id.answer2_textview);
answerTexts[2] = this.findViewById(R.id.answer3_textview);
answerTexts[3] = this.findViewById(R.id.answer4_textview);
/*
Prepare to use Database
*/
dbHelper = DatabaseHelper.getInstance(this,true);
SQLiteDatabase db = dbHelper.getWritableDatabase();
/* Clear any existing data for consistent results */
db.delete(DatabaseHelper.ANSWER_TABLE,null,null);
db.delete(DatabaseHelper.QUESTION_TABLE,null,null);
/* Add some demo data 1 question with 5 answers (comment out to reduce number of answers)*/
long q1id = dbHelper.insertQuestion("What is 1 + 1");
dbHelper.insertAnswer(q1id,"It is 1");
dbHelper.insertAnswer(q1id,"it is 2", true);
dbHelper.insertAnswer(q1id,"It is 3");
dbHelper.insertAnswer(q1id,"It is 4");
dbHelper.insertAnswer(q1id,"It is something Else"); // will be ignored because only 4 radio buttons
/* Get the question with the answers, however many ot has */
currentQuestionWithAnswers = new QuestionWithAnswersPOJO(dbHelper.getQuestionById(q1id),dbHelper.getAnswersForQuestion(q1id,false));
/* setup (refresh) the radio buttons and textviews */
getAndSetRadioButtons(currentQuestionWithAnswers);
}
/*
Does as it says
*/
private void getAndSetRadioButtons(QuestionWithAnswersPOJO qwa) {
// Clear All
for (int i=0;i < radiobuttons.length; i++) {
radiobuttons[i].setVisibility(View.GONE);
answerTexts[i].setText("");
answerTexts[i].setVisibility(View.GONE);
}
/* show only the least of answers/radio buttons */
int numberToShow = radiobuttons.length;
if (qwa.answers.size() < radiobuttons.length) {
numberToShow = qwa.answers.size();
}
int i = 0;
for(AnswerPOJO a: qwa.answers) {
if (i < numberToShow) {
radiobuttons[i].setVisibility(View.VISIBLE);
answerTexts[i].setVisibility(View.VISIBLE);
answerTexts[i++].setText(a.answerText);
} else {
/* nothing more to do so early break out of the loop */
break;
}
}
}
}
结果
结果 1 - 运行 有 5 个答案 (参见第 5 个答案的评论)
结果 2 - 所有栏 1 的答案都注释掉所以问题只有 1 个答案
使用的代码:-
long q1id = dbHelper.insertQuestion("What is 1 + 1");
dbHelper.insertAnswer(q1id,"It is 1");
//dbHelper.insertAnswer(q1id,"it is 2", true);
//dbHelper.insertAnswer(q1id,"It is 3");
//dbHelper.insertAnswer(q1id,"It is 4");
//dbHelper.insertAnswer(q1id,"It is something Else"); // will be ignored because only 4 radio buttons
显示:-
结果 3 - 只插入了第一个和第五个答案
等等。
代码发布后追加。
附加答案,因为代码现已添加。
你说:-
Tried to use:
if (DataContract.QuestionTable.COLUMN_ANSWER4.isEmpty ()){ radioButton4.setVisibility (View.INVISIBLE); }
but does not seem to work.
如果 DataContract.QuestionTable.COLUMN_ANSWER4 为空,那么将面临更严重的问题,因为它是 列名 .
您应该检查从数据库中检索并放入 currentQuestion 中的列的内容,以便检查问题。
我相信使用(旧代码已注释掉):-
radioButton1.setText(currentQuestion.getAnswer1 ());
radioButton2.setText(currentQuestion.getAnswer2 ());
radioButton3.setText(currentQuestion.getAnswer3 ());
radioButton4.setText(currentQuestion.getAnswer4 ());
if(currentQuestion.getAnswer4() == null || currentQuestion.getAnswer4().isEmpty()) {
radioButton4.setVisibility(View.INVISIBLE);
} else {
radioButton4.setVisibility(View.VISIBLE); // just in case it is INVISIBLE
radioButton4.setText(currentQuestion.getAnswer4());
}
/*
if (DataContract.QuestionTable.COLUMN_ANSWER4.isEmpty ()){
radioButton4.setVisibility (View.INVISIBLE);
} else {
radioButton4.setText(currentQuestion.getAnswer4 ());
}
*/
将解决此问题。例如在第三季度:-