Open main menu

CDOT Wiki β

Changes

MAP524/DPS924 Lecture 5

516 bytes added, 07:47, 20 July 2015
ListView
== ListView ==
This should be as simple as is very similar to a spinner, it appears to do 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
<presource lang="xml"><!?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"/></presource>
* 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:
<presource lang="java">String[] colourNames;</presource>
* Tie your array to your xml array like this :
<presource lang="java">colourNames = getResources().getStringArray(R.array.listArray);</presource>
* Create a ListView object pointing to your list view like this:
<presource lang="java">ListView lv = (ListView) findViewById(R.id.listView);</presource>
* Create an array adapter and attach it to your list view like this:
<presource lang="java">ArrayAdapter aa = new ArrayAdapter(this, R.layout.activity_listview, colourNames);lv.setAdapter(aa);</presource>
* You can now run your app to see your scrollable list of colours.
* Now add an onClickListener to your list view like this:
<presource 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(); } });</presource>
* 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.
* First let's register the list view for a Context Menu by adding this line to our activity :
<presource lang="java">registerForContextMenu(lv);</presource>
* Now add this method to create our context menu:
<presource lang="java">@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
menu.add(0, v.getId(), 0, "Make a Phone Call");
menu.add(0, v.getId(), 0, "Send an SMS Message");
}</presource>
* Now add the listener method like this:
<presource lang="java">@Override
public boolean onContextItemSelected(MenuItem item) {
if (item.getTitle() == "Make a Phone Call") {
}
return true;
}</presource>
* You can now run your app and test the context menu.
== 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.