Use adapter to provide data for listview (2)

Source: Internet
Author: User
When some list data is collected to the mobile phone screen in the previous section, the listview component + arrayadapter are usually used.  

Although it can provide us with the ability to display data lists, the items displayed cannot be customized. If our items are composed of two textviews, it will be powerless. Most of the projects not only display simple item templates, but more often we can customize the item templates to meet our needs, suppose that the item template needs to display two textviews? What should I do?

We can use simpleadapter + listview.

One of the constructors of simpleadapter is as follows:

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

The first parameter is the current context object. The second parameter: A list type generic set. The generic type must inherit the map type. Third: the layout resource ID,

Fourth parameter: List of keys to be bound. Fifth parameter: the specific component Id set in the resource file corresponding to the key list item.

With the above theoretical basis, we know that using simpleadapter brings the following benefits:

1: You can customize any template with a high degree of freedom (depending on the level of the artist)

2: You can specify a matching data for the item template.

Let's take a look at the layout of the item template. It is very simple: An imageview and two textviews.

View code

 <?  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 = "Match_parent" >


< Imageview
Android: layout_width = "Wrap_content"
Android: layout_height = "Wrap_content"
Android: ID = "@ + ID/IMG" >
</ Imageview >


< Textview
Android: ID = "@ + ID/txtname"
Android: layout_width = "Wrap_content"
Android: layout_height = "Wrap_content"
>
</ Textview >

< Textview
Android: paddingleft = "20sp"
Android: ID = "@ + ID/txtlength"
Android: layout_width = "Wrap_content"
Android: layout_height = "Wrap_content"
>
</ Textview >

</ Linearlayout >

 

We can define a data source. Of course, this data source inherits from the list set interface and the type is the basic map interface. As follows:

View code

 List  <  Map  <  String, Object  > Lists  =  New  Arraylist  <  Map  <  String, Object  >  ();
For ( Int I = 0 ; I < 4 ; I ++ ){
Map < String, Object > Map = New Hashmap < String, Object > ();
Map. Put ( " IMG " , R. drawable. Icon );
Map. Put ( " Name " , " Simpleadapter " + I );
Map. Put ( " Length " , " 300 " );
Lists. Add (MAP );
}

Then we want to bind the data to the specified component, allCodeAs follows:

View code

 List  <  Map  <  String, Object  >  Lists  =  New  Arraylist  < Map  <  String, Object  >  ();
For ( Int I = 0 ; I < 4 ; I ++ ){
Map < String, Object > Map = New Hashmap < String, Object > ();
Map. Put ( " IMG " , R. drawable. Icon );
Map. Put ( " Name " , " Simpleadapter " + I );
Map. Put ( " Length " , " 300 " );
Lists. Add (MAP );
}

String [] From = { " IMG " , " Name " , " Length " };
Int [] = Using r.id.img,r.id.txtname,r.id.txt length };
Simpleadapter Adapter = New Simpleadapter ( This , Lists, R. layout. Image, from, );
Listview. setadapter (adapter );

Let's see how it works:

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.