Search code examples
javaandroidandroid-sqlite

Can i replace my table-arranged dummy values with actual data froma database?


[So as you can see in the picture, i'm a newbie in android programming and i just had an assignment concerning creating a management system app for university. The views you see in the picture are just customly made with xml(Table layout) and the data are just dummy data placed on the respective textviews in the tables. I desire to create a database for students and all those data e.g names, gender, should be fetched from the database or sent. Remember, as for courses the number of courses would differ from one degree prog to another(imagine a student with less ormore courses than the tables i sampled there). How do i acomplish this

As for database, im thinking of using SQLite or Firebase ]1


Solution

  • There are many ways to approach this, depending on the requirements of your program. If you need to share data between multiple people or devices, you probably want a database that lives on a web server, and you would write an API (application programming interface) which you would call from your app, and it would go and get the data from the database and return it to your app.

    If your app is meant to be 'stand alone', where you could use it without being connected to the internet, then yes, a SQLite database is a good solution and it is pretty easy to integrate into your app.

    To create a SQLite database, you can download the free tool SQLiteStudio. Once you create the database (usually saved with a .db file extension) you can add it to your project in the Assets folder (create it if it doesn't exist). The folder lives at the same level as the bin and gen folders in your project.

    Next, search for a class called DatabaseHelper.java, it has a number of methods for opening, closing and querying a SQLite database, copy that to your project.

    At the start of your program, you should check to see if you have a copy of the database in your local data/databases folder in your App's file storage area. If you don't have a copy there, which on the first execution of your program, it will not be there, then you have to copy the database .db file from your Assets folder to the data/databases folder, then open it there. If it is found, you've already done this, so, just open it.

    When you want to read data into your app, you execute a SELECT statement to retrieve data from your database into a Cursor object, and return that cursor from your DatabaseHelper class to your activity. In your activity, you will iterate through the Cursor, reading one row at a time, and copy the data into program variables. Sometimes, you will create a class object to hold one record, with attributes that match a row from your query, and you'll create an instance of your class, fill it with the data from the Cursor, then add that object to an Array List of objects which your program can use at a later time, say, to display a list of people or whatever it is you queried for. Once the Cursor has been read to the end, you close the Database by calling a close method in the DatabaseHelper class.

    That's the general idea. If you search for a copy of the DatabaseHelper.java class, that will get you started. Then, search for some example projects that use that DatabaseHelper.java class so you can figure out how to use it.

    Good luck!!