简体   繁体   中英

How to: Android Quiz App, extracting da

I'm looking to create a 'quiz' application that will have multiple categories, in those categories will be questions that the user will be able to answer in order and their progress will be saved. I want the progress to be saved locally so it can be used without the internet but I would also like to access some of the information that is being saved if possible, such as what users have completed what category?

I have Java and SQL knowledge but I'm pretty confused about the whole thing when it comes to adding them together.

Do I need to implement an SQLite database in the app to store progress for the users, as well as a regular SQL server that will store the required data online? I was hoping it would be possible just to use the SQLite database to do both of these but I've looked and I can't figure out if it is possible.

Thanks.

An SQLite database is only for local storage, meaning you won't be able to use it to communicate with other devices.

Here is a sample of my java and SQLite interaction

public List multiplyFat(){

String totalFat = "SELECT " + total + " FROM " + MyTable + " WHERE ( "
    + gas + " + " + shopping + " ) > 1200"; 

SQLiteDatabase db = this.getReadableDatabase(); 

As we need to open a readable database session

List<Food> storeTotalFat = new ArrayList<>(); 

We create an array to store the data coming from the SQLite database

Cursor cursor = db.rawQuery(totalFat, null);

Treat the cursor as a means to lead the code through all the rows produced by the query above

cursor.moveToFirst();
 while (!cursor.isAfterLast()) {
  do {
     double fat = Double.parseDouble(cursor.getString(0));
      storeTotalFat.add(new Food(fat));
    } cursor.moveToNext();
  }
  cursor.close();
  return storeTotalFat;
}

Now when this method is invoked from another activity you will have a populated ArrayList ready to do whatever you want with.

// now as to your question of which database to use

As you want this shared database, you can use the Google Cloud platform .

I prefer to use Google Firebase but that works off of NoSQL meaning you have to query using JSON.

SQLite is an internal Android's database, to which only the user has access. If you have no prior experience with it, I want to mention that if you reformat the structure of SQLite, you will have to erase the app and install it once again as the onCreate() method is called only when the app is first opened.

For your question you can do so:

The user does not have to have internet access to pass the quiz, and every quiz result you can store in the SQLite. And you can simply add a statement that checks whether the user is connected to the internet or not. If he is connected, fetch all the data from the SQLite, make a JSON object form the data you got, and use Volley or Retrofit to send this data to a webserver through the REST Api which you should make. If you have any questions please ask

Well, if you want to save LOCALLY your data, you need SharedPreferences ( https://developer.android.com/training/basics/data-storage/shared-preferences.html ). There is few data, so a database is wasted. Instead if you want an online database, you need Firebase for example. It's very good and give you other tools like the crash tool to know the crashes of your app in all devices ( https://firebase.google.com/docs/android/setup ).

You can use SharedPreferences to save the questions and Firebase to record the points and the names of all players for example. SQLite is only local ( https://stackoverflow.com/a/21599873/7339411 ), but not use it, you can do it with SharedPreferences, it's easier and faster.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM