Showing posts with label ArrayAdapter. Show all posts
Showing posts with label ArrayAdapter. Show all posts

Add a Dynamic ListView, Step-By-Step

How to Add a Dynamic ListView to your Android Project, Step-By-Step

After each step you should be able to compile and test, with no errors.

A "Dynamic ListView" is a ListView where items can be added.
A "Static ListView" is a ListView where the items in the ListView will not be changed.

1. Create a List with an ArrayList.

2. Link your ArrayAdapter to the ListView named myListView.

3. Compile and run!

Next Recommended Article: Do Action on ListView Item Click


How to Link List to ArrayAdapter

1. Create a list called myList.

2. Create an ArrayAdapter.

3. In MainActivity.java, add the line below to the onCreate method. This is linked to a list named myList.

myArrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, myList);

4. Compile and test!

Resource:

How to Create an ArrayAdapter

ArrayAdapter: Added in API level 1

1. If you don't already have an Android project, create a new project. 

2. In the MainActivity.java file, add the below line to the imports section.

import android.widget.ArrayAdapter;

3. In the MainActivity.java file, add the below line in the onCreate method.

    ArrayAdapter<String> myArrayAdapter;

4. Compile and run!

Previous Recommended Article: How to Create a String Array

Resources:

How to Add a Static ListView to your Android Project

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!

setAdapter - How to Link an ArrayAdapter to a ListView without extending ListActivity


1. Add the basic ListView.

2. Add an ArrayAdapter.

3. Link the ArrayAdapter to the ListView by adding this bolded line of code to the onCreate method in the MainActivity.java file.

myListView.setAdapter(myArrayAdapter);

4. Compile and run!

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:

Solution for: error: cannot find symbol, "ArrayAdapter"

Problem:

    [javac] Compiling 3 source files to C:\xxxx\Workspace\MyApp\bin\classes
    [javac] C:\xxxx\Workspace\MyApp\src\com\akp\MyApp\MainActivity.java:22: error: cannot find symbol
    [javac]     ArrayAdapter<String> adapter;
    [javac]     ^
    [javac]   symbol:   class ArrayAdapter
    [javac]   location: class MainActivity

Solution: 

1. Open the MainActivity.java file and add the below line of code to the imports section. 

import android.widget.ArrayAdapter;

2. Compile and test the app.