Simpleadapter and listview for common android controls

Source: Internet
Author: User

1. simpleadapter

Simpleadapter is a simple adapter that maps static data to a view defined in an XML file. You can specify a list supported by data, such as a map composed of arraylist. Each entry in the arraylist corresponds to a row in the list. Maps contains each row of data. You can specify a view XML file that is used to display rows and map it to the specified view using keywords.

Constructor

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

Parameters

Context associates with the context of the view running on simpleadapter.

A list of data maps. Each entry in the list corresponds to a row in the list, which should contain all the entries specified in the from

Resource: the unique identifier of a resource that defines the view layout of a list item. The layout file should contain at least the names defined in.

From a list of column names that will be added to map associated with each project

To should display the column view in the FROM parameter. All of these should be textview. The initial n view in the list is the value obtained from the initial n column of the from parameter.

A simlpleadapter works like this. Assume that simpleadapter is used for listview. Each list item in listview is the layout specified by the resource parameter value. The data parameter is the data to be loaded into the listview. Let's first look at each list item. Assume that the layout file corresponding to the list item contains two components: textview and edittext, with the ID textview and edittext respectively. When loading a list item, you must use the component ID to correspond to the map object in the list element in the Data parameter. Therefore, the from parameter is the key of the map object, and to represents the component ID. For example, the parameter value in this example is from = new string [] {"userid", "username "}, to = new int [] {R. id. userid, R. id. username }. This means to bind the value of key userid in the map object to R. Id. userid, And the username is similar. Now let's look at the data parameter. A listview consists of multiple list items. Each list item provides data by a map object, while multiple list items provide multiple map objects by the list object.

Ii. listview

In Android development, listview is a commonly used component that displays specific content in the form of a list and can be automatically displayed based on the length of data.

The display of the list requires three elements:

1. listveiw is used to display the view of the list.

2. the adapter is used to map data to the mediation on the listview.

3. The specific string, image, or basic component of the data to be mapped.

Based on the list of adapter types, the list is divided into three types: arrayadapter, simpleadapter, and simplecursoradapter. Arrayadapter is the simplest and can only display one line of words. Simpleadapter has the best scalability and Can Customize various effects. Simplecursoradapter can be considered as a simple combination of simpleadapter on the database. It can display the database content in the form of a list. Simpleadapter inherits from adapterview. You can add a listener to the listview by using the setonitemclicklistener () method. When you click a list item, perform the corresponding operation. In the listener, public abstract void onitemclick (adapterview <?> Parent, view, int position, long ID) method.

To access data related to the selected items, the execution program can call getitematposition (position ).

Parameters

The adapterview of the parent click action.

View clicked in adapterview (a view provided by the adapter ).

Position view position in the adapter.

ID of the row of the clicked element.

Example:We use simpleadapter as an example to describe the usage of listview.

Android_listview.java

Package com.idea.org; </P> <p> Import Java. util. arraylist; <br/> Import Java. util. hashmap; </P> <p> Import android. app. activity; <br/> Import android. OS. bundle; <br/> Import android. view. view; <br/> Import android. widget. adapterview; <br/> Import android. widget. adapterview. onitemclicklistener; <br/> Import android. widget. listview; <br/> Import android. widget. simpleadapter; <br/> Import android. widget. toast; </P> <P> public class android_listview extends activity {<br/>/** called when the activity is first created. */<br/> @ override <br/> Public void oncreate (bundle savedinstancestate) {<br/> super. oncreate (savedinstancestate); <br/> setcontentview (R. layout. main); <br/> arraylist <pashmap <string, string> List = new arraylist <pashmap <string, string> (); <br/> hashmap <string, string> map1 = new hashmap <string, STR Ing> (); <br/> hashmap <string, string> MAP2 = new hashmap <string, string> (); <br/> hashmap <string, string> map3 = new hashmap <string, string> (); <br/> listview = (listview) findviewbyid (R. id. listview); <br/> map1.put ("userid", "100001"); <br/> map1.put ("username", "user 1"); <br/> list. add (map1); <br/> map2.put ("userid", "100002"); <br/> map2.put ("username", "user 2 "); <br/> list. add (MAP2); <br/> map3.put ("Userid", "100003"); <br/> map3.put ("username", "user 3"); <br/> list. add (map3); <br/> // defines a simpleadapter. Each row has two textviews, indicating userid and username. <br/> simpleadapter = new simpleadapter (this, list, r. layout. user, <br/> New String [] {"userid", "username"}, new int [] {R. id. userid, R. id. username}); <br/> // Add an adapter for listview <br/> listview. setadapter (simpleadapter); // set the data behind the listview to simpleadapter. <Br/>/* Add a click listener for the listview. Import android. widget. adapterview. onitemclicklistener; statement <br/> * When you click a list item, use toast to display the text in this list item <br/> */<br/> listview. setonitemclicklistener (New onitemclicklistener () {</P> <p> @ override <br/> Public void onitemclick (adapterview <?> Arg0, view arg1, int arg2, <br/> long arg3) {<br/> listview = (listview) arg0; <br/> toast. maketext (android_listview.this, listview. getitematposition (arg2 ). tostring (), <br/> toast. length_short ). show (); <br/>}< br/>}); <br/>}< br/>}

Main. xml

<? XML version = "1.0" encoding = "UTF-8"?> <Br/> <linearlayout xmlns: Android = "http://schemas.android.com/apk/res/android" <br/> Android: Orientation = "vertical" <br/> Android: layout_width = "fill_parent" <br/> Android: layout_height = "wrap_content" <br/> <! -- Add a listview control --> <br/> <listview <br/> Android: Id = "@ + ID/listview" <br/> Android: layout_width = "fill_parent" <br/> Android: layout_height = "wrap_content" <br/> </linearlayout> <br/>

The layout file user. xml used by simpleadapter

<? XML version = "1.0" encoding = "UTF-8"?> <Br/> <linearlayout xmlns: Android = "http://schemas.android.com/apk/res/android" <br/> Android: Orientation = "horizontal" <br/> Android: layout_width = "fill_parent" <br/> Android: layout_height = "fill_parent" <br/> Android: paddingright = "10dip" <br/> Android: paddingleft = "10dip" <br/> Android: paddingtop = "1dip" <br/> Android: paddingbottom = "1dip" <br/> <! -- Each item of listview has two textviews, respectively displaying userid and username --> <br/> <textview <br/> Android: id = "@ + ID/userid" <br/> Android: layout_width = "fill_parent" <br/> Android: layout_height = "wrap_content" <br/> Android: textsize = "20pt" <br/> Android: layout_weight = "1" <br/> <textview <br/> Android: id = "@ + ID/username" <br/> Android: layout_width = "fill_parent" <br/> Android: layout_height = "wrap_content" <br/> Android: textsize = "20pt" <br/> Android: layout_weight = "1" <br/> </linearlayout> <br/>

Running Effect

The strange thing is that listview cannot be filled with the whole screen of the android3.0 simulator. I don't know whether it is due to the android3.0 version or other reasons.

Related Article

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.