List Editor!
December 4th, 2012!
Background
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:
- Read from a file in assets
- Click on an item in a list view
- Send data to another Activity
- Have a different activity with some control over this data
- Return back to the first activity and see the effects
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.
PART 1
Setup
- NEW PROJECT, lets call it MyListEditor
- Instead of MainActivity, lets call it MenuActivity
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:
- EditText. id=edit_text. layout_width = fill_parent. layout_height = wrap_content. alignBottom = @+id/add_button. layout_alignParentLeft= true . layout_toLeftOf = @id/add_button. hint = @string/edit_text_hint.
- Button. id = @id/add_button (SINCE WE ALREADY DECLARED IT). layout_width && layout_height = wrap_content. layout_alignParentRight= true. layout_alignParentTop = true. text = @string/add_button_text. onClick = addData .
- ListView. id= @+id/list_view . layout_width && layout_height = fill_parent. layout_below = @id/add_button
- Strings to add to strings.xml. edit_text_hint = Insert Item. add_button_text = Add Item.
MainActivity
- Start by having private references to the following:
- ListView listView
- ArrayList<String> items
- ArrayAdapter<String> adapter
- EditText
- get the reference to list view
- instantiate items
- load data from assets. SEE PREVIOUS LAB IF YOU DON'T KNOW HOW TO DO THIS
setClickListener
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.
addDataMethod
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) toast.show()
PART 2
Setup
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
- An edit text with a button next to it.
- A button below both things to delete
- A button to go back to the MenuActivity
- Text file with layout for easy looking
- FIX OUR STRING RESOURCES. easy access
Code Madness in Item Activity
- Private references to EdiText, All 3 buttons, String data, int index
- Get references to edit text and buttons using view id
- GET THE DATA with MAGIC:
Build extras = getIntent().getExtras() if (extras NOT null) data_text = extras.getString("data_text") data_index = extras.getInt("data_index") - Set edit item to the text given by data_text
- call the three listener methods to instantiate, edit, delete, back
CONNECTING IT ALL
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!
startActivity(intent)
NOW lets implement the STATIC methods, edit and delete
Part4
Give yourself a pat on the back. You did it. We're done. Lets run it and mess around!