Core Concepts Covered
- Use the built-in android.R.layout.simple_list_item_1 to create an Android UI List.
- Use android.widget.ArrayAdapter to display a List of Strings data in the Android UI List.
- Create class extending android.app.ListActivity.
- Obtain data in List of Strings data structure.
- Create an android.widget.ArrayAdapter that connects the List of Strings data to android.R.layout.simple_list_item_1.
- Bind the android.widget.ListAdapter using the ListActivity.setListAdapter method.
1. Create class extending android.app.ListActivity.
public class MainActivity extends ListActivity {
@Override
protected final void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
}
For this tutorial, we will use a List of Strings containing the 13 US original colonies. View the full UsState.java code listing.
public final class UsState {
public static final List<String> ORIGINAL_COLONIES = initColonies();
private UsState() { }
private static List<String> initColonies() {
final List<String> colonies = new ArrayList<String>();
colonies.add("Delaware");
colonies.add("Pennsylvania");
colonies.add("New Jersey");
return Collections.unmodifiableList(colonies);
}
}
private ListAdapter createListAdapter(final List<String> list) {
return new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, list);
}
4. Bind the android.widget.ListAdapter using the ListActivity.setListAdapter method.
public class MainActivity extends ListActivity {
@Override
protected final void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
final ListAdapter listAdapter = createListAdapter(UsState.ORIGINAL_COLONIES);
setListAdapter(listAdapter);
}
}
The final result will look like this
Source Code: Git | SVN | Download Zip
Delta I found this website crystaldiskmark and valued the dependable benchmark outputs, practical interface, and accurate performance measurements, helping users compare drives efficiently while understanding system capabilities more clearly overall. It remains informative reliable, and useful for planning improvements ahead.
ReplyDeleteSolstice I recently explored the engaging website dshidmini and appreciated its informative content, clean layout, and practical features. The platform delivers useful resources efficiently, helping visitors enjoy a seamless experience while accessing dependable information and guidance whenever needed.
ReplyDeletePassion I recently visited the website deskpins and genuinely admired its accessible layout, dependable tools, and smooth functionality. Everything is presented clearly, helping users stay organized while enjoying a practical and professional online experience throughout.
ReplyDelete