Upload
others
View
9
Download
0
Embed Size (px)
Citation preview
Mobile ProgrammingLecture 3
Resources, Selection, Activities, Intents
Lecture 2 Review
● What widget would you use to allow the user to enter○ a yes/no value○ a range of values from 1 to 100
● What's the benefit of a TableLayout over LinearLayout?
● How many ways can you set an event listener?
Lecture 2 Review
● How do you make the android:inputType attribute of an EditText both textCapWords and textMultiLine?
● Why should you use a @string resource for TextViews instead of hardcoding the string?
● If you use the same android:onClick value for multiple views, how do you determine which one was clicked?
AgendaReferencing @string resources● Setters and Getters● Selection Views● Android Components - Activity● Intents● More on the Android Manifest File
String Resources
● Three types of string resources○ String○ String Array○ Quantity Strings
String Resources
● Three types of string resources○ String○ String Array○ Quantity Strings
String Resources - String
Used to store a single string resource
<string name=”sample_string”>Sample String</string>
Referenced using ● @string/<string> - in XML● Resources res = getResources();
res.getString() / getText()//getText() retains any text styling
String Resources
● Three types of string resources○ String○ String Array○ Quantity Strings
String Resources - String Array
Used to provide an array of strings
<string-array name=”array_name”>
<item>Item 1</item>
<item>Item 2</item>
<item>Item 3</item>
</string-array>
Referenced using:● @array/array_name in XML● Resources res = getResources();
res.getStringArray();
String Resources
● Three types of string resources○ String○ String Array○ Quantity Strings
String Resources - Quantity Strings
Used to provide different string outputs for plurals
<plurals name=”plural_name”>
<item quantity=”one”>%d student has an A</item>
<item quantity=”other”>%d students have an A</item>
</plurals>
Referenced using:● Resource res = getResources();
res.getQuantityString(R.plurals.plural_name,
count, count);
Referencing @string Resources
● All @string Resources are stored in res/values/strings.xml
● You reference them in XML as "@string/string_name"
Referencing @string Resources
You can also reference them in Java
String buttonText = "";
Resources res = getResources();
buttonText = res.getString(R.string.string_name);
Referencing Resources
You can store an array of strings if you need a collection instead of just a single string. This makes it an @array resource instead of @string resource!● Open res/values/strings.xml and create the new
entry● You can also get some assistance by
○ Right-click the body of the strings.xml file○ Select Generate… > XML Tag…○ Select the appropriate resource type (string-array)
● Provide your resource with a name, e.g. "countries"● Add an <item> entry for each item in the array
Setters and Getters for Views
Most of a View's attributes can be “set” and “get” programmatically
Example widget XML attribute set method get method
EditText android:text setText(String) getText()
Button android:onClicksetOnClickListener(OnClickListener)
-
CheckBox android:checked setChecked(Boolean) isChecked()
SeekBar android:progress setProgress(int) getProgress()
SeekBar android:max setMax(int) getMax()
Selection Widgets - Spinner
Similar to a drop-down list● android:spinnerMode
○ "dialog" or "dropdown"● android:prompt
○ e.g. "Select an option"○ message to display when the spinner dialog is shown
■ only when android:spinnerMode="dialog"● android:entries
○ e.g. "@array/countries"○ populate the spinner using this array
Selection Widgets - Spinner
Similar to a drop-down list● android:prompt
○ e.g. "Select an option"○ message to display when the spinner dialog is shown
● android:spinnerMode○ "dialog" or "dropdown"
● android:entries○ e.g. "@array/countries"○ populate the spinner using this array
If the list is static, you can again avoid Java by specifying the entries in XML!
Selection Widgets - Spinner
Similar to a drop-down list● android:prompt
○ e.g. "Select an option"○ message to display when the spinner dialog is shown
● android:spinnerMode○ "dialog" or "dropdown"
● android:entries○ e.g. "@array/countries"○ populate the spinner using this array
If you don't like XML, you can set the entries in Java also.
Selection Widgets - Spinnerpublic void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Spinner mySpinner = (Spinner)
findViewById(R.id.my_spinner);
ArrayAdapter<CharSequence> adapter =
ArrayAdapter.createFromResource(this, R.array.countries, android.R.layout.simple_spinner_item);
mySpinner.setAdapter(adapter);
}
Selection Widgets - Spinnerpublic void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Spinner mySpinner = (Spinner)
findViewById(R.id.my_spinner);
ArrayAdapter<CharSequence> adapter =
ArrayAdapter.createFromResource(this, R.array.countries, android.R.layout.simple_spinner_item);
mySpinner.setAdapter(adapter);
}
This is the Java code. If you choose to do this, then you don't need to set the android:entries attribute
Selection Widgets - Spinnerpublic void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Spinner mySpinner = (Spinner)
findViewById(R.id.my_spinner);
ArrayAdapter<CharSequence> adapter =
ArrayAdapter.createFromResource(this, R.array.countries, android.R.layout.simple_spinner_item);
mySpinner.setAdapter(adapter);
}
You should already know what this does.
Selection Widgets - Spinnerpublic void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Spinner spinner = (Spinner)
findViewById(R.id.my_spinner);
ArrayAdapter<CharSequence> adapter =
ArrayAdapter.createFromResource(this, R.array.countries, android.R.layout.simple_spinner_item);
mySpinner.setAdapter(adapter);
}
Since you don't like XML, to do it programmatically, you need to use an Adapter.
Selection Widgets - Spinnerpublic void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Spinner mySpinner = (Spinner)
findViewById(R.id.my_spinner);
ArrayAdapter<CharSequence> adapter =
ArrayAdapter.createFromResource(this, R.array.countries, android.R.layout.simple_spinner_item);
mySpinner.setAdapter(adapter);
}
This is Java stuff. If you don't know what it is, try removing it to see what happens.
Selection Widgets - Spinnerpublic void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Spinner mySpinner = (Spinner)
findViewById(R.id.my_spinner);
ArrayAdapter<CharSequence> adapter =
ArrayAdapter.createFromResource(this, R.array.countries, android.R.layout.simple_spinner_item);
mySpinner.setAdapter(adapter);
}
If you have an @array resource already, then create an ArrayAdapter from that resource.
Selection Widgets - Spinnerpublic void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Spinner mySpinner = (Spinner)
findViewById(R.id.my_spinner);
ArrayAdapter<CharSequence> adapter =
ArrayAdapter.createFromResource(this, R.array.countries, android.R.layout.simple_spinner_item);
mySpinner.setAdapter(adapter);
}
First argument is of type Context, you can just pass the "this" keyword here (because of the state of app)
Selection Widgets - Spinnerpublic void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Spinner mySpinner = (Spinner)
findViewById(R.id.my_spinner);
ArrayAdapter<CharSequence> adapter =
ArrayAdapter.createFromResource(this, R.array.countries, android.R.layout.simple_spinner_item);
mySpinner.setAdapter(adapter);
}
This is the resource that you're creating the ArrayAdapter from.
Selection Widgets - Spinnerpublic void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Spinner mySpinner = (Spinner)
findViewById(R.id.my_spinner);
ArrayAdapter<CharSequence> adapter =
ArrayAdapter.createFromResource(this, R.array.countries, android.R.layout.simple_spinner_item);
mySpinner.setAdapter(adapter);
}
3rd argument expects the @id of a TextView.
Selection Widgets - Spinnerpublic void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Spinner mySpinner = (Spinner)
findViewById(R.id.my_spinner);
ArrayAdapter<CharSequence> adapter =
ArrayAdapter.createFromResource(this, R.array.countries, android.R.layout.simple_spinner_item);
mySpinner.setAdapter(adapter);
}
android.R gives you resources that exist for use even before you create your app
Selection Widgets - Spinnerpublic void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Spinner mySpinner = (Spinner)
findViewById(R.id.my_spinner);
ArrayAdapter<CharSequence> adapter =
ArrayAdapter.createFromResource(this, R.array.countries, android.R.layout.simple_spinner_item);
mySpinner.setAdapter(adapter);
}
So instead of creating and using your own TextView for this purpose, use this one
Selection Widgets - Spinnerpublic void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Spinner mySpinner = (Spinner)
findViewById(R.id.my_spinner);
ArrayAdapter<CharSequence> adapter =
ArrayAdapter.createFromResource(this, R.array.countries, android.R.layout.simple_spinner_item);
mySpinner.setAdapter(adapter);
}
Attach the Adapter to the Spinner in order to populate the Spinner!
Selection Widgets - Spinnerpublic void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Spinner mySpinner = (Spinner) findViewById(R.id.my_spinner);
ArrayAdapter<CharSequence> adapter =
ArrayAdapter.createFromResource(this, R.array.countries, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource
(android.R.layout.simple_spinner_dropdown_item);
mySpinner.setAdapter(adapter);
}
Finally, this line of code is not required, but it makes your drop down look pretty.
Selection Widgets - Spinner
● Both ways give you a Spinner which uses predetermined entries
● How do you populate the Spinner at runtime, when the entries are also determined at runtime?
Selection Widgets - Spinnerpublic void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Spinner mySpinner = (Spinner) findViewById(R.id.my_spinner);
String[] countries = new String[]{ "Brazil", "China",
"Denmark" };
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, countries);
mySpinner.setAdapter(adapter);
}
Selection Widgets - Spinnerpublic void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Spinner spinner = (Spinner) findViewById(R.id.my_spinner);
String[] countries = new String[] { "Brazil", "China",
"Denmark" };
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, countries);
mySpinner.setAdapter(adapter);
}
Create and initialize a String array
Selection Widgets - Spinnerpublic void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Spinner mySpinner = (Spinner) findViewById(R.id.my_spinner);
String[] countries = new String[] { "Brazil", "China",
"Denmark"};
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, countries);
mySpinner.setAdapter(adapter);
}
Create an ArrayAdapter to populate the Spinner with
Selection Widgets - Spinnerpublic void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Spinner mySpinner = (Spinner) findViewById(R.id.my_spinner);
String[] countries = new String[] { "Brazil", "China",
"Denmark" };
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, countries);
mySpinner.setAdapter(adapter);
}
3rd argument accepts a List of Strings
Selection Widgets - ListView
A ListView is similar to a Spinner● They both show the user a list of items● They both have the android:entries
attribute, so they can both be set using the same @array resource or the same ArrayAdapter
Selection Widgets - ListView
A ListView is similar to a Spinner with some exceptions, to name a few:
● A Spinner is normally used for forms, a ListView not so much
● A Spinner only shows one item at a time● A Spinner item can be "selected", a ListView item
can be "clicked" or "selected"
Selection Widgets - ListView
A ListView is similar to a Spinner with some exceptions, to name a few:
● A Spinner is normally used for forms, a ListView not so much
● A Spinner only shows one item at a time● A Spinner item can be "selected", a ListView item
can be "clicked" or "selected"
This was added in hindsight. On non-touch-screen devices it can be selected
Events - Spinner OnItemSelected
You can set an EventListener for when the user selects an item from the Spinner
Events - Spinner OnItemSelectedmySpinner.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view,
int position, long id) {
Toast.makeText(getApplicationContext(),
((TextView) view).getText() ,
Toast.LENGTH_SHORT).show();
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
Toast.makeText(getApplicationContext(),
"You didn't select anything",
Toast.LENGTH_SHORT).show();
}
});
Events - Spinner OnItemSelectedmySpinner.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view,
int position, long id) {
Toast.makeText(getApplicationContext(),
((TextView) view).getText() ,
Toast.LENGTH_SHORT).show();
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
Toast.makeText(getApplicationContext(),
"You didn't select anything" ,
Toast.LENGTH_SHORT).show();
}
});
Setter method for setting the EventListener
Events - Spinner OnItemSelectedmySpinner.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view,
int position, long id) {
Toast.makeText(getApplicationContext(),
((TextView) view).getText() ,
Toast.LENGTH_SHORT).show();
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
Toast.makeText(getApplicationContext(),
"You didn't select anything" ,
Toast.LENGTH_SHORT).show();
}
});
Anonymous class
Events - Spinner OnItemSelectedmySpinner.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view,
int position, long id) {
Toast.makeText(getApplicationContext(),
((TextView) view).getText() ,
Toast.LENGTH_SHORT).show();
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
Toast.makeText(getApplicationContext(),
"You didn't select anything" ,
Toast.LENGTH_SHORT).show();
}
});
When an item is selected ...,
Events - Spinner OnItemSelectedmySpinner.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view,
int position, long id) {
Toast.makeText(getApplicationContext(),
((TextView) view).getText() ,
Toast.LENGTH_SHORT).show();
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
Toast.makeText(getApplicationContext(),
"You didn't select anything" ,
Toast.LENGTH_SHORT).show();
}
});
You can now reference the actual View (i.e., each entry in Spinner is a TextView)
Events - Spinner OnItemSelectedmySpinner.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view,
int position, long id) {
Toast.makeText(getApplicationContext(),
((TextView) view).getText() ,
Toast.LENGTH_SHORT).show();
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
Toast.makeText(getApplicationContext(),
"You didn't select anything" ,
Toast.LENGTH_SHORT).show();
}
});
The position of the view in the Adapter
Events - Spinner OnItemSelectedmySpinner.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view,
int position, long id) {
Toast.makeText(getApplicationContext(),
((TextView) view).getText() ,
Toast.LENGTH_SHORT).show();
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
Toast.makeText(getApplicationContext(),
"You didn't select anything" ,
Toast.LENGTH_SHORT).show();
}
});
The android:id attribute of the view
Events - Spinner OnItemSelectedmySpinner.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view,
int position, long id) {
Toast.makeText(getApplicationContext(),
((TextView) view).getText() ,
Toast.LENGTH_SHORT).show();
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
Toast.makeText(getApplicationContext(),
"You didn't select anything" ,
Toast.LENGTH_SHORT).show();
}
});
Cast the generic View to a TextView
Events - Spinner OnItemSelectedmySpinner.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view,
int position, long id) {
Toast.makeText(getApplicationContext(),
((TextView) view).getText() ,
Toast.LENGTH_SHORT).show();
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
Toast.makeText(getApplicationContext(),
"You didn't select anything" ,
Toast.LENGTH_SHORT).show();
}
});
Get the android:text value
Events - Spinner OnItemSelected
● Note that we're using OnItemSelectedListener instead of OnItemClickListener for Spinner!
● If you try to use OnItemClickListener you may get errors
● Also, note the difference between these two listeners and OnClickListener
○ OnItemClickListener■ an entry in the Spinner has been clicked, don't use this
○ OnClickListener■ the Spinner has been clicked
○ OnItemSelectedListener■ an entry in the Spinner has been selected
Events - ListView OnItemClick
● As mentioned before, a ListView item can be clicked, or selected
● In that case, you use OnItemClickListener or OnItemSelectedListener
Events - ListView OnItemClickmyListView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Toast.makeText(getApplicationContext(),
((TextView) view).getText(),
Toast.LENGTH_SHORT).show();
}
});
Android Application Components
So far we've only been working with 1 of the 4 Android components, namely Activity
Android Application Components
The 4 Android application components are 1. Activity
2. Broadcast Receiver
3. Content Provider
4. Service
Android Application Components
1. Activity
2. Broadcast Receiver
3. Content Provider
4. Service
Activity
● So far we've only been working with a single Activity
● Remember, an Activity is a single screen with a user interface
● Each Activity in an application is independent
Activity
An Activity is the only Android application component with a UI!
Activity
● In Homework 2, you're creating a form that simply displays the data after the form is submitted
● In order to take the user to a different screen after the form is submitted, you should add a new Activity to your app
Activity
To add a new Activity● Right-click your project and choose New > Activity● Select an Activity template, provide a name for the Layout
Resource file and provide any other necessary information● Click FinishOr … ● Create a new Java class that extends the Activity base
class● Open the AndroidManifest.xml file● Add a new <activity> tag and configure the appropriate
attributes● Save the AndroidManifest.xml file
● You can design the UI for your second Activity as well○ e.g. add another res/layout/xml_file.xml
● Now we need to figure out how to launch the second Activity B from the first Activity A
Activity
Intent
● An Intent is an abstract description of an operation to be performed
● You can use an Intent to launch another Activity B from within Activity A
Activity and Intent
...
public void onClick(View view)
{
Intent myIntent = new Intent(A.this,
B.class);
startActivity(intent);
}
...
Activity and Intent
...
public void onClick(View view)
{
Intent myIntent = new Intent(A.this, B.class);
startActivity(myIntent);
}
...
The name of the Activity the app is currently running
Activity and Intent
...
public void onClick(View view)
{
Intent myIntent = new Intent(A.this,
B.class);startActivity(myIntent);
}
...
The name of the Activity that should be launched
Activity and Intent
...
public void onClick(View view)
{
Intent myIntent = new Intent(A.this,
B.class);
startActivity(myIntent);}
...
Launch Activity B now
Activity - Android Manifest File
● If you added the second Activity B using the wizard/template, then B will be automatically added to the Android Manifest file
● If you added it manually, then Activity B may not be in the Manifest File. This is an easy to get a Force Close when you try to launch Activity B!
● Android needs to know what Activities your app may launch
Passing Data between Activities
When you start Activity B from Activity A, you may want to send data to Activity B
Activity A Activity B
startActivity B
send some data to B also
Passing Data between Activities
Activity A - onCreate()
Intent intent = new Intent(this,
SecondActivity.class);
Bundle bundle = new Bundle();
bundle.putString( "fname", "John");
bundle.putString( "lname", "Doe");
bundle.putInt("age", 18);
intent.putExtras(bundle);
startActivity(intent);
Activity B - onCreate()
Intent intent = getIntent();
Bundle bundle = intent.getExtras();
if (bundle != null) {
edit1.setText(bundle.getString(
"fname"));
edit2.setText(bundle.getString(
"lname"));
int age= bundle.getInt( "age");
}
Activity - ListActivity
● Sometimes the entire UI for your Activity may be a ListView
● In this case, you can extend ListActivity instead of extending Activity
● You don't need a layout.xml file for a ListActivity!
Activity - ListActivitypublic class MyListActivity extends ListActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
String[] list = new String[] { "one", "two",
"three" };
ArrayAdapter<CharSequence> adapter =
new ArrayAdapter<CharSequence>(this,
android.R.layout.simple_list_item_1, list);
setListAdapter(adapter);
}
}
Activity - ListActivitypublic class MyListActivity extends ListActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
String[] list = new String[]{"one","two","three"};
ArrayAdapter<CharSequence> adapter =
new ArrayAdapter<CharSequence>(this,
android.R.layout.simple_list_item_1, list);
setListAdapter(adapter);
}
}
extend ListActivity instead of Activity
Activity - ListActivitypublic class MyListActivity extends ListActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
String[] list = new String[]{"one","two","three"};
ArrayAdapter<CharSequence> adapter =
new ArrayAdapter<CharSequence>(this,
android.R.layout.simple_list_item_1, list);
setListAdapter(adapter);
}
}
We don't need to setContentView because we're extending ListActivity
Activity - ListActivitypublic class MyListActivity extends ListActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
String[] list = new String[] { "Brazil", "China",
"Denmark"};
ArrayAdapter<CharSequence> adapter =
new ArrayAdapter<CharSequence>(this,
android.R.layout.simple_list_item_1, list);
setListAdapter(adapter);
}
}
We've seen this before in this lecture
Activity - ListActivitypublic class MyListActivity extends ListActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
String[] list = new String[]{ "Brazil", "China",
"Denmark"};
ArrayAdapter<CharSequence> adapter =
new ArrayAdapter<CharSequence>(this,
android.R.layout.simple_list_item_1, list);
setListAdapter(adapter);
}
}
We've also seen this before in this lecture
Activity - ListActivitypublic class MyListActivity extends ListActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
String[] list = new String[] { "Brazil", "China",
"Denmark"};
ArrayAdapter<CharSequence> adapter =
new ArrayAdapter<CharSequence>(this,
android.R.layout.simple_list_item_1, list);
setListAdapter(adapter);
}
}
We call setListAdapter on this ListActivity instead of calling it on a ListView (we don't have a ListView specified in an XML file this time!)
Activity - ListActivitypublic class MyListActivity extends ListActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
String[] list = new String[] { "Brazil", "China",
"Denmark"};
ArrayAdapter<CharSequence> adapter =
new ArrayAdapter<CharSequence>(this,
android.R.layout.simple_list_item_1, list);
setListAdapter(adapter);
}
}
This is equivalent to calling this.setListAdapter(adapter);i.e., using the this keyword to refer to this instance of MyListActivity
References
● The Busy Coder's Guide to Android Development - Mark Murphy
● Android Developers● The Mobile Lab at Florida State University