90 likes | 219 Views
Video Games list lab 6. At the end of this lab you will be expected to know: What Views, View Groups, Layouts, and Widgets are and how they relate to each other. How to declare layouts dynamically at runtime. How to reference resources in code and from other resource layout files.
E N D
Video Games list lab 6 • At the end of this lab you will be expected to know: • What Views, View Groups, Layouts, and Widgets are and how they relate to each other. • How to declare layouts dynamically at runtime. • How to reference resources in code and from other resource layout files. • How to use Events and Event Listeners. CS440
Part 3: Game List • Modify the view: • Goto main.xml • Delete the current view • Add a text editor with ID GameText, a button with ID btnAdd and a listview with ID GameListView • Add these widgets as properties in the GameListActivity class • Find the objects: edit text, button and list view using findViewById (look at the android API for more information) • Set key listener for the edit text and click listener for the button CS440
Part 3: Game List • Create GameList property as an ArrayList of Strings • Create an Array Adapter aa property(see android API) • Create addGame(String GameTitle) method in the GameListActivityclass • Add the game title to the list • Notify the adapter that the data set has changed (see android API) • Clear the text for the Edit Text widget CS440
Part 4: Add event listeners • The GameListActivity class needs to implement two listeners: • OnClickListener, • OnKeyListener CS440
Part 4: Add event listeners • Setup the onClickListener for the "Add Game" Button. • When the user hits the "Add Game" Button a new Game object should be initialized with the text from the EditText field and added to your game list. If the EditText is empty, then the Button should do nothing. • public void onClick(View v): • Check if the button is pressed • Add game title to the list CS440
Part 4: Add event listeners • Setup the onKey listener • public booleanonKey(View v, intKeyCode, KeyEvent event) : • Check if the key is pressed (ACTION_DOWN, KEYCODE_DPAD CENTER) • Add game title to the list CS440
Add event listeners with anonymous class (a different approach) • If you are unfamiliar with anonymous inner classes that's alright. Essentially, its an inline way of creating a one-time-use class that implements some interface. You declare the class and instantiate it in one motion. You can read more about them from Sun's Java Tutorials on Anonymous Inner Classes. • Copy the following code: CS440
Part 4: Add event listeners • Fill in the onClick method in the anonymous inner class that you created to add a new game to the list. • Retrieve the text entered by the user into the EditText by calling getText().toString() (Check for empty strings here). • Clear the EditText. • Call your addGame method • Call initAddGameListenersfrom onCreate. • Run your application to ensure that the "Add Game" Button functions properly. CS440
References • Professional Android 2 Application Development (Wrox Programmer to Programmer) by Reto Meier CS440