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.


Android List Making!

November 27th, 2012!

Background

We have successfully created our first android app in the previous lab! But, what now? Well, we managed to send a message to a different screen. BUT, what if we want it in the same screen? Well, this is what we'll do today

Things that you already have

Things we'll need

Preliminary Layout Steps

  1. Create a new project, lets call it List
  2. Modify main_activity.xml and remove the default hello world text. Make orientation vertical
  3. Add an EditText tag (this will make a text field), lets id this "myEditText", and give it a HINT string (I will call mine textbox). layout_width=match_parent, layout_height=wrap_content
  4. Add a Button tag (this will make a button for us). ID it button1 and TEXTstring (I call mine addbutton).l_w=match_parent, l_h = wrap_content
  5. Add a ListView ID it myListView. l_w = fill_parent, l_h = wrap_content.

Working Steps

  1. Lets add an "android:onClick" parameter to our button. The value we give this will ALSO BE THE NAME OF THE METHOD TO IMPLEMENT IN MainActivity
  2. Lets connect it all. So, we will need private references for EditText, ArrayList, ArrayAdapter, ListView

Modifying the onCreate method

  1. We need the first 2 lines, they're important!
  2. lets get references to ListView and editText by using (CAST) findViewById(R.id.NAME); NAME is what we determined the ID of the elements to be
  3. We need a new ArrayList of strings
  4. This part is the magic, we need to make a
    new ArrayAdapter(this, android.R.layout.simple_list_item_1, NAME_OF_OUR_ARRAY_LIST
  5. make our view set the adapter to be OUR array adapter

Last STEP

If you noticed, the app does nothing right? Well, we forgot to implement our special method when someone clicks the button. Lets go do that.

  1. add the String item from the editText field to the list
  2. notify the array adapter that data changed, by using the notifyDataSetChanged() method
  3. set the text to "" again

Things not to forget

Submissions

You do not have to submit this lab. This is only for your own benefit. You can chose not to do it, or come to lab; but be reminded that you do have a project that's based on android stuff so this might be helpful.

End product files