ListView Adapter Use (bind data) to customize the layout of each item to bind the data _android

Source: Internet
Author: User
Tags stub
Let's take a look at the first example showing:

This interface is believed to have been seen, this time more than the previous example is ListView each of the binding is no longer a simple string, ListView each entry we need to display two information, numbers and names. As you can see, we've bound 10 pieces of data and used a For loop, Each binding creates a hashmap, then put the data inside, put the data has a corresponding key, that is, Id,name, when we pick up the data on the basis of this key to find we put in the data, a bit similar to the meaning of the key value pairs, do not know this explanation you do not understand.

Post the source code and then explain:

Copy Code code 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: Afternoon 05:45:38 Author:cz TODO * Write a method to bind 10 data
* Private arraylistarraylisthashmap<string, object> mitem = new hashmap<string, object> (); Mitem.put ("id", "Current ID is" + i);
Mitem.put ("name", "name is Belle" + i); Hashdata.add (Mitem);
return hashdata;
}
}

This adapter is directly simpleadapter, with five parameters in the constructor, you can look at the following:

Android.widget.SimpleAdapter.SimpleAdapter (context, list<? extends Map<string,?>> data, int Resource, string[] from, int[] to)

There are five parameters in the constructor,

The first one is the context reference object,
The second argument is the list of datasets we want to display,
The third parameter is type int, which is a custom layout for each of our programs, where we use the Android layout, so we'll use Android. R.layout.list_item_2 to quote, if it is our custom layout, directly with r.layout.xx reference.
The fourth parameter needs to be understood together with the fifth parameter, which is understood from the literal meaning, 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 of which is defined with a hash map, with a defined field of key, Id,name, a relationship with such a mapping, the key value in the fourth parameter is mapped to the corresponding TextView in the fifth parameter, So where do these textview come from, the TextView in the layout file in the third argument, as you can see in these four parameters.

I hope you can understand this at this time.

Next is the example of a custom layout, but because the first time I finished writing the prompt character more than 80,000, sticky paste wrong things, so I can not help but complain in the heart, the editor to improve Ah ...

Next is the custom layout example, we first look at the program out of the effect of the picture:

The code is:

1. First customize the layout, we name a custom_list_layout.xml layout file, the code is as follows:

Copy Code code 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 Code code 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
* Custom class to inherit Simpleadapter * *
Private class Customsimpleadapter extends Simpleadapter {private context;
Private arraylist/**
* @param context * @param data
* @param resource * @param from
* @param to Constructor * *
Public Customsimpleadapter (context, arraylistSuper (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: Afternoon 05:46:45 * Author:cz
* TODO Custom method binding data, in order to avoid all the pictures are the same, we let three pictures loop bound. */
Private arraylistfor (int i = 0; i < i++) {hashmap<string, object> mitem = new hashmap<string, object> ();
Mitem.put ("id", "Current number is:" + i); Mitem.put ("name", "name is Belle" + 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 to do the basis, this time it will seem to be clearer, binding data is the same, but is more bound to the image of the data.

And then we rewrite the Simpleadapter, the constructor, we define the parameters ourselves, and we need to say:

1) GetView () method Return Yes is a view value, after we bind the data, we return this view

2 We write the layout, first through transformation, into a view, find her corresponding textview, and then take out the list table in the relative position of HashMap, remove the data displayed to the corresponding ImageView and TextView above.

Hope to help the novice.

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.