This is the first blog post in series of posts that will demonstrate how Android ListView can be used in your application. The series will start off with the most basic loading of hard coded arraylist in a ListView. Then we will progress through various applications that are more advanced as we follow through the series of posts.
Here is the general outline of the series of posts.
1. Example 1 – ListView with ArrayList. Simply load the hard coded arraylist in the ListView using default Android data adapters and simple list item layout.
2. Example 2 – Display multi select checkboxes on the ListView using ArrayList. We will also save user selections in private preferences of the user and reload the same when application gets restarted.
3. Example 3 – Display multi select checkboxes on the ListView using Xml file and standard Android data adapters and list item layout. We will also save user selections in private preferences of the user and reload the same when application gets restarted.
4. Example 4 – Display multi select checkboxes on the ListView using Sqlite database and Android cursor data adapter. We will also save user selections in private preferences of the user and reload the same when application gets restarted.
5. Example 5 – Display multi select checkboxes on the ListView from Sqlite database using custom data Adapter and custom list item layout.
Lets jump into the first example.
Skill Assumptions:
- You have configured your Android development environment on Eclipse or other tools
- You know how to create a new Android project
Step 1 – Create a new Eclipse project
Step 2 -Â Add a default Activity named Start which will add main.xml under layout folder and Start.java under your namespace folder under src.
Step 3 – Copy paste following Xml on your main.xml file.
// layout/main.xml
Step 4 – Copy paste following code in your AndroidManifest.xml file. The most important attribute is to android:theme=”@android:style/Theme.NoTitleBar” which will disable the default Android title bar because we are replacing that with our own Action bar.
// AndroidManifest.xml
Step 5 – Copy and paste following code in your Start.java class file. Notice the resource id – android.R.layout.simple_list_item_1. That is the magic default Android layout that allows us to fill up the ListView without specifying the layout Xml for individual list items.
// src/Start.java
package com.appfulcrum.blog.examples;
import java.util.ArrayList;
import android.app.Activity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;
public class Start extends Activity {
private String[] lv_arr = {};
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// Prepare an Array list of todo items
ArrayList listTODO = PrepareList();
// Get a handle to the list view
ListView lv = (ListView) findViewById(R.id.ListView01);
// Bind the data with the list
lv_arr = (String[]) listTODO.toArray(new String[0]);
lv.setAdapter(new ArrayAdapter(Start.this,
android.R.layout.simple_list_item_1, lv_arr));
}
// The main ArrayList .
private ArrayList PrepareList() {
ArrayList todoItems = new ArrayList();
todoItems.add("Fill up Gasoline");
todoItems.add("Wash car");
todoItems.add("Dinner with friends");
todoItems.add("Watch Movie");
return todoItems;
}
}
Step 6 – Run the application as Android Application and you should see below for output.
