The ListView Adapter uses (BIND data) to customize the layout of each item to bind data.

Source: Internet
Author: User

The first example shows:

I believe everyone has seen this interface. This time, more than in the previous example, each of the items bound by ListView is no longer simply a string, for each entry of ListView, We need to display two information, ID and name. we can see that we have bound ten pieces of data and used a for loop to create a HashMap for each piece of binding, and then put the data in it. The data has the corresponding key, that is, id, name. When we get the data, we will find the data we put in based on this key. This is a bit similar to the key-value pair. If you do not know this, you will not understand it.

Paste the source code and then explain:

Copy codeThe Code is as follows :/***
*/Package com. cz. list. demo;
Import java. util. ArrayList;
Import java. util. HashMap;
Import android. app. Activity; import android. OS. Bundle;
Import android. widget. ListView; import android. widget. SimpleAdapter;
/**
* @ Author CZ *
*/Public class SimpleAdapterListDemo extends Activity {
Private ListView listView;
/** (Non-Javadoc)
** @ See android. app. Activity # onCreate (android. OS. Bundle)
* // @ Override
Protected void onCreate (Bundle savedInstanceState) {// TODO Auto-generated method stub
Super. onCreate (savedInstanceState); setContentView (R. layout. array_list_layout );
ListView = (ListView) findViewById (R. id. array_list );
SimpleAdapter simpleAdapter = new SimpleAdapter (SimpleAdapterListDemo. this, getHashMapData (),
Android. R. layout. simple_list_item_2, new String [] {"id", "name"}, new int [] {android. R. id. text1,
Android. R. id. text2}); listView. setAdapter (simpleAdapter );
}
/***
* @ Return Year: 2011 Date: 2011-10-23 Time: 05:45:38 Author: cz todo * write a method to bind ten pieces of data
*/Private ArrayList <HashMap <String, Object> getHashMapData (){
ArrayList <HashMap <String, Object> hashData = new ArrayList <HashMap <String, Object> (); for (int I = 0; I <10; I ++ ){
HashMap <String, Object> mItem = new HashMap <String, Object> (); mItem. put ("id", "current id is" + I );
MItem. put ("name", "name Is Beauty" + I); hashData. add (mItem );
} Return hashData;
}
}

This Adapter uses SimpleAdapter directly. The constructor has five parameters. You can see the following:

Android. widget. SimpleAdapter. SimpleAdapter (Context context, List <? Extends Map <String,?> Data, int resource, String [] from, int [])

The constructor has five parameters,

The first is the reference object of context,
The second parameter is the List of datasets to be displayed,
The third parameter is the int type, which is the custom Layout of each item. In the program, we use the layout of android, so we use android. r. layout. list_item_2 is referenced. If it is a custom layout, R is used directly. layout. XX references.
The fourth parameter must be understood together with the fifth parameter. from to is to display the data in the fourth parameter to the fifth parameter. The second parameter is the dataset we want to display. Each piece of data is defined by hash Map, which contains the key field, id, name, there is such a ing relationship. The key value in the fourth parameter is mapped to the TextView corresponding to the fifth parameter. Then, where does the TextView come from, is the TextView in the layout file of the third parameter, which can be understood as follows.

I hope you can understand it at this time.

The following is an example of custom la S. However, when I wrote the submission for the first time, I indicated that the number of characters was over 80 thousand characters, and I was stuck with something wrong, which made me complain, editor to be improved ..

The following is an example of custom layout. Let's take a look at the program:

The code is:

1. First, customize the layout. We name a m_list_layout.xml layout file. The Code is as follows:

Copy codeThe Code is as follows: <? Xml version = "1.0" encoding = "UTF-8"?> <RelativeLayout xmlns: android = "http://schemas.android.com/apk/res/android"
Android: layout_width = "fill_parent" android: layout_height = "fill_parent"> <ImageView android: id = "@ + id/imageView" android: layout_width = "48dip"
Android: layout_height = "48dip" android: scaleType = "fitCenter" android: adjustViewBounds = "true" android: layout_alignParentLeft = "true"
Android: layout_margin = "5dip"> </ImageView> <TextView android: id = "@ + id/number" android: layout_width = "wrap_content"
Android: layout_height = "wrap_content" android: textSize = "14dip" android: layout_toRightOf = "@ + id/imageView" android: layout_alignTop = "@ + id/imageView"> </TextView>
<TextView android: id = "@ + id/name" android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: textSize = "10dip"
Android: layout_toRightOf = "@ + id/imageView" android: layout_below = "@ + id/number"> </TextView> </RelativeLayout>

