1 / 9

Video Games list lab 6

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.

jovita
Download Presentation

Video Games list lab 6

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 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

  2. 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

  3. 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

  4. Part 4: Add event listeners • The GameListActivity class needs to implement two listeners: • OnClickListener, • OnKeyListener CS440

  5. 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

  6. 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

  7. 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

  8. 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

  9. References • Professional Android 2 Application Development (Wrox Programmer to Programmer) by Reto Meier CS440

More Related