My MainActivity
looks like this:
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.iq_test);
DBHandler db = new DBHandler(this);
// Inserting Questions
db.addQuestion(new Question("1. Question1", 1));
db.addQuestion(new Question("2. Question2", 2));
// Inserting Answers
db.addAnswer(new Answer("2", 1, 0));
db.addAnswer(new Answer("3", 1, 0));
db.addAnswer(new Answer("4", 1, 1));
db.addAnswer(new Answer("5", 1, 0));
// Reading all Questions
List<Question> questions = db.getAllQuestions();
// Reading all Answers
List<Answer> answers = db.getAllAnswers();
// other code
}
I've created a new java file and i moved some of the code there. My new activity looks like this:
public class DatabaseContent extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.iq_test);
DBHandler db = new DBHandler(this);
// Inserting Questions
db.addQuestion(new Question("1. Question1", 1));
db.addQuestion(new Question("2. Question2", 2));
// Inserting Answers
db.addAnswer(new Answer("2", 1, 0));
db.addAnswer(new Answer("3", 1, 0));
db.addAnswer(new Answer("4", 1, 1));
db.addAnswer(new Answer("5", 1, 0));
}
The ramaining code from the MainActivity
looks like this:
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.iq_test);
DBHandler db = new DBHandler(this);
// Reading all Questions
List<Question> questions = db.getAllQuestions();
// Reading all Answers
List<Answer> answers = db.getAllAnswers();
// other code
}
I want to insert records in database from the DatabaseContent
activity and in MainActivity
i want only to read them. I know that in java it's not like in php to include
files, but kind of. With this code, the records are not inserted in the database.
Also, when the MainActivity
is recreated, i want the data not to be inserted again. How can i do that? Thanks!