2016年9月9日 星期五

Android List View

Ref :  http://www.tutorialspoint.com/android/android_list_view.htm

Android ListView is a view which groups several items and display them in vertical scrollable list. The list items are automatically inserted to the list using an Adapter that pulls content from a source such as an array or database.
List View

LIST VIEW

An adapter actually bridges between UI components and the data source that fill data into UI Component. Adapter holds the data and send the data to adapter view, the view can take the data from adapter view and shows the data on different views like as spinner, list view, grid view etc.
The ListView and GridView are subclasses of AdapterView and they can be populated by binding them to an Adapter, which retrieves data from an external source and creates a View that represents each data entry.
Android provides several subclasses of Adapter that are useful for retrieving different kinds of data and building views for an AdapterView ( i.e. ListView or GridView). The common adapters are ArrayAdapter,Base Adapter,CursorAdapter, SimpleCursorAdapter,SpinnerAdapter andWrapperListAdapter. We will see separate examples for both the adapters.

ListView Attributes

Following are the important attributes specific to GridView −
AttributeDescription
android:idThis is the ID which uniquely identifies the layout.
android:dividerThis is drawable or color to draw between list items. .
android:dividerHeightThis specifies height of the divider. This could be in px, dp, sp, in, or mm.
android:entriesSpecifies the reference to an array resource that will populate the ListView.
android:footerDividersEnabledWhen set to false, the ListView will not draw the divider before each footer view. The default value is true.
android:headerDividersEnabledWhen set to false, the ListView will not draw the divider after each header view. The default value is true.

ArrayAdapter

You can use this adapter when your data source is an array. By default, ArrayAdapter creates a view for each array item by calling toString() on each item and placing the contents in a TextView. Consider you have an array of strings you want to display in a ListView, initialize a new ArrayAdapter using a constructor to specify the layout for each string and the string array −
ArrayAdapter adapter = new ArrayAdapter<String>(this,R.layout.ListView,StringArray);
Here are arguments for this constructor −
  • First argument this is the application context. Most of the case, keep itthis.
  • Second argument will be layout defined in XML file and havingTextView for each string in the array.
  • Final argument is an array of strings which will be populated in the text view.
Once you have array adapter created, then simply call setAdapter() on yourListView object as follows −
ListView listView = (ListView) findViewById(R.id.listview);
listView.setAdapter(adapter);
You will define your list view under res/layout directory in an XML file. For our example we are going to using activity_main.xml file.

Example

Following is the example which will take you through simple steps to show how to create your own Android application using ListView. Follow the following steps to modify the Android application we created in Hello World Example chapter −
StepDescription
1You will use Android Studio IDE to create an Android application and name it as ListDisplay under a package com.example.ListDisplay as explained in the Hello World Example chapter.
2Modify the default content of res/layout/activity_main.xml file to include ListView content with the self explanatory attributes.
3No need to change string.xml, Android studio takes care of default string constants.
4Create a Text View file res/layout/activity_listview.xml. This file will have setting to display all the list items. So you can customize its fonts, padding, color etc. using this file.
6Run the application to launch Android emulator and verify the result of the changes done in the application.
Following is the content of the modified main activity filesrc/com.example.ListDisplay/ListDisplay.java. This file can include each of the fundamental life cycle methods.

package com.example.ListDisplay;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class ListDisplay extends Activity {
   // Array of strings...
   String[] mobileArray = {"Android","IPhone","WindowsMobile","Blackberry","WebOS","Ubuntu","Windows7","Max OS X"};
   
   @Override
   protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
      
      ArrayAdapter adapter = new ArrayAdapter<String>(this, R.layout.activity_listview, mobileArray);
      
      ListView listView = (ListView) findViewById(R.id.mobile_list);
      listView.setAdapter(adapter);
   }
}

沒有留言:

張貼留言