Android learning notes listview Adapter

Source: Internet
Author: User

The function of listview is to display a list. The list content item can be customized, fill the content to be displayed in the adapter, and then use the listview class

Setadapter (listadapter adapter) method to complete the settings of listview. Next, let's take a look at the listview. below is my test. There are two items in total, occupying two rows. Next, let's take a look at the specific usage method to get a listview. It's very easy to write as follows in the main. xml file:
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:orientation="vertical" android:layout_width="fill_parent"android:layout_height="fill_parent"><ListView android:layout_height="wrap_content" android:id="@+id/lvt"android:layout_width="fill_parent"></ListView></LinearLayout>

Then, in the program, as long:

Public void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); setcontentview (R. layout. main); Lv = (listview) findviewbyid (R. id. lvt); LV. setadapter (New ADT (New String [] {"lingsha", "Ziying "}));}

Get the listview and set the adapter. So now the key lies in how to fill the data we need to display into the adapter. There are many adapters available for us to use. Currently, I have access to simpleadapter and baseadapter.
Next let's take a look at simpleadapter.
Simpleadapter, which can be constructed by the constructor. See its constructor:

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

Context. It is not explained. This is generally used.
Data. What you need here is the data you want to display in the listview. Its type should be a linked list of the graph type,
I believe you can understand the description. If you cannot understand the description, please refer to this example:
Arraylist In this way, each node will eventually correspond to an item.
Resource, which is the XML layout implementation of each item. You can customize the item in this XML, for example:

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="fill_parent" android:layout_height="fill_parent"><TextView android:text="TextView" android:id="@+id/tv"android:layout_width="wrap_content" android:layout_height="wrap_content"></TextView></LinearLayout>

I just put a textview. For simplicity, we can add the required controls to it as needed.
In the last two parameters, from and to are the correspondence between the attribute values of each node in data to the attribute values of each control in item.
Here is an example:
If the main. xml file is not mentioned much, it will not be pasted out.
Let's see the layout file my_listitem.xml of item:

<?xml version="1.0" encoding="utf-8"?>  <LinearLayout           android:layout_width="fill_parent"           xmlns:android="http://schemas.android.com/apk/res/android"           android:orientation="vertical"          android:layout_height="wrap_content"           android:id="@+id/MyListItem"           android:paddingBottom="3dip"           android:paddingLeft="10dip">          <TextView                   android:layout_height="wrap_content"                   android:layout_width="fill_parent"                   android:id="@+id/ItemTitle"                   android:textSize="30dip">          </TextView>          <TextView                   android:layout_height="wrap_content"                   android:layout_width="fill_parent"                   android:id="@+id/ItemText">          </TextView>  </LinearLayout>  

Two textviews are defined in each item.
Then let's look at the Java file:

Public void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); setcontentview (R. layout. main); // bind the listview in XML as the item container listview list = (listview) findviewbyid (R. id. mylistview); // generates a dynamic array and returns the arraylist 

The above is the usage of simpleadapter. Let's take a look at baseadapter.
It can be inherited and used when using baseadapter. The key lies in the Data filling method. The principle is naturally the same.
Layoutinflater is used for applying the layout XML of item.
Check the chestnuts:
The main. xml file will not be pasted. The following is the layout file viewitem. xml of the item.

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="fill_parent" android:layout_height="fill_parent"><TextView android:text="TextView" android:id="@+id/tv"android:layout_width="wrap_content" android:layout_height="wrap_content"></TextView></LinearLayout>

Then Java:

Public class test extends activity {private listview lv;/** called when the activity is first created. * // @ overridepublic void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); setcontentview (R. layout. main); Lv = (listview) findviewbyid (R. id. lvt); LV. setadapter (New ADT (New String [] {"lingsha", "Ziying"});} public class ADT extends baseadapter {view [] viewitem; public ADT (string [] viewtexts) {viewitem = new view [viewtexts. length]; for (INT I = 0; I <viewtexts. length; I ++) {viewitem [I] = makeview (viewtexts [I]);} private view makeview (string S) {// obtain the layoutinflater object layoutinflater lin = (layoutinflater) test. this. getsystemservice (context. layout_inflater_service); // make the layout File View VT = Lin. inflate (R. layout. viewitem, null); // set textview TV = (textview) VT for the corresponding attributes of our elements in the item. findviewbyid (R. id. TV); TV. settext (s); Return VT ;}@ overridepublic int getcount () {// todo auto-generated method stubreturn viewitem. length ;}@ overridepublic object getitem (INT position) {// todo auto-generated method stubreturn viewitem [position] ;}@ overridepublic long getitemid (INT position) {// todo auto-generated method stubreturn position;} @ overridepublic view getview (INT position, view convertview, viewgroup parent) {// todo auto-generated method stubreturn viewitem [position] ;}}








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.