2. Code in our program:
Copy codeThe Code is as follows :/***
*/Package com. cz. list. demo;
Import java. util. ArrayList;
Import java. util. HashMap;
Import android. app. Activity; import android. content. Context;
Import android. OS. Bundle; import android. util. Log;
Import android. view. LayoutInflater; import android. view. View;
Import android. view. ViewGroup; import android. widget. ImageView;
Import android. widget. ListView; import android. widget. SimpleAdapter;
Import android. widget. TextView;
/*** @ Author CZ
**/
Public class CustomSimpleAdapterDemo extends Activity {private ListView listView;
@ Override
Protected void onCreate (Bundle savedInstanceState) {// TODO Auto-generated method stub
Super. onCreate (savedInstanceState); setContentView (R. layout. array_list_layout );
ListView = (ListView) findViewById (R. id. array_list );
CustomSimpleAdapter customSimpleAdapter = new CustomSimpleAdapter (CustomSimpleAdapterDemo. this, getHashMapData (),
R. layout. custom_list_layout );
ListView. setAdapter (customSimpleAdapter );}
/**
** @ Author CZ
* Customize the class to inherit SimpleAdapter */
Private class CustomSimpleAdapter extends SimpleAdapter {private Context context;
Private ArrayList <HashMap <String, Object> data; private int layoutResource;
/**
* @ Param context * @ param data
* @ Param resource * @ param from
* @ Param to constructor */
Public CustomSimpleAdapter (Context context, ArrayList <HashMap <String, Object> data, int resource ){
Super (context, data, resource, null, null); this. context = context;
This. data = data; this. layoutResource = resource;
}
Class ViewHolder {ImageView picture;
TextView number; TextView name;
}
/** (Non-Javadoc)
** @ See android. widget. SimpleAdapter # getView (int, android. view. View,
* Android. view. ViewGroup )*/
@ Override public View getView (int position, View convertView, ViewGroup parent ){
LayoutInflater layoutInflater = (LayoutInflater) getSystemService (LAYOUT_INFLATER_SERVICE); View layoutView = layoutInflater. inflate (layoutResource, null );
ViewHolder viewHolder = new ViewHolder (); viewHolder. picture = (ImageView) layoutView
. FindViewById (R. id. imageView); viewHolder. number = (TextView) layoutView. findViewById (R. id. number );
ViewHolder. name = (TextView) layoutView. findViewById (R. id. name );
ViewHolder. picture. setImageResource (Integer. parseInt (data. get (position). get ("imageView"). toString ()));
ViewHolder. number. setText (data. get (position ). get ("id "). toString (); Log. e ("id", data. get (position ). get ("name "). toString ());
ViewHolder. name. setText (data. get (position). get ("name"). toString (); return layoutView;
}}
/**
** @ Return
* Year: 2011 Date: 2011-10-23 Time: 05:46:45 * Author: CZ
* The TODO custom method binds data. To avoid all images being the same, we need to bind three images in a loop. */
Private ArrayList <HashMap <String, Object> getHashMapData () {ArrayList <HashMap <String, Object> hashData = new ArrayList <HashMap <String, Object> ();
For (int I = 0; I <10; I ++) {HashMap <String, Object> mItem = new HashMap <String, Object> ();
MItem. put ("id", "current number:" + I); mItem. put ("name", "name Is Beauty" + I );
Switch (I % 3) {case 0:
MItem. put ("imageView", R. drawable. test1); break;
Case 1: mItem. put ("imageView", R. drawable. test2 );
Break; case 2:
MItem. put ("imageView", R. drawable. test3); break;
Default: mItem. put ("imageView", R. drawable. test4 );
Break ;}
HashData. add (mItem );}
Return hashData;
}
}

With the previous example as the basis, it seems clear at this time that the data to be bound is the same, but the data to images is mostly bound.

Then we overwrite SimpleAdapter, And the constructor defines the parameters by ourselves. It should be noted that:

1) The getView () method returns a View value. After data is bound, the View is returned.

2) The layout we wrote should first be converted into a View, find the corresponding TextView in her, and then retrieve the HashMap at the corresponding position in the List table, the retrieved data is displayed on the corresponding ImageView and TextView.

Hope to help new users ..

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.