Android: listactivity Learning

Source: Internet
Author: User

Listactivity is a subclass of activity.

 

An activity that displays a list of items by binding to a data source such as an array or cursor, and exposes Event Handlers when the user selects an item.

 

Displays data in the form of a list. Its data source mainly comes from arrays or cursor. It also provides some processing functions for processing a user to select an item, which is also called a callback function, it inherits from activity, so many methods in activity can be used.

 

Listactivity hostsListViewObject that can be bound to different data sources, typically either an array or a cursor holding query results. Binding, screen layout, and row layout are discussed in the following sections.

 

To use listactivity to display and bind data, you must specify screen layout, which is the overall display layout file on the screen. Row layout is the layout file displayed on each row in listview.

 

Screen Layout

Listactivity has a default layout that consists of a single, full-screen list in the center of the screen. however, if you desire, you can customize the screen layout by setting your own view layout with setcontentview () in oncreate (). to do this, your own view must contain a listview object with the ID "@ Android: ID/List" (orlistIf it's in code)

 

You can customize the listview effect, but the listview ID must be set to list.

 

Optionally, your custom view can contain another view object of any type to display when the list view is empty. this "empty list" notifier must have an id "Android: empty ". note that when an empty view is present, the list view will be hidden when there is no data to display.

 

It is best to specify what to display when no data is available. This requires "@ Android: ID/empty" as the ID, which is specified by the system.

 

The following is an example of screen layout:

 

<? XML version = "1.0" encoding = "UTF-8"?>
<Linearlayout xmlns: Android = "http://schemas.android.com/apk/res/android"
Android: Orientation = "vertical"
Android: layout_width = "match_parent"
Android: layout_height = "match_parent"
Android: paddingleft = "8dp"
Android: paddingright = "8dp">

<Listview Android: Id = "@ ID/Android: List"
Android: layout_width = "match_parent"
Android: layout_height = "match_parent"
Android: Background = "#00ff00"
Android: layout_weight = "1"
Android: drawselectid Top = "false"/>

<Textview id = "@ ID/Android: empty"
Android: layout_width = "match_parent"
Android: layout_height = "match_parent"
Android: Background = "# ff0000"
Android: text = "no data"/>
</Linearlayout>
 

Row Layout

You can specify the layout of individual rows in the list. you do this by specifying a layout resource in the listadapter object hosted by the activity (the listadapter binds the listview to the data; more on this later ).

 

You can set the layout of a specific row in the list. listadapter binds the listview to the data.

 

A listadapter constructor takes a parameter that specifies a layout resource for each row. it also has two additional parameters that let you specify which data field to associate with which object in the row layout resource. these two parameters are typically parallel arrays.

 

Android provides some standard row layout resources. These are inR.layoutClass, and have names such as simple_list_item_1, simple_list_item_2, and two_line_list_item. the following layout XML is the source for the resource two_line_list_item, which displays two data fields, one above the other, for each list row.

 

 

Android provides some standard row layout Resources in the R. layout class, such as simple_list_item_1, simple_list_item_2, and two_line_list_item. The following is an XML file for two_line_list_item implementation:

 <?xml version="1.0" encoding="utf-8"?>
 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
     android:layout_width="match_parent"
     android:layout_height="wrap_content"
     android:orientation="vertical">

     <TextView android:id="@+id/text1"
         android:textSize="16sp"
         android:textStyle="bold"
         android:layout_width="match_parent"
         android:layout_height="wrap_content"/>

     <TextView android:id="@+id/text2"
         android:textSize="16sp"
         android:layout_width="match_parent"
         android:layout_height="wrap_content"/>
 </LinearLayout>
 
When the system provides the two_line_list_item row layout, the IDs of the two textviews: text1 and text2 can be used globally,
Therefore, do not define the same ID elsewhere to avoid conflict.

You must identify the data bound to each textview object in this layout. The syntax for this is discussed in the next section.

 

Binding to data

 

You bind the listactivity's listview object to data using a class that implementsListAdapterInterface. Android provides two standard list adapters:SimpleAdapterFor static data (MAPS), andSimpleCursorAdapterFor cursor query results.

 

You can bind a listview object to data by using a class that implements the listadapter interface. Android provides two standard list adapters: simpleadapter for static data (MAPS) and simplecursoradapter for cursor query results.

 

The following is an example in the SDK:

 

Public class mylistadapter extends listactivity {

@ Override
Protected void oncreate (bundle savedinstancestate ){
Super. oncreate (savedinstancestate );

// We'll define a custom screen layout here (the one shown above),
// Typically, you can use the standard listactivity layout.
Setcontentview (R. layout. custom_list_activity_view );

// Query for all people contacts usingContacts.PeopleConvenience class.
// Put a managed wrapper around the retrieved cursor so we don't have to worry about
// Requerying or closing it as the activity changes state.
Mcursor = This. getcontentresolver (). Query (people. content_uri, null );
Startmanagingcursor (mcursor );

// Now create a new list adapter bound to the cursor.
// Simplelistadapter is designed for binding to a cursor.
Listadapter adapter = new simplecursoradapter (
This, // context.
Android. R. layout. two_line_list_item, // specify the row template to use (here, two columns bound to the two retrieved cursor
Rows ).
Mcursor, // pass in the cursor to bind.
New String [] {People. Name, people. Company}, // array of cursor columns to bind.
New int [] {Android. R. Id. text1, Android. R. Id. text2}); // parallel array of which template objects to bind to those columns.

// Bind to our new adapter.
Setlistadapter (adapter );
}
}
 

Now let's take a look at simplecursoradapter:

An easy adapter to map columns from a cursor to textviews or imageviews defined in an XML file. you can specify which columns you want, which views you want to display the columns, and the XML file that defines the appearance of these views.

 

Simplecursoradapter is mainly used to display data in the database as a data source. You can specify the columns, views, and XML files used to display database tables.

 

Let's take a look at the old constructor:

 

 

 

Public simplecursoradapter (context, int layout, cursor C, string [] From, int [])

Constructor.

Context The context where the listview associated with this simplelistitemfactory is running
Layout Resource identifier of a layout file that defines the views for this list item. The layout file shocould include at least those named views defined in ""
C The database cursor. Can be null if the cursor is not available yet.
From A list of column names representing the data to bind to the UI. Can be null if the cursor is not available yet.
To

The views that shoshould display column in the "from" parameter. these shoshould all be textviews. the first n views in this list are given the values of the First n columns in the FROM parameter. can be null if the cursor is not available yet.

The following is a brief introduction to simpleadapter: public class

Simpleadapter

Extends baseadapter
Implements filterable class overview

An easy adapter to map static data to views defined in an XML file. you can specify the data backing the list as an arraylist of maps. each entry in the arraylist corresponds to one row in the list. the maps contain the data for each row. you also specify an XML file that defines the views used to display the row, and a mapping from keys in the map to specific views. binding data to views occurs in two phases.

 

This is a simple adapter class that maps static data to the view defined in the XML file. You can save the data to be displayed in the listview as a map-type arraylist. Each data entry in the arraylist corresponds to a row in the listview.

 

Let's take a look at the constructor:

 

Public simpleadapter (context, list <? Extends Map <string,?> Data, int resource, string [] From, int [])

Since: API Level 1

Constructor

Context The context where the view associated with this simpleadapter is running
Data A list of maps. Each entry in the list corresponds to one row in the list. The maps contain the data for each row, and shoshould include all the entries specified in "from"
Resource Resource identifier of a view layout that defines the views for this list item. The layout file shoshould include at least those named views defined in ""
From A list of column names that will be added to the map associated with each item.
To The views that shocould display column in the "from" parameter. These shocould all be textviews. The first n views in this list are given the values of the First n columns in the FROM parameter.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.