當我已經選擇了一個選擇時,我該如何開始一個新的問題?抱歉我的代碼有點亂,因爲我沒有把它放在另一個Java文件中。下一個問題android測驗
MainActivity.java
public class MainActivity extends AppCompatActivity {
public static final String MyPreferences = "MyPrefs";
TextView lbl;
DBController controller = new DBController(this);
private TextView etQuestion;
private TextView etAnswer1;
private TextView etAnswer2;
private TextView etAnswer3;
private TextView etAnswer4;
int score = 0;
int qid =3;
ListView lv;
final Context context = this;
ListAdapter adapter;
private Context myContext;
Question queryQ;
private static String DB_PATH = "/data/data/com.example.johnwhisker.schoolproject/databases/";
private static String DB_NAME ="school.db";
SQLiteDatabase myDataBase;
private String fileLink;
public static final int requestcode = 1;
SharedPreferences sharedPreferences;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_make_test);
etQuestion = (TextView) findViewById(R.id.etQuestion);
etAnswer1 = (TextView) findViewById(R.id.etAnswer1);
etAnswer2 = (TextView) findViewById(R.id.etAnswer2);
etAnswer3 = (TextView) findViewById(R.id.etAnswer3);
etAnswer4 = (TextView) findViewById(R.id.etAnswer4);
etAnswer1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
getAnswer(etAnswer1.getText().toString());
}
});
etAnswer2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
getAnswer(etAnswer2.getText().toString());
}
});
etAnswer3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
getAnswer(etAnswer3.getText().toString());
}
});
etAnswer4.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
getAnswer(etAnswer4.getText().toString());
}
});
try {
fileLink = Environment.getExternalStorageDirectory().getAbsolutePath();
Log.d("1234", "d1: " + fileLink);
File myDirs = new File(fileLink + "/databases");
if (!myDirs.exists())
myDirs.mkdirs();
fileLink = fileLink + "/databases/" + DB_NAME;
Log.d("1234", "d2: " + fileLink);
File dbFile = new File(fileLink);
if (!dbFile.exists()) {
copydatabase();
}
Log.d("1234", "Question" + dbFile.exists());
myDataBase = SQLiteDatabase.openDatabase(fileLink, null, SQLiteDatabase.OPEN_READONLY);
getAllProducts();
} catch (ActivityNotFoundException e) {
e.printStackTrace();
lbl.setText("No activity can handle picking a file. Showing alternatives.");
} catch (Exception e) {
e.printStackTrace();
}
}
public void cvChoice1OnSelect(View v){
Intent i = new Intent(this, MakeTestActivity.class);
i.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
startActivity(i);
overridePendingTransition(R.anim.left_to_right, R.anim.right_to_left);
}
public void cvChoice2OnSelect(View v){
Intent i = new Intent(this, AddQuestionActivity.class);
i.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
startActivity(i);
overridePendingTransition(R.anim.right_to_left, R.anim.left_to_right);
}
public List<Question> getAllProducts() {
List<Question> quesList = new ArrayList<Question>();
String selectQuery = "SELECT * FROM Question";
Question quest = new Question();
Cursor cursor = myDataBase.rawQuery(selectQuery, null);
if (cursor.moveToFirst()) {
do {
quest.setID(cursor.getInt(0));
quest.setQUESTION(cursor.getString(1));
quest.setOPTA(cursor.getString(2));
quest.setOPTB(cursor.getString(3));
quest.setOPTC(cursor.getString(4));
quest.setOPTD(cursor.getString(5));
quest.setANSWER(cursor.getString(6));
quesList.add(quest);
if(quest.getID()==1)
{etQuestion.setText(quest.getQUESTION());
etAnswer1.setText(quest.getOPTA());
etAnswer2.setText(quest.getOPTB());
etAnswer3.setText(quest.getOPTC());
etAnswer4.setText(quest.getANSWER());
}
} while (cursor.moveToNext());
}
return quesList;
}
public void getAnswer(String AnswerString) {
ArrayList<Question> quesList = new ArrayList<Question>();
String selectQuery = "SELECT * FROM Question";
Question quest = new Question();
Cursor cursor = myDataBase.rawQuery(selectQuery, null);
if (cursor.moveToFirst()) {
do {
quest.setID(cursor.getInt(0));
quest.setQUESTION(cursor.getString(1));
quest.setOPTA(cursor.getString(2));
quest.setOPTB(cursor.getString(3));
quest.setOPTC(cursor.getString(4));
quest.setOPTD(cursor.getString(5));
quest.setANSWER(cursor.getString(6));
} while (cursor.moveToNext());
}
if (quest.getANSWER().equals(AnswerString)) {
score++;
Toast.makeText(MainActivity.this,"YOUR SCORE IS NOW" +score,Toast.LENGTH_SHORT).show();
}
if (quest.getID() < 3) {
etQuestion.setText(quest.getQUESTION());
etAnswer1.setText(quest.getOPTA());
etAnswer2.setText(quest.getOPTB());
etAnswer3.setText(quest.getOPTC());
etAnswer4.setText(quest.getANSWER());
}
}
private void copydatabase() throws IOException {
InputStream myinput = getAssets().open(DB_NAME);
String outfilename = fileLink;
Log.d("1234", "e " + fileLink);
File file = new File(outfilename);
file.createNewFile();
OutputStream myoutput = new FileOutputStream(outfilename);
Log.d("1234", "rer55");
byte[] buffer = new byte[1024];
int length;
while ((length = myinput.read(buffer))>0) {
myoutput.write(buffer,0,length);
}
myoutput.flush();
myoutput.close();
myinput.close();
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (data == null)
return;
switch (requestCode) {
case requestcode:
String filepath = data.getData().getPath();
controller = new DBController(getApplicationContext());
SQLiteDatabase db = controller.getWritableDatabase();
String tableName = "Question";
db.execSQL("delete from " + tableName);
try {
if (resultCode == RESULT_OK) {
try {
FileReader file = new FileReader(filepath);
BufferedReader buffer = new BufferedReader(file);
ContentValues contentValues = new ContentValues();
String line = "";
db.beginTransaction();
while ((line = buffer.readLine()) != null) {
String[] str = line.split(",", 3);
Question quest = new Question();
contentValues.put("ID",quest.getID());
contentValues.put("QUESTION", quest.getQUESTION());
contentValues.put("A", quest.getOPTA());
contentValues.put("B", quest.getOPTB());
contentValues.put("C",quest.getOPTC());
contentValues.put("D",quest.getOPTD());
contentValues.put("ANSWER",quest.getANSWER());
db.insert(tableName, null, contentValues);
lbl.setText("Successfully Updated Database.");
}
db.setTransactionSuccessful();
db.endTransaction();
SharedPreferences prefs = this.getSharedPreferences(
MyPreferences, Context.MODE_PRIVATE);
prefs.edit().putBoolean("imported", true).commit();
} catch (IOException e) {
if (db.inTransaction())
db.endTransaction();
Dialog d = new Dialog(this);
d.setTitle(e.getMessage().toString() + "first");
d.show();
// db.endTransaction();
}
} else {
if (db.inTransaction())
db.endTransaction();
Dialog d = new Dialog(this);
d.setTitle("Only CSV files allowed");
d.show();
}
} catch (Exception ex) {
if (db.inTransaction())
db.endTransaction();
Dialog d = new Dialog(this);
d.setTitle(ex.getMessage().toString() + "second");
d.show();
// db.endTransaction();
}
}
}
}
Question.java
public class Question {
private int ID;
private String QUESTION;
private String OPTA;
private String OPTB;
private String OPTC;
private String OPTD;
private String ANSWER;
public Question()
{
ID=0;
QUESTION="";
OPTA="";
OPTB="";
OPTC="";
OPTD="";
ANSWER="";
}
public Question(String qUESTION, String oPTA, String oPTB, String oPTC,String oPTD,
String aNSWER) {QUESTION = qUESTION;
OPTA = oPTA;
OPTB = oPTB;
OPTC = oPTC;
OPTD = oPTD;
ANSWER = aNSWER;
}
public int getID()
{
return ID;
}
public String getQUESTION() {
return QUESTION;
}
public String getOPTA() {
return OPTA;
}
public String getOPTB() {
return OPTB;
}
public String getOPTC() {
return OPTC;
}
public String getOPTD(){return OPTD;}
public String getANSWER() {
return ANSWER;
}
public void setID(int id)
{
ID=id;
}
public void setQUESTION(String qUESTION) {
QUESTION = qUESTION;
}
public void setOPTA(String oPTA) {
OPTA = oPTA;
}
public void setOPTB(String oPTB) {
OPTB = oPTB;
}
public void setOPTC(String oPTC) {
OPTC = oPTC;
}
public void setOPTD(String oPTD)
{
OPTD = oPTD;
}
public void setANSWER(String aNSWER) {
ANSWER = aNSWER;
}
}'
這裏是我的代碼。
您想要做什麼? –
實際上,我試圖每當按下TextView按鈕並且if語句正確時就開始一個新問題。 –