Difference between revisions of "MAP524/DPS924 Lecture 5"
(Created page with '= Adapters = We'll finish the '''Spinner''' example from last week's lecture. Unlike the other Views we looked at last week it requires the use of an Adapter. See the [http://de…') |
(→ListView) |
||
(7 intermediate revisions by the same user not shown) | |||
Line 1: | Line 1: | ||
− | = Adapters = | + | = More complex views = |
+ | == Adapters == | ||
We'll finish the '''Spinner''' example from last week's lecture. Unlike the other Views we looked at last week it requires the use of an Adapter. See the [http://developer.android.com/guide/topics/ui/controls/spinner.html official documentation here]. Note that in the setup you have: | We'll finish the '''Spinner''' example from last week's lecture. Unlike the other Views we looked at last week it requires the use of an Adapter. See the [http://developer.android.com/guide/topics/ui/controls/spinner.html official documentation here]. Note that in the setup you have: | ||
Line 7: | Line 8: | ||
* An adapter to connect the first two (specifically here an ArrayAdapter) | * An adapter to connect the first two (specifically here an ArrayAdapter) | ||
− | + | Here's a [https://github.com/codepath/android_guides/wiki/Using-an-ArrayAdapter-with-ListView nice tutorial on Adapters] generally with a ListView as a specific example. | |
− | This | + | == ListView == |
+ | |||
+ | This is very similar to a spinner, it does essentially the same thing, but it's a bit more complicated to build. Follow these steps: | ||
* Create a new Android app and name it My List. | * Create a new Android app and name it My List. | ||
* Add a ListView below the TextView in your activity_main.xml file. | * Add a ListView below the TextView in your activity_main.xml file. | ||
* Create a second layout file named "activity_listview.xml" with root element set to TextView (no layout element). It should look like this | * Create a second layout file named "activity_listview.xml" with root element set to TextView (no layout element). It should look like this | ||
− | < | + | <source lang="xml"><!?xml version="1.0" encoding="utf-8"?> |
<textview xmlns:android="http://schemas.android.com/apk/res/android" | <textview xmlns:android="http://schemas.android.com/apk/res/android" | ||
android:layout_width="match_parent" | android:layout_width="match_parent" | ||
− | android:layout_height="match_parent"/></ | + | android:layout_height="match_parent"/></source> |
* Create a new [https://scs.senecac.on.ca/~andrew.smith/android/2015-02/arrays.xml arrays.xml] file inside your values directory. | * Create a new [https://scs.senecac.on.ca/~andrew.smith/android/2015-02/arrays.xml arrays.xml] file inside your values directory. | ||
* Add a String array named colourNames as a class variable in MainActivity like this: | * Add a String array named colourNames as a class variable in MainActivity like this: | ||
− | < | + | <source lang="java">String[] colourNames;</source> |
* Tie your array to your xml array like this : | * Tie your array to your xml array like this : | ||
− | < | + | <source lang="java">colourNames = getResources().getStringArray(R.array.listArray);</source> |
* Create a ListView object pointing to your list view like this: | * Create a ListView object pointing to your list view like this: | ||
− | < | + | <source lang="java">ListView lv = (ListView) findViewById(R.id.listView);</source> |
* Create an array adapter and attach it to your list view like this: | * Create an array adapter and attach it to your list view like this: | ||
− | < | + | <source lang="java">ArrayAdapter aa = new ArrayAdapter(this, R.layout.activity_listview, colourNames); |
− | lv.setAdapter(aa);</ | + | lv.setAdapter(aa);</source> |
* You can now run your app to see your scrollable list of colours. | * You can now run your app to see your scrollable list of colours. | ||
− | * Now add an onClickListener to your | + | * Now add an onClickListener to your list view like this: |
− | < | + | <source lang="java">lv.setOnItemClickListener(new AdapterView.OnItemClickListener() { |
− | + | public void onItemClick(AdapterView parent, View view, int position, long id) { | |
− | + | Toast.makeText(getApplicationContext(), ((TextView) view).getText(), Toast.LENGTH_SHORT).show(); | |
− | + | } | |
− | + | });</source> | |
* Run your app again and this time select a colour by clicking it. | * Run your app again and this time select a colour by clicking it. | ||
* You should see a Toast with the selected colour name pop up. | * You should see a Toast with the selected colour name pop up. | ||
+ | |||
+ | == Context Menu == | ||
+ | |||
+ | Add a Context Menu to the Scrollable List in the ListView example above. [http://developer.android.com/reference/android/view/ContextMenu.html Official documentation here]. | ||
+ | |||
+ | * First let's register the list view for a Context Menu by adding this line to our activity : | ||
+ | <source lang="java">registerForContextMenu(lv);</source> | ||
+ | * Now add this method to create our context menu: | ||
+ | <source lang="java">@Override | ||
+ | public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) { | ||
+ | super.onCreateContextMenu(menu, v, menuInfo); | ||
+ | menu.setHeaderTitle("Select The Action"); | ||
+ | menu.add(0, v.getId(), 0, "Make a Phone Call"); | ||
+ | menu.add(0, v.getId(), 0, "Send an SMS Message"); | ||
+ | }</source> | ||
+ | * Now add the listener method like this: | ||
+ | <source lang="java">@Override | ||
+ | public boolean onContextItemSelected(MenuItem item) { | ||
+ | if (item.getTitle() == "Make a Phone Call") { | ||
+ | Toast.makeText(getApplicationContext(), "making a call", Toast.LENGTH_LONG).show(); | ||
+ | } else if (item.getTitle() == "Send an SMS Message") { | ||
+ | Toast.makeText(getApplicationContext(), "sending a message", Toast.LENGTH_LONG).show(); | ||
+ | } else { | ||
+ | return false; | ||
+ | } | ||
+ | return true; | ||
+ | }</source> | ||
+ | * You can now run your app and test the context menu. | ||
+ | |||
+ | = Threads = | ||
+ | |||
+ | You shouldn't (and Android often enforces it) run long operations on the UI thread. That would result in a blocked user interface which is annoying on the desktop but extremely annoying on a slow mobile device. See the [http://developer.android.com/guide/components/processes-and-threads.html official documentation] for an overview. | ||
+ | |||
+ | We'll look at one means of doing hard processing work in the background without affecting the user interface: AsyncTask. | ||
+ | |||
+ | == AsyncTask == | ||
+ | |||
+ | Basically just a thread but with a built-in function to update the UI after it's finished. To use it you'll need to: | ||
+ | |||
+ | * Extend AsyncTask to make your own class, and decide on the types of parameters and return value. | ||
+ | * Implement doInBackground() to do all the work, and parse the parameters first. | ||
+ | * Implement onPostExecute() to update the UI after doInBackground() completes. |
Latest revision as of 06:47, 20 July 2015
More complex views
Adapters
We'll finish the Spinner example from last week's lecture. Unlike the other Views we looked at last week it requires the use of an Adapter. See the official documentation here. Note that in the setup you have:
- A store of data (an array of values)
- A UI element to display those values (the spinner)
- An adapter to connect the first two (specifically here an ArrayAdapter)
Here's a nice tutorial on Adapters generally with a ListView as a specific example.
ListView
This is very similar to a spinner, it does essentially the same thing, but it's a bit more complicated to build. Follow these steps:
- Create a new Android app and name it My List.
- Add a ListView below the TextView in your activity_main.xml file.
- Create a second layout file named "activity_listview.xml" with root element set to TextView (no layout element). It should look like this
<!?xml version="1.0" encoding="utf-8"?>
<textview xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
- Create a new arrays.xml file inside your values directory.
- Add a String array named colourNames as a class variable in MainActivity like this:
String[] colourNames;
- Tie your array to your xml array like this :
colourNames = getResources().getStringArray(R.array.listArray);
- Create a ListView object pointing to your list view like this:
ListView lv = (ListView) findViewById(R.id.listView);
- Create an array adapter and attach it to your list view like this:
ArrayAdapter aa = new ArrayAdapter(this, R.layout.activity_listview, colourNames);
lv.setAdapter(aa);
- You can now run your app to see your scrollable list of colours.
- Now add an onClickListener to your list view like this:
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView parent, View view, int position, long id) {
Toast.makeText(getApplicationContext(), ((TextView) view).getText(), Toast.LENGTH_SHORT).show();
}
});
- Run your app again and this time select a colour by clicking it.
- You should see a Toast with the selected colour name pop up.
Context Menu
Add a Context Menu to the Scrollable List in the ListView example above. Official documentation here.
- First let's register the list view for a Context Menu by adding this line to our activity :
registerForContextMenu(lv);
- Now add this method to create our context menu:
@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
menu.setHeaderTitle("Select The Action");
menu.add(0, v.getId(), 0, "Make a Phone Call");
menu.add(0, v.getId(), 0, "Send an SMS Message");
}
- Now add the listener method like this:
@Override
public boolean onContextItemSelected(MenuItem item) {
if (item.getTitle() == "Make a Phone Call") {
Toast.makeText(getApplicationContext(), "making a call", Toast.LENGTH_LONG).show();
} else if (item.getTitle() == "Send an SMS Message") {
Toast.makeText(getApplicationContext(), "sending a message", Toast.LENGTH_LONG).show();
} else {
return false;
}
return true;
}
- You can now run your app and test the context menu.
Threads
You shouldn't (and Android often enforces it) run long operations on the UI thread. That would result in a blocked user interface which is annoying on the desktop but extremely annoying on a slow mobile device. See the official documentation for an overview.
We'll look at one means of doing hard processing work in the background without affecting the user interface: AsyncTask.
AsyncTask
Basically just a thread but with a built-in function to update the UI after it's finished. To use it you'll need to:
- Extend AsyncTask to make your own class, and decide on the types of parameters and return value.
- Implement doInBackground() to do all the work, and parse the parameters first.
- Implement onPostExecute() to update the UI after doInBackground() completes.