A "Static ListView" meaning that the items in the ListView will not be changed. For a ListView that you'd like to add items to, see "Dynamic ListView".
1. Add a Basic ListView.
2. Add an array of items and name it arrayTasks.
For testing, make the list "Task 1", Task 2", Task 3", "Task 4", "Task 5"
3. Create an ArrayAdapter named myAdapter.
4. Link your ArrayAdapter to your Array.
5. Link your ArrayAdapter to the ListView named myListView.
6. Compile and run!
Showing posts with label Array. Show all posts
Showing posts with label Array. Show all posts
How to Link a String Array to an ArrayAdapter
ArrayAdapter was added in API level 1
1. Create an String Array to link to your ArrayAdapter. Name this array, myArray.
2. Create an ArrayAdapter.
3. Open the MainActivity.java file and add the below code to the onCreate method. The row layout that should be used for each row is named android.R.layout.simple_list_item_1. This uses the Android stock list item xml file of android.R.layout.simple_list_item located in your Android platform directory. You may use your own row layout instead, defined in res\layout directory.
myArrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, myArray);
4. Compile and run!
5. Here are some other stock list row item xml files:
test_list_item.xml - API 10
simple_list_item_2.xml - API 10
simple_list_item_checked.xml - API 10
simple_list_item_multiple_choice.xml - API 10
simple_list_item_single_choice.xml - API 10
simple_list_item_activated_1.xml - API 11
simple_list_item_activated_2.xml - API 11
simple_selectable_list_item.xml - API 11
simple_gallery_item.xml - API 13
simple_list_item_2_single_choice.xml - API 19
Resources:
1. Create an String Array to link to your ArrayAdapter. Name this array, myArray.
2. Create an ArrayAdapter.
3. Open the MainActivity.java file and add the below code to the onCreate method. The row layout that should be used for each row is named android.R.layout.simple_list_item_1. This uses the Android stock list item xml file of android.R.layout.simple_list_item located in your Android platform directory. You may use your own row layout instead, defined in res\layout directory.
myArrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, myArray);
4. Compile and run!
5. Here are some other stock list row item xml files:
test_list_item.xml - API 10
simple_list_item_2.xml - API 10
simple_list_item_checked.xml - API 10
simple_list_item_multiple_choice.xml - API 10
simple_list_item_single_choice.xml - API 10
simple_list_item_activated_1.xml - API 11
simple_list_item_activated_2.xml - API 11
simple_selectable_list_item.xml - API 11
simple_gallery_item.xml - API 13
simple_list_item_2_single_choice.xml - API 19
Resources:
How to Create a String Array
Note: The size of an array can't be modified. If you want a bigger array a new array needs to be instantiated
1. If you don't already have your Android project, create your project.
2. Open the MainActivity.java file and add the below line in your onCreate method.
This will define and initialize a single array named myArray.
String[] myArray = {"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"};
3. Compile and test!
4. Another way to create it.
String[] myArray = new String[] { "January", "February", "March" };
5. Other useful String Arrays.
// Days of the Month
//Days of the Week
Next Recommended Article: How to Create an ArrayAdapter
Resources:
1. If you don't already have your Android project, create your project.
2. Open the MainActivity.java file and add the below line in your onCreate method.
This will define and initialize a single array named myArray.
String[] myArray = {"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"};
3. Compile and test!
4. Another way to create it.
String[] myArray = new String[] { "January", "February", "March" };
5. Other useful String Arrays.
// Days of the Month
String[] myArray = {"1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23", "24", "25", "26", "27", "28", "29", "30", "31"};
//Days of the Week
String[] myArray = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
Next Recommended Article: How to Create an ArrayAdapter
Resources:
Subscribe to:
Posts (Atom)
Popular Posts
- Solution: Running Android Lint has encountered a problem Failed
- Perform an Action after Enter keypress on EditText
- How to Declare a URLConnection object
- How to Get a new HttpURLConnection
- How to Create a ProGuard Exception
- How to Get a new URL Connection
- How to Reference a Library in your Project
- How to Register an Activity that Responds to NFC tags for URI's that point to a URL
- How to Import a Library
- How to Open the SDK Manager