CS251L - Fall 2012

Intermediate Programming

Section 002

Jan Monterrubio

Email: janmonterrubio@gmail.com

Office: FEC 3rd Floor Lounge or (FEC 309)

Office Hours:

Wednesday 3 - 5 PM

Friday 2 - 4 PM

Full TA and Tutor Schedule

If these times don't work email me and I'll make sure you get the help you need.

List Editor!

December 4th, 2012!


For this demo, I will be using code from the previous link with a simple file reader. At the end of this demo, you will have a similar product (hopefully), that can do the following:

This one is longer, hopefully we had time to go through it in lab! Code will be posted sometime before Midnight today, but hopefully you're trying this without copy pasting in order to learn something.



First Layout

RelativeLayout lets you organize things the way you want them. It is quite powerful and you can organize elements in relation to their relatives (pun intended). Therefore, this time we are going to stick with RelativeLayout since it offers more GUI creativity freedom. Our activity_menu will have these components:


  1. Start by having private references to the following:
    • ListView listView
    • ArrayList<String> items
    • ArrayAdapter<String> adapter
    • EditText
  2. get the reference to list view
  3. instantiate items
  4. load data from assets. SEE PREVIOUS LAB IF YOU DON'T KNOW HOW TO DO THIS


here we want to cal listView.setOnItemClickListener. Give it a new View.onItemClickListener(). We will implement this as an anonymous class. The constructor has 4 arguments. We are interested in the third which represents the position of the item that was clicked. We are going to get a reference to the object using getItemAtPosition(index) and then call the toString method on it.


Since we declared it with the onClick method, we have to implement this method like we have been doing all this time. We'll get the text here, add it to index 0, set thte text field to "" and notify data set change. We'll also create something called a TOAST! which notifies the user of something.

Context context = getApplicationContext()
CharSequence msg = "Item Added: " + text
int duration = Toast.LENGTH_SHORT
Toast toast = Toast.makeText(context, msg, duration)



We need a new activity, we shall call it ItemActivity. This one will receive some data and will eventually get access to some methods in MenuActivity

Layout in activity_item

Code Madness in Item Activity

  • Listeners code Assume these static methods exist!

    Start by making ITEMS and ADAPTER static. Then, lets modify setClickListener.

    Make a new intent.
    Intent intent = new Intent(MenuActivity.this, ItemActivity.class)
    Create the data pairs.
    intent.putExtra("data_text", text)
    intent.putExtra("data_index", position)
    Start the thing!

    NOW lets implement the STATIC methods, edit and delete


    Give yourself a pat on the back. You did it. We're done. Lets run it and mess around!


    Once again, no submissions, only for your benefit

    End Product