Те же вопросы по-прежнему отображаются, даже если удалить / изменить / изменить из массива (android studio) - PullRequest
0 голосов
/ 17 февраля 2020

Это сделано из 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 coloumn[] = {UID, QUESTION, OPTA, OPTB, OPTC, OPTD, ANSWER};
    Cursor cursor = db.query(TABLE_NAME, coloumn, 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;
}}

1 Ответ

0 голосов
/ 17 февраля 2020

Если вы скопировали свой класс базы данных, но забыли изменить имя таблицы - это может привести к дублированию данных.

...