First we know that in Android, adapter itself is an interface, he derives a lot of sub-interfaces, these sub-interfaces are implemented by many specific classes, to achieve the specific display effect. This time we mainly introduce the Simpleadapter implementation class.
Simpleadapter class: In fact, Simpleadapter is not simple, and his function is very powerful, you can encapsulate multiple objects of the list collection into lists items.
This is the function that we often need to use. For example: When we display things on the phone, each item will have an ID, title, description, image, and so on attribute, obviously multiple attribute, how we bind the multiple items to display on an item, This is the reason why we should study simpleadapter deeply.
Here's an important constructor for Simpleadapter:
Public constructors PublicSimpleadapter (context context, list<? extends Map<string,?>> data, int resource, string[] from, int[] to)
constructor function
Parameters
Context |
The context of the running view associated with the Simpleadapter. |
Data |
A list of maps. Each entry in the list corresponds to a row. Maps contains all the data specified in the From. |
Resource |
The resource ID of the view layout that defines the list item. The layout file should contain at least the name defined in to. |
From |
A list of column names associated with the items in the Map. |
To |
A list of views used to display the columns in the from parameter. These views should all be of type TextView. The nth view in the list shows the value obtained from the nth column in the parameter from. |
In general, however, we find that the image is inside the drawable or/res/raw folder inside Android's own project, which has been specified at the beginning, When these images are mapped to the R.java file and saved as an int, you can certainly use the int type when calling.
But if we didn't set these image directly at the beginning of the Android project, a clear question arose: because it wasn't set in project beforehand, These image will be automatically imported into the R.java file, of course, it will not be in the type of int saved, then this function how to use.
Let's use Simpleadapter's nested class--Simpleadapter.viewbinder
This class is used by external customers of Simpleadapter to bind the value of the adapter to the view. You can use this class to bind values that are not supported by Simpleadapter to the view, or to change how the views supported by Simpleadapter are bound.
Public methods |
Abstract Boolean |
setviewvalue(view view, Object data, String textrepresentation) binds the specified data to the specified view. |
So here's how we can use this nested class of simpleadapter to implement this particular feature.
public void ShowDialog (context context, final String template, int mode) {arraylist<string> templatelist = Gettempla Telist (template, mode); Layoutinflater Inflater = layoutinflater.from (context); final View dialog = inflater.inflate (r.layout.template, NULL); ListView ListView = (ListView) Dialog.findviewbyid (R.id.mylist); final list<map<string, object>> ListItems = new arraylist<map<string, object>> (); final Alertdialog.builder Builder = new Alertdialog.builder (context ); Mtemplateprocessor processor = new Mtemplateprocessor (); for (int i = 0; i < templatelist.size (); ++i) {try {processor.i NIT (MainActivity.s_Engine.getThemePath () + templatelist.get (i)); catch (Exception e) {//TODO auto-generated catch Blocke.printstacktrace ();} map<string, object> listItem = new hashmap<string, object> (); Listitem.put ("id", Templatelist.get (i)); Listitem.put ("title", Processor.gettitle (Mtemplateprocessor.language_code_english)); Listitem.put ("description", Processor.geTdescription (Mtemplateprocessor.language_code_english)); Mbitmap Mbitmap = Processor.getthumbnail (mainactivity.s_engine,mcolorspace.mpaf_rgb32_b8g8r8a8, +,); if (MBitmap ! = null) {Bitmap Bitmap = Mandroidbitmapfactory.createbitmapfrommbitmap (Mbitmap, false); Listitem.put ("Thumbnail", bitmap); try {processor.freethumbnail (mbitmap);} catch (Exception e) {//TODO auto-generated catch Blocke.printstacktrace ();}} Listitems.add (ListItem); try {processor.uninit ();} catch (Exception e) {//Todo:handle Exception}}simpleadapter Simpleadapter = new Simpleadapter (this, listitems,r.layout.dialog_layout, new string[] {"title", "description", " Thumbnail "}, new int[] {r.id.title,r.id.description, r.id.thumbnail}); <span style=" color: #ff0000; " ><em>simpleadapter.setviewbinder (New Viewbinder () {@Overridepublic Boolean setviewvalue (view view, Object Data, String arg2) {//TODO auto-generated method stubif (View instanceof ImageView) && (data instanceof Bitmap) {ImageView ImageView= (ImageView) view; Bitmap Bitmap = (Bitmap) data;imageview.setimagebitmap (BITMAP); return true;} return false;}}); </em></span>listview.setadapter (Simpleadapter); Listview.setonitemclicklistener (new Onitemclicklistener () {@Overridepublic void Onitemclick (adapterview<?> arg0, View arg1,int position, long Arg3) {/ /TODO auto-generated method stubstring templatestring = MainActivity.s_Engine.getThemePath () + listitems.get (position) . Get ("id"). toString (), if (Template.equals ("transition")) Addtransition (templatestring), if (Template.equals (" Effect ")) AddEffect (templatestring);}); if (template.equals ("transition")) Builder.settitle ("select transition"); if (Template.equals ("effect")) Builder.settitle ("select Effect"); Builder.setview (dialog); Builder.show ();}
The/res/layout/dialog_layout.xml file is as follows:
<?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 " android:o rientation= "Horizontal" > <textview android:id= "@+id/title" android:layout_width= "Wrap_ Content " android:layout_height=" wrap_content " android:textcolor=" #f00 "/> <textview Android:id= "@+id/description" android:layout_width= "wrap_content" android:layout_height= "Wrap_ Content " android:textcolor=" #0f0 "/> <imageview android:id=" @+id/thumbnail " android: Layout_width= "Wrap_content" android:layout_height= "Wrap_content"/></linearlayout>
The/res/layout/template.xml file is as follows:
<?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 " android:o rientation= "Horizontal" > <textview android:id= "@+id/title" android:layout_width= "Wrap_ Content " android:layout_height=" wrap_content " android:textcolor=" #f00 "/> <textview Android:id= "@+id/description" android:layout_width= "wrap_content" android:layout_height= "Wrap_ Content " android:textcolor=" #0f0 "/> <imageview android:id=" @+id/thumbnail " android: Layout_width= "Wrap_content" android:layout_height= "Wrap_content"/></linearlayout>
The above results appear as follows:
The above shows the binding bitmap at run time.
Android Quest: The combination of simpleadapter and bitmap