即使从阵列(Android 工作室)中删除/修改/更改,相同的问题仍然会显示



这是从Android studio制作的

我有一个SQLite数据库,用于我的测验游戏的问题。

它基本上有效:它有 20 个问题供用户正确回答,但我想切换内容,我添加了一个难度设置简单 - 10 个简单问题中等 - 15 个问题难 - 20 个问题。

我只是为中等和困难的难度制作了另一个类,只是从原始数据库类中复制并粘贴了代码和问题,然后更改了问题。

成功了。但是问题仍然相同(所有20个问题仍然存在(。我什至尝试删除数组中的所有问题(当应该没有问题时,因为我将它们全部删除了(,并且那里有相同的 20 个问题。

TLDR:无论是否从数组中删除/添加/编辑,相同的 20 个问题仍然显示

private Context context;
private static final String DB_NAME = "TQuiz.db";
//If you want to add more questions or wanna update table values
//or any kind of modification in db just increment version no.
private static final int DB_VERSION = 3;
//Table name
private static final String TABLE_NAME = "TQ";
//Id of question
private static final String UID = "_UID";
//Question
private static final String QUESTION = "QUESTION";
//Option A
private static final String OPTA = "OPTA";
//Option B
private static final String OPTB = "OPTB";
//Option C
private static final String OPTC = "OPTC";
//Option D
private static final String OPTD = "OPTD";
//Answer
private static final String ANSWER = "ANSWER";
//So basically we are now creating table with first column-id , sec column-question , third column -option A, fourth column -option B , Fifth column -option C , sixth column -option D , seventh column - answer(i.e ans of  question)
private static final String CREATE_TABLE = "CREATE TABLE " + TABLE_NAME + " ( " + UID + " INTEGER PRIMARY KEY AUTOINCREMENT , " + QUESTION + " VARCHAR(255), " + OPTA + " VARCHAR(255), " + OPTB + " VARCHAR(255), " + OPTC + " VARCHAR(255), " + OPTD + " VARCHAR(255), " + ANSWER + " VARCHAR(255));";
//Drop table query
private static final String DROP_TABLE = "DROP TABLE IF EXISTS " + TABLE_NAME;
TriviaQuizHelper(Context context) {
super(context, DB_NAME, null, DB_VERSION);
this.context = context;
}
@Override
public void onCreate(SQLiteDatabase sqLiteDatabase) {
//OnCreate is called only once
sqLiteDatabase.execSQL(CREATE_TABLE);
}
@Override
public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {
//OnUpgrade is called when ever we upgrade or increment our database version no
sqLiteDatabase.execSQL(DROP_TABLE);
onCreate(sqLiteDatabase);
}
void allQuestion() {
ArrayList<TriviaQuestion> arraylist = new ArrayList<>();
arraylist.add(new TriviaQuestion("Galileo was an Italian astronomer who developed?" , "Telescope", "Airoplane", "Electricity", "Train", "Telescope"));
arraylist.add(new TriviaQuestion("Who is the father of Geometry ?" , "Aristotle", "Euclid", "Pythagoras", "Kepler", "Euclid"));
arraylist.add(new TriviaQuestion("Who was known as Iron man of India ?", "Govind Ballabh Pant", "Jawaharlal Nehru", "Subhash Chandra Bose", "Sardar Vallabhbhai Patel", "Sardar Vallabhbhai Patel"));
arraylist.add(new TriviaQuestion("The first woman in space was ?", "Valentina Tereshkova", "Sally Ride", "Naidia Comenci", "Tamara Press", "Valentina Tereshkova"));
arraylist.add(new TriviaQuestion("Who is the Flying Sikh of India ?", "Mohinder Singh", "Joginder Singh", "Ajit Pal Singh", "Milkha singh", "Milkha singh"));
arraylist.add(new TriviaQuestion("The Indian to beat the computers in mathematical wizardry is", "Ramanujam", "Rina Panigrahi", "Raja Ramanna", "Shakunthala Devi", "Shakunthala Devi"));
arraylist.add(new TriviaQuestion("Who is Larry Pressler ?", "Politician", "Painter", "Actor", "Tennis player", "Politician"));
arraylist.add(new TriviaQuestion("Michael Jackson is a distinguished person in the field of ?", "Pop Music", "Jounalism", "Sports", "Acting", "Pop Music"));
arraylist.add(new TriviaQuestion("The first Indian to swim across English channel was ?", "V. Merchant", "P. K. Banerji", "Mihir Sen", "Arati Saha", "Mihir Sen"));
arraylist.add(new TriviaQuestion("Who was the first Indian to make a movie?", "Dhundiraj Govind Phalke", " Asha Bhonsle", " Ardeshir Irani", "V. Shantaram", "Dhundiraj Govind Phalke"));
arraylist.add(new TriviaQuestion("Who is known as the ' Saint of the gutters ?", "B.R.Ambedkar", "Mother Teresa", "Mahatma Gandhi", "Baba Amte", "Mother Teresa"));
arraylist.add(new TriviaQuestion("Who invented the famous formula E=mc^2", "Albert Einstein", "Galilio", "Sarvesh", "Bill Gates", "Albert Einstein"));
arraylist.add(new TriviaQuestion("Who is elected as president of us 2016", "Donald Trump", "Hilary Clinton", "Jhon pol", "Barack Obama", "Donald Trump"));
arraylist.add(new TriviaQuestion("Who was the founder of company Microsoft", "Bill Gates", "Bill Clinton", "Jhon rio", "Steve jobs", "Bill Gates"));
arraylist.add(new TriviaQuestion("Who was the founder of company Apple ?", "Steve Jobs", "Steve Washinton", "Bill Gates", "Jobs Wills", "Steve Jobs"));
arraylist.add(new TriviaQuestion("Who was the founder of company Google ?", "Steve Jobs", "Bill Gates", "Larry Page", "Sundar Pichai", "Larry Page"));
arraylist.add(new TriviaQuestion("Who is know as god of cricket ?", "Sachin Tendulkar", "Kapil Dev", "Virat Koli", "Dhoni", "Sachin Tendulkar"));
arraylist.add(new TriviaQuestion("who has won ballon d'or of 2015 ?", "Lionel Messi", "Cristiano Ronaldo", "Neymar", "Kaka", "Lionel Messi"));
arraylist.add(new TriviaQuestion("who has won ballon d'or of 2014 ?", "Neymar", "Lionel Messi", "Cristiano Ronaldo", "Kaka", "Cristiano Ronaldo"));
arraylist.add(new TriviaQuestion("the Founder of the most famous gaming platform steam is ?",
"Bill Cliton", "Bill Williams", "Gabe Newell", "Bill Gates", "Gabe Newell"));
this.addAllQuestions(arraylist);
}

private void addAllQuestions(ArrayList<TriviaQuestion> allQuestions) {
SQLiteDatabase db = this.getWritableDatabase();
db.beginTransaction();
try {
ContentValues values = new ContentValues();
for (TriviaQuestion question : allQuestions) {
values.put(QUESTION, question.getQuestion());
values.put(OPTA, question.getOptA());
values.put(OPTB, question.getOptB());
values.put(OPTC, question.getOptC());
values.put(OPTD, question.getOptD());
values.put(ANSWER, question.getAnswer());
db.insert(TABLE_NAME, null, values);
}
db.setTransactionSuccessful();
} finally {
db.endTransaction();
db.close();
}
}

List<TriviaQuestion> getAllOfTheQuestions() {
List<TriviaQuestion> questionsList = new ArrayList<>();
SQLiteDatabase db = this.getWritableDatabase();
db.beginTransaction();
String column[] = {UID, QUESTION, OPTA, OPTB, OPTC, OPTD, ANSWER};
Cursor cursor = db.query(TABLE_NAME, column, null, null, null, null, null);

while (cursor.moveToNext()) {
TriviaQuestion question = new TriviaQuestion();
question.setId(cursor.getInt(0));
question.setQuestion(cursor.getString(1));
question.setOptA(cursor.getString(2));
question.setOptB(cursor.getString(3));
question.setOptC(cursor.getString(4));
question.setOptD(cursor.getString(5));
question.setAnswer(cursor.getString(6));
questionsList.add(question);
}
db.setTransactionSuccessful();
db.endTransaction();
cursor.close();
db.close();
return questionsList;
}}

如果您复制了数据库类,但忘记更改表名 - 这可能会导致数据重复。

最新更新