288 lines
4.7 KiB
Plaintext
288 lines
4.7 KiB
Plaintext
|
Application
|
|||
|
Programming
|
|||
|
Hend Alkittawi
|
|||
|
|
|||
|
Android Development
|
|||
|
Building An Application That
|
|||
|
Utilizes Data
|
|||
|
|
|||
|
ROWDY QUIZ APP
|
|||
|
-
|
|||
|
|
|||
|
We created the Rowdy Quiz app, with hard-coded questions
|
|||
|
|
|||
|
-
|
|||
|
|
|||
|
Now, it is time to load the questions from a data file
|
|||
|
|
|||
|
-
|
|||
|
|
|||
|
We will also add a logo to our screen
|
|||
|
|
|||
|
WORKING WITH DATA
|
|||
|
Reader
|
|||
|
|
|||
|
Writer
|
|||
|
|
|||
|
InputStream
|
|||
|
|
|||
|
OutputStream
|
|||
|
|
|||
|
InputStreamReader
|
|||
|
|
|||
|
PrintWriter
|
|||
|
|
|||
|
FileInputStream
|
|||
|
|
|||
|
FileOutputStream
|
|||
|
|
|||
|
FileReader
|
|||
|
|
|||
|
OutputStreamWriter
|
|||
|
|
|||
|
ObjectInputStream
|
|||
|
|
|||
|
ObjectOutputStream
|
|||
|
|
|||
|
FileWriter
|
|||
|
|
|||
|
PrintStream
|
|||
|
|
|||
|
WORKING WITH DATA
|
|||
|
-
|
|||
|
|
|||
|
Classes utilized for reading and writing data in Android
|
|||
|
-
|
|||
|
|
|||
|
Activity
|
|||
|
|
|||
|
-
|
|||
|
|
|||
|
AssetManager
|
|||
|
|
|||
|
-
|
|||
|
|
|||
|
InputStream
|
|||
|
|
|||
|
-
|
|||
|
|
|||
|
OutputStream
|
|||
|
|
|||
|
READING DATA FROM A FILE
|
|||
|
-
|
|||
|
|
|||
|
To read data from a file:
|
|||
|
-
|
|||
|
|
|||
|
place the file in the Assets folder
|
|||
|
- app > New > Folder > Assets Folder (do not change the folder
|
|||
|
location)
|
|||
|
|
|||
|
READING DATA FROM A FILE
|
|||
|
-
|
|||
|
|
|||
|
Under the model package, modify QuizBank.java
|
|||
|
-
|
|||
|
|
|||
|
Modify the loadQuestions() method to create the questions from a data
|
|||
|
(*.csv) file
|
|||
|
-
|
|||
|
|
|||
|
the method uses the Activity, AssetManger and InputStream classes to read
|
|||
|
the data from the file
|
|||
|
public void loadQuestions(MainActivity activity){
|
|||
|
AssetManager manager = activity.getAssets();
|
|||
|
Scanner scan = null;
|
|||
|
String filename = "test.csv";
|
|||
|
try {
|
|||
|
InputStream file = manager.open(filename);
|
|||
|
scan = new Scanner(file);
|
|||
|
// do something with the file data
|
|||
|
}
|
|||
|
catch (IOException e) { // handle exception }
|
|||
|
}
|
|||
|
|
|||
|
READING DATA FROM A FILE
|
|||
|
-
|
|||
|
|
|||
|
For the controller, modify MainActivity.java
|
|||
|
-
|
|||
|
|
|||
|
onCreate() calls the createQuizBank() method to load the questions
|
|||
|
in the quiz bank
|
|||
|
|
|||
|
-
|
|||
|
|
|||
|
createQuizBank() calls the loadQuestions() method which requires
|
|||
|
|
|||
|
an AssetManager to load the data from the *.csv file in the
|
|||
|
Assets folder
|
|||
|
-
|
|||
|
|
|||
|
Get an AssetManager object from MainActivity using
|
|||
|
activity.getAssets()
|
|||
|
private void createQuizBank(){
|
|||
|
quizBank = new QuizBank();
|
|||
|
quizBank.loadQuestions(this);
|
|||
|
}
|
|||
|
|
|||
|
READING DATA FROM A FILE
|
|||
|
-
|
|||
|
|
|||
|
In QuizBank.java
|
|||
|
public void loadQuestions(MainActivity activity){
|
|||
|
AssetManager manager = activity.getAssets();
|
|||
|
Scanner scan = null;
|
|||
|
try {
|
|||
|
InputStream file = manager.open("test.csv");
|
|||
|
scan = new Scanner(file);
|
|||
|
// do something with the file data
|
|||
|
}
|
|||
|
catch (IOException e) {
|
|||
|
// handle exception
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
-
|
|||
|
|
|||
|
In MainActivity.java
|
|||
|
private void createQuizBank(){
|
|||
|
quizBank = new QuizBank();
|
|||
|
quizBank.loadQuestions(this);
|
|||
|
}
|
|||
|
|
|||
|
ROWDY QUIZ APP UML
|
|||
|
|
|||
|
WRITING DATA TO A FILE
|
|||
|
-
|
|||
|
|
|||
|
The Assets folder is read-only, we cannot write data to the
|
|||
|
files in the Assets folder
|
|||
|
|
|||
|
-
|
|||
|
|
|||
|
To write data to a file, create a file in the AVD memory.
|
|||
|
|
|||
|
-
|
|||
|
|
|||
|
Once created, the file can be located in the AVD memory by
|
|||
|
navigating to View > Tool Windows > Device Explorer then data
|
|||
|
> user > 0 > edu.utsa.cs3443.projectName > files
|
|||
|
|
|||
|
WRITING DATA TO A FILE
|
|||
|
-
|
|||
|
|
|||
|
Under the model package, modify QuizBank.java
|
|||
|
-
|
|||
|
|
|||
|
add the saveData() method to save data to a (*.txt) file
|
|||
|
-
|
|||
|
|
|||
|
the method uses the Activity and OutputStream classes to read the data
|
|||
|
from the file
|
|||
|
|
|||
|
public void saveData(MainActivity activity){
|
|||
|
try {
|
|||
|
OutputStream out = activity.openFileOutput(filename, Context.MODE_PRIVATE );
|
|||
|
out.write("test writing to a file".getBytes(StandardCharsets.UTF_8));
|
|||
|
out.close();
|
|||
|
} catch (IOException e) {
|
|||
|
System.out.println("Failed to write data");
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
WRITING DATA TO A FILE
|
|||
|
-
|
|||
|
|
|||
|
For the controller, modify MainActivity.java
|
|||
|
-
|
|||
|
|
|||
|
onCreate() calls the createQuizBank() method to load the questions
|
|||
|
in the quiz bank
|
|||
|
|
|||
|
-
|
|||
|
|
|||
|
saveData() method requires an Activity to access the AVD
|
|||
|
memory
|
|||
|
quizBank.loadQuestions(this);
|
|||
|
quizBank.saveData(this); // call it wherever your code needs to save data
|
|||
|
|
|||
|
READING DATA FROM A FILE
|
|||
|
-
|
|||
|
|
|||
|
In QuizBank.java
|
|||
|
public void saveData(MainActivity activity){
|
|||
|
try {
|
|||
|
OutputStream out = activity.openFileOutput(filename, Context.MODE_PRIVATE );
|
|||
|
out.write("test writing to a file".getBytes(StandardCharsets.UTF_8));
|
|||
|
out.close();
|
|||
|
} catch (IOException e) {
|
|||
|
System.out.println("Failed to write data");
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
-
|
|||
|
|
|||
|
In MainActivity.java
|
|||
|
quizBank.saveData(this);
|
|||
|
|
|||
|
WORKING WITH DATA
|
|||
|
-
|
|||
|
|
|||
|
What if we would like to update data in a file from the Assets
|
|||
|
folder?
|
|||
|
-
|
|||
|
|
|||
|
The Assets folder is read-only, we cannot write data to the
|
|||
|
files in the Assets folder
|
|||
|
|
|||
|
-
|
|||
|
|
|||
|
A work around this is to create a copy of the file from the
|
|||
|
Assets to the AVD memory, then use the “copy file” for reading
|
|||
|
and writing data.
|
|||
|
|
|||
|
ADDING IMAGEVIEWS TO THE LAYOUT
|
|||
|
-
|
|||
|
|
|||
|
Use an ImageView in the layout file to display an
|
|||
|
image on the screen
|
|||
|
|
|||
|
-
|
|||
|
|
|||
|
ImageViews can display images from the drawable
|
|||
|
folder
|
|||
|
-
|
|||
|
|
|||
|
-
|
|||
|
|
|||
|
Place the image file under res > drawable
|
|||
|
|
|||
|
The ImageView’s src attribute specifies the image
|
|||
|
that the ImageView will display
|
|||
|
<ImageView
|
|||
|
android:layout_width ="wrap_content"
|
|||
|
android:layout_height ="wrap_content"
|
|||
|
android:src="@drawable/rowdylogo"/>
|
|||
|
|
|||
|
CODE DEMO
|
|||
|
-
|
|||
|
|
|||
|
Walk through the
|
|||
|
Android App in Android
|
|||
|
Studio
|
|||
|
|
|||
|
DO YOU HAVE ANY
|
|||
|
QUESTIONS?
|
|||
|
|
|||
|
THANK
|
|||
|
YOU!
|
|||
|
|
|||
|
@
|
|||
|
|
|||
|
hend.alkittawi@utsa.edu
|
|||
|
|
|||
|
By Appointment
|
|||
|
Online
|
|||
|
|
|||
|
|