With the help of ListView, we can display a list of items in an easily scrollable manner. The adapter pulls the data (list) from any data source (array, xml, result set of a database query etc) and convert each and every item to a view present in list.
Here is a live demo of the ListView, we populated using the list of most populous Counties in USA.
Let's see how to create a ListView.
Create a new project and in the activity_main.xml, place a ListView element.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <ListView android:id="@+id/simple_list" android:layout_width="fill_parent" android:layout_height="wrap_content" android:divider="#030303" android:dividerHeight="1dp" /> </LinearLayout>
Create another xml file list_activity.xml and place a TextView element in a Linear Layout
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <TextView android:id="@+id/txt_list" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_gravity="center" android:padding="@dimen/activity_horizontal_margin" android:textColor="#000000" android:text="Counties" android:textSize="30dp"/> </LinearLayout>
Now let's look at the MainActivity java class. The names of the Counties in USA will be declared inside a String array.
import android.os.Bundle; import android.app.Activity; import android.widget.ArrayAdapter; import android.widget.ListView; public class MainActivity extends Activity { ListView cList; // Array of strings... String countyList[] = { "Los Angeles", "Cook", "Harris", "Maricopa", "San Diego", "Orange", "Miami-Dade", "Kings", "Dallas", "Queens", "Riverside", "San Bernardino", "King", "Clark", "Tarrant", "Santa Clara", "Broward", "Bexar", "Wayne", "New York", "Philadelphia", "Alameda", "Middlesex", "Suffolk", "Sacramento", "Bronx", "Nassau", "Palm Beach", "Cuyahoga", "Hillsborough", "Allegheny", "Oakland", "Franklin", "Orange", "Hennepin", "Fairfax", "Contra Costa", "Travis", "Salt Lake", "St. Louis", "Montgomery", "Pima", "Honolulu", "Westchester", "Milwaukee", "Fulton", "Mecklenburg", "Fresno", "Shelby", "Wake", "Fairfield", "DuPage", "Erie", "Pinellas", "Marion", "Bergen", "Hartford", "Prince George's", "Duval", "New Haven", "Kern", "Macomb", "Ventura", "Gwinnett", "El Paso", "San Francisco", "Collin", "Baltimore", "Pierce", "Montgomery", "Worcester", "Hamilton", "Hidalgo", "Essex", "Multnomah", "Jefferson", "Monroe", "Oklahoma", "Suffolk", "San Mateo", "Snohomish", "Lake", "DeKalb", "Cobb", "San Joaquin", "Denton", "Will", "Jackson", "Norfolk", "Bernalillo", "Jefferson", "Hudson", "El Paso", "Davidson", "Lee", "Monmouth", "Bucks", "Providence" }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); cList = (ListView) findViewById(R.id.simple_list); //Initialize ArrayAdapter with context,layout,textView id and String array ArrayAdapterarrayAdapter = new ArrayAdapter (this, R.layout.list_activity, R.id.txt_list, countyList); //set adapter to listview cList.setAdapter(arrayAdapter); } }
With this the setup is complete. Now try running the app & you should get the output, you see in the live demo