The first midterm exam will be held in class on Wednesday, Oct. 7. The exam will cover the material I have posted on the course web site in the "Introduction to Kotlin" and "Android View Examples" sections.
The exam will mostly focus on key concepts in Android View applications, including
onCreate() methods.findViewById<>(), reading strings from TextFields, and setting the text in TextViews.finish() to return from an activity.I will ask two types of questions. In the first type of question I will show you a chunk of code and ask you to explain what the code is doing. In the second type of question I will ask you to explain how you would accomplish a particular task: for this type of question you can answer with a mix of written explanation and a few relevant snippets of code.
Here are some sample questions:
1. What does this code do?
interface QuizApi {
@GET("questions")
suspend fun getQuestions(@Query("quiz") quizId: Int): Response<Array<Question>>
@POST("answers")
suspend fun postAnswer(@Body answer: Answer): Response<Void>
@GET("quizzes")
suspend fun getQuizzes(): Response<Array<Quiz>>
@GET("students")
suspend fun getStudents(): Response<Array<Student>>
}
2. What does this code do?
companion object {
const val NAME: String = "edu.lawrence.SecondAndroid.NAME"
}
fun doGreeting(view: View) {
val nameField = findViewById<EditText?>(R.id.name)
val name = nameField?.getText().toString()
val intent: Intent = Intent(this, SecondActivity::class.java)
intent.putExtra(NAME, name)
startActivity(intent)
}
What role does the companion object play here?
3. Putting data in an Intent is one way to pass information from one activity to another. An alternative is to use a ViewModel. How would you restructure the example in number two to use a ViewModel to pass the person's name from the first activity to the second?