我正在创建一个包含不同问题类型的测验。要调用这些类型,我想在我的活动中使用Enum然后使用switch语句来使特定类型可见。
以下是我在课堂上的内容
public class Question {
private String question;
private String option1;
private String option2;
private String option3;
private int answerNumber;
private enum type {RADIO, CHECKBOX, TEXTENTRY};
public Question(){}
public Question(String question, String option1, String option2, String option3, int answerNumber, int type) {
this.question = question;
this.option1 = option1;
this.option2 = option2;
this.option3 = option3;
this.answerNumber = answerNumber;
this.type = type; // expression expected, not sure how to approach this
}
}
如果我尝试使用Android Studio生成它们,我手动创建了我的getter + setter,因为我收到此消息
'没有找到getter + setter的字段'
public int getType() {
return type;
}
public void setType(int type) {
this.type = type;
}
这就是我在DBHelper中所拥有的。
private void fillQuestionsTable() {
Question q1 = new Question("1 is correct", "a", "b","c",1,0);
addQuestion(q1);
Question q2 = new Question("2 is correct", "a", "b","c",2,1);
addQuestion(q2);
Question q3 = new Question("3 is correct", "a", "b","c",3,2);
addQuestion(q3);
}
private void addQuestion(Question question){
ContentValues cv = new ContentValues();
cv.put(QuestionsTable.COLUMN_QUESTION, question.getQuestion());
cv.put(QuestionsTable.COLUMN_OPTION1, question.getOption1());
cv.put(QuestionsTable.COLUMN_OPTION2, question.getOption2());
cv.put(QuestionsTable.COLUMN_OPTION3, question.getOption3());
cv.put(QuestionsTable.COLUMN_ANSWERNUMBER, question.getAnswerNumber());
cv.put(QuestionsTable.COLUMN_TYPE, question.getType());
db.insert(QuestionsTable.TABLE_NAME,null, cv);
}
public List<Question> getAllQuestions(){
List<Question> questionList = new ArrayList <>();
db = getReadableDatabase();
Cursor c = db.rawQuery("SELECT * FROM " + QuestionsTable.TABLE_NAME, null);
if (c.moveToFirst()){
do {
Question question = new Question();
question.setQuestion(c.getString(c.getColumnIndex(QuestionsTable.COLUMN_QUESTION)));
question.setOption1(c.getString(c.getColumnIndex(QuestionsTable.COLUMN_OPTION1)));
question.setOption2(c.getString(c.getColumnIndex(QuestionsTable.COLUMN_OPTION2)));
question.setOption3(c.getString(c.getColumnIndex(QuestionsTable.COLUMN_OPTION3)));
question.setAnswerNumber(c.getInt(c.getColumnIndex(QuestionsTable.COLUMN_ANSWERNUMBER)));
question.setType(c.getInt(c.getColumnIndex(QuestionsTable.COLUMN_TYPE)));
questionList.add(question);
} while (c.moveToNext());
}
c.close();
return questionList;
}
在我的活动中,这就是我用来尝试切换问题类型的方法 - 所有视图设置为INVISIBLE以启动然后使方法可见 -
private void showNextQuestion(){
if (questionCounter < getQuestionCounter){
currentQuestion = questionList.get(questionCounter);
question.setText(currentQuestion.getQuestion());
rb1.setText(currentQuestion.getOption1());
rb2.setText(currentQuestion.getOption2());
rb3.setText(currentQuestion.getOption3());
//switch question formats
int type = (int)
question.setText(currentQuestion.getQuestion()).questionList.get.questionType;
switch (questionType) {
// seems to like questionList better in here... still not sure how to bring up the enums
case Question.RADIO:
showRadioGroup();
break;
case Question.CHECKBOX:
showCheckboxes();
break;
case Question.TEXTENTRY:
showTypeAnswer();
break;
}
questionCounter++;
这个想法是从我的阵列中带来一切 - 是吗?这里有点困惑 - 如果我使用questionList这是我的数组无法解决 如果我使用getAllQuestions以防无法解决getAllQuestions 如果我使用typeAnswer或questionList以防无法解析RADIO或TEXTENTRY 对不起我正在尝试不同的方法。
也刚刚意识到我将很难尝试使用代码验证文本条目。正在努力。
答案 0 :(得分:0)
粗略地说,当你说private enum type {RADIO, CHECKBOX, TEXTENTRY};
时你可以(需要)然后使用类型就好像它是一个类一样。
所以你需要一个额外的行来声明一个类型的对象,因为它是一个成员(enum ??? {...}
是隐式的静态和最终的(如果我的理解是有限的话)),例如: -
private mytype = type;
这里有一些代码可以帮助您了解如何构建(以几种方式),使用枚举来获取和设置: -
public class Question {
public enum qtype {RADIO,CHECKBOX,TEXTENTRY} //<<< accessible outside perhaps prefereable
private enum altqtype {RADIO, CHECKBOX, TEXTENTRY} //<<<< Alternative if you want private (restrictive)
private String question;
private String option1;
private String option2;
private String option3;
private int answerNumber;
private long Id;
// Define members of the enum's
private qtype mytype;
private altqtype alternative_qtype;
private qtype another;
public Question(){}
public Question(
String question,
String option1,
String option2,
String option3,
int answerNumber,
int type, //<<<< pass as an int
String stype, //<<<< pass as a String
long id, //<<<< likely needed
qtype questiontype //<<<< pass as an qtype (e.g. qtype.RADIO)
) {
this.question = question;
this.option1 = option1;
this.option2 = option2;
this.option3 = option3;
this.answerNumber = answerNumber;
this.Id = id; //<<<< You may well need this to reference DB from Arraylist
// Setting enum's
this.mytype = questiontype; // set to passed value
this.alternative_qtype = altqtype.CHECKBOX; // Sets to default
// Set it accoring to the string passed
if (isValidTypeString(stype)) {
this.alternative_qtype = altqtype.valueOf(stype);
}
this.another = convertIntToQtype(type);
}
public qtype getMytype() {
return mytype;
}
public void setMytype(qtype mytype) {
this.mytype = mytype;
}
//Given an int convert it to a qtype
private qtype convertIntToQtype(int type){
qtype rv = qtype.TEXTENTRY;
switch (type) {
case 1:
rv = qtype.RADIO;
break;
case 2:
rv = qtype.CHECKBOX;
break;
case 4:
rv = qtype.TEXTENTRY;
break;
default:
rv = qtype.TEXTENTRY;
}
return rv;
}
// Check if a given string is valid as a question type
// Note done for alternative
private boolean isValidTypeString(String pv) {
boolean rv = false;
altqtype[] typearray = altqtype.values();
for (altqtype aqt: typearray) {
if (pv.equals(aqt.toString())) {
rv = true;
break;
}
}
return rv;
}
}
这是一个有效的例子: -
public class Question {
public enum qtype {RADIO,CHECKBOX,TEXTENTRY} //<<< accessible outside perhaps prefereable
private enum altqtype {RADIO, CHECKBOX, TEXTENTRY} //<<<< Alternative if you want private (restrictive)
private String question;
private String option1;
private String option2;
private String option3;
private int answerNumber;
private long rowid;
// Define members of the enum's
private qtype mytype;
private altqtype alternative_qtype;
private qtype another;
public Question(){}
public Question(
String question, String option1, String option2, String option3,
int answerNumber, qtype type, long id) {
this.question = question;
this.option1 = option1;
this.option2 = option2;
this.option3 = option3;
this.answerNumber = answerNumber;
this.mytype = type;
this.rowid = id;
}
public Question(
String question, String option1, String option2, String option3,
int answerNumber, qtype type
){
this(question,option1,option2,option3,answerNumber,type,-1);
}
//<<<< Note constrcutor for demonstration of enums
public Question(
String question,
String option1,
String option2,
String option3,
int answerNumber,
int type, //<<<< pass as an int
String stype, //<<<< pass as a String
long id, //<<<< likely needed
qtype questiontype //<<<< pass as an qtype (e.g. qtype.RADIO)
) {
this.question = question;
this.option1 = option1;
this.option2 = option2;
this.option3 = option3;
this.answerNumber = answerNumber;
this.rowid = id; //<<<< You may well need this to reference DB from Arraylist
// Setting enum's
this.mytype = questiontype; // set to passed value
this.alternative_qtype = altqtype.CHECKBOX; // Sets to default
// Set it accoring to the string passed
if (isValidTypeString(stype)) {
this.alternative_qtype = altqtype.valueOf(stype);
}
this.another = convertIntToQtype(type);
}
public qtype getMytype() {
return mytype;
}
public void setMytype(qtype mytype) {
this.mytype = mytype;
}
//Given an int convert it to a qtype
private qtype convertIntToQtype(int type){
qtype rv = qtype.TEXTENTRY;
switch (type) {
case 1:
rv = qtype.RADIO;
break;
case 2:
rv = qtype.CHECKBOX;
break;
case 4:
rv = qtype.TEXTENTRY;
break;
default:
rv = qtype.TEXTENTRY;
}
return rv;
}
// Check if a given string is valid as a question type
// Note done for alternative
private static boolean isValidTypeString(String pv) {
boolean rv = false;
altqtype[] typearray = altqtype.values();
for (altqtype aqt: typearray) {
if (pv.equals(aqt.toString())) {
rv = true;
break;
}
}
return rv;
}
public static qtype convertStringToQtype(String s) {
qtype rv = qtype.TEXTENTRY; // <<<< Default
if (isValidTypeString(s)) {
rv = qtype.valueOf(s);
}
return rv;
}
public String getQuestion() {
return question;
}
public void setQuestion(String question) {
this.question = question;
}
public String getOption1() {
return option1;
}
public void setOption1(String option1) {
this.option1 = option1;
}
public String getOption2() {
return option2;
}
public void setOption2(String option2) {
this.option2 = option2;
}
public String getOption3() {
return option3;
}
public void setOption3(String option3) {
this.option3 = option3;
}
public int getAnswerNumber() {
return answerNumber;
}
public void setAnswerNumber(int answerNumber) {
this.answerNumber = answerNumber;
}
public long getRowid() {
return rowid;
}
public void setRowid(long rowid) {
this.rowid = rowid;
}
}
public class QuestionsTable {
public static final String TABLE_NAME = "myquestions";
public static final String COLUMN_ID = BaseColumns._ID;
public static final String COLUMN_QUESTION = "question";
public static final String COLUMN_OPTION1 = "option1";
public static final String COLUMN_OPTION2 = "option2";
public static final String COLUMN_OPTION3 = "option3";
public static final String COLUMN_ANSWERNUMBER = "answernumber";
public static final String COLUMN_TYPE = "type";
}
public class DBHelper extends SQLiteOpenHelper {
public static final String DBNAME = "questions.sqlite";
public static final int DBVERSION = 1;
SQLiteDatabase db;
public DBHelper(Context context) {
super(context, DBNAME, null, DBVERSION);
db = this.getWritableDatabase();
}
@Override
public void onCreate(SQLiteDatabase db) {
String crtsql = "CREATE TABLE IF NOT EXISTS " +
QuestionsTable.TABLE_NAME +
"(" +
QuestionsTable.COLUMN_ID + " INTEGER PRIMARY KEY," +
QuestionsTable.COLUMN_QUESTION + " TEXT NOT NULL," +
QuestionsTable.COLUMN_OPTION1 + " TEXT NOT NULL," +
QuestionsTable.COLUMN_OPTION2 + " TEXT NOT NULL," +
QuestionsTable.COLUMN_OPTION3 + " TEXT NOT NULL," +
QuestionsTable.COLUMN_ANSWERNUMBER + " INTEGER NOT NULL," +
//<<<< NOTE will default to "TEXTENTRY"
QuestionsTable.COLUMN_TYPE + " TEXT NOT NULL DEFAULT '" +
Question.qtype.TEXTENTRY.toString() + "'" +
")";
db.execSQL(crtsql);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
public void addQuestion(Question question){
ContentValues cv = new ContentValues();
cv.put(QuestionsTable.COLUMN_QUESTION, question.getQuestion());
cv.put(QuestionsTable.COLUMN_OPTION1, question.getOption1());
cv.put(QuestionsTable.COLUMN_OPTION2, question.getOption2());
cv.put(QuestionsTable.COLUMN_OPTION3, question.getOption3());
cv.put(QuestionsTable.COLUMN_ANSWERNUMBER, question.getAnswerNumber());
cv.put(QuestionsTable.COLUMN_TYPE, String.valueOf(question.getMytype()));
//<<<< alternative cv.put(QuestionsTable.COLUMN_TYPE,question.getMytype().toString());
db.insert(QuestionsTable.TABLE_NAME,null, cv);
}
public Cursor getQuestionsAsCursor() {
return db.query(QuestionsTable.TABLE_NAME,null,null,null,null,null,null);
}
public ArrayList<Question> getQuestionsAsArrayList() {
ArrayList<Question> rv = new ArrayList<>();
Cursor csr = db.query(QuestionsTable.TABLE_NAME,null,null,null,null,null,null);
while (csr.moveToNext()) {
rv.add(new Question(
csr.getString(csr.getColumnIndex(QuestionsTable.COLUMN_QUESTION)),
csr.getString(csr.getColumnIndex(QuestionsTable.COLUMN_OPTION1)),
csr.getString(csr.getColumnIndex(QuestionsTable.COLUMN_OPTION2)),
csr.getString(csr.getColumnIndex(QuestionsTable.COLUMN_OPTION3)),
csr.getInt(csr.getColumnIndex(QuestionsTable.COLUMN_ANSWERNUMBER)),
//<<< get the String and convert to qtype
Question.convertStringToQtype(
csr.getString(
csr.getColumnIndex(QuestionsTable.COLUMN_TYPE)
)
),
csr.getLong(csr.getColumnIndex(QuestionsTable.COLUMN_ID))
));
}
csr.close();
return rv;
}
}
<?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">
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="4">
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="4"
android:text="Hello World!"/>
<Button
android:id="@+id/prevquestion"
android:layout_width="0dp"
android:layout_weight="4"
android:layout_height="wrap_content"
android:text="PREVIOUS"/>
<Button
android:id="@+id/nextquestion"
android:layout_width="0dp"
android:layout_weight="4"
android:layout_height="wrap_content"
android:text="NEXT"/>
</LinearLayout>
<ListView
android:id="@+id/questionlist_by_cursor"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_weight="41">
</ListView>
<ListView
android:id="@+id/questionlist_by_arraylist"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_weight="41"
android:background="#FFDDDDFF">
</ListView>
<LinearLayout
android:orientation="horizontal"
android:id="@+id/questiondisplay"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="14">
<TextView
android:id="@+id/questiontext"
android:layout_width="0dp"
android:layout_weight="8"
android:layout_height="wrap_content" />
<RadioButton
android:id="@+id/answerrb"
android:layout_width="0dp"
android:layout_weight="2"
android:layout_height="wrap_content"
android:visibility="gone"/>
<CheckBox
android:id="@+id/answercb"
android:layout_width="0dp"
android:layout_weight="2"
android:layout_height="wrap_content"
android:visibility="gone"/>
<EditText
android:id="@+id/answeret"
android:layout_width="0dp"
android:layout_weight="8"
android:layout_height="wrap_content"
android:visibility="gone"/>
</LinearLayout>
</LinearLayout>
public class MainActivity extends AppCompatActivity {
TextView mQuestionText;
Button mNext, mPrev;
RadioButton mRadio;
CheckBox mCheckBox;
EditText mTextEntry;
ListView mQuestionListByCursor,mQuestionListByArrayList;
DBHelper mDBHlpr;
Cursor mCsr;
SimpleCursorAdapter mSCA;
ArrayList<Question> mQuestionArrayList;
ArrayAdapter<Question> mArrayAdapter;
int mQuestionPointer = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mQuestionListByCursor = (ListView) this.findViewById(R.id.questionlist_by_cursor);
mQuestionListByArrayList = (ListView) this.findViewById(R.id.questionlist_by_arraylist);
mNext = (Button) this.findViewById(R.id.nextquestion);
mPrev = (Button) this.findViewById(R.id.prevquestion);
mRadio = (RadioButton) this.findViewById(R.id.answerrb);
mCheckBox = (CheckBox) this.findViewById(R.id.answercb);
mTextEntry = (EditText) this.findViewById(R.id.answeret);
mQuestionText = (TextView) this.findViewById(R.id.questiontext);
mDBHlpr = new DBHelper(this);
addSomeTestData(); //<<<< Add some test questions
mQuestionArrayList = mDBHlpr.getQuestionsAsArrayList(); //<<<< get the questions
refreshQuestion(); //<<<< Setup current (1st Question)
//Via Cursor
mCsr = mDBHlpr.getQuestionsAsCursor();
mSCA = new SimpleCursorAdapter(this,
android.R.layout.simple_list_item_2,
mCsr,
new String[]{QuestionsTable.COLUMN_QUESTION,QuestionsTable.COLUMN_TYPE},new int[]{android.R.id.text1,android.R.id.text2},0);
mQuestionListByCursor.setAdapter(mSCA);
//Via ArrayList
mArrayAdapter = new ArrayAdapter<Question>(
this,
android.R.layout.simple_list_item_1,mQuestionArrayList
);
mQuestionListByArrayList.setAdapter(mArrayAdapter);
mPrev.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(mQuestionPointer > 0) {
mQuestionPointer--;
refreshQuestion();
}
}
});
mNext.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(mQuestionPointer < (mQuestionArrayList.size() -1)) {
mQuestionPointer++;
refreshQuestion();
}
}
});
}
protected void onDestroy() {
super.onDestroy();
if (!mCsr.isClosed()) {
mCsr.close();
}
}
private void addSomeTestData() {
if (DatabaseUtils.queryNumEntries(
mDBHlpr.getWritableDatabase(),
QuestionsTable.TABLE_NAME
) < 1) {
mDBHlpr.addQuestion(new Question(
"What is 1 + 1?",
"0","1","2",
2,
Question.qtype.CHECKBOX //<<<< uses shorter signature
));
mDBHlpr.addQuestion(new Question(
"What is Tom's first name?",
"Fred","Tom","Bert",
1,
Question.qtype.TEXTENTRY //<<<< uses shorter signature
));
mDBHlpr.addQuestion(new Question(
"Where is Timbuktu?",
"Afirca","Russia","Australia",
1,
Question.qtype.RADIO //<<<< uses shorter signature
));
}
}
private void refreshQuestion() {
Question q = mQuestionArrayList.get(mQuestionPointer);
mQuestionText.setText(q.getQuestion());
switch (q.getMytype()) {
case TEXTENTRY:
mTextEntry.setVisibility(View.VISIBLE);
mRadio.setVisibility(View.GONE);
mCheckBox.setVisibility(View.GONE);
break;
case RADIO:
mRadio.setVisibility(View.VISIBLE);
mCheckBox.setVisibility(View.GONE);
mTextEntry.setVisibility(View.GONE);
break;
case CHECKBOX:
mCheckBox.setVisibility(View.VISIBLE);
mRadio.setVisibility(View.GONE);
mTextEntry.setVisibility(View.GONE);
break;
}
}
}
注释
SimpleCursorAdapter
(1)ArrayList<Question)
的列表(2)
toString
方法尚未被覆盖,因此默认的toString
方法会显示有趣的值。答案 1 :(得分:0)
这也很有效:
在 Question.java
enum QuestionType
{RADIO,CHECKBOX, TEXTENTRY}
public class Question {
...
private QuestionType type;
public Question(String question, String option1, String option2, String option3, int answerNumber,
QuestionType type) {
...
this.type = type;
}
//generate getters and setters using the generate tool in Android Studio
public QuestionType getType() {
return type;
}
public void setType(QuestionType type) {
this.type = type;
}
}
然后在
QuizDnHelper.java
@Override
public void onCreate(SQLiteDatabase db) {
this.db = db;
final String SQL_CREATE_QUESTIONS_TABLE = "CREATE TABLE " +
QuestionsTable.TABLE_NAME + " (" +
...
QuestionsTable.COLUMN_TYPE + " TEXT" + //not INTEGER!
")";
...
private void fillQuestionsTable(){
Question q1 = new Question("1 is correct", "a", "b","c",1,
QuestionType.RADIO);
private void addQuestion(Question question){
ContentValues cv = new ContentValues();
...
//set and get enum as string:
cv.put(QuestionsTable.COLUMN_TYPE, String.valueOf(question.getType()));
...
}
Cursor c = db.rawQuery("SELECT * FROM " + QuestionsTable.TABLE_NAME, null);
if (c.moveToFirst()){
do {
...
//set and get enum as string:
question.setType(QuestionType.valueOf(c.getString(c.getColumnIndex(QuestionsTable.COLUMN_TYPE))));
...
} while (c.moveToNext());
并将其称为MainActivity:
QuestionType.RADIO
....
我们使用枚举作为一个类。