Explain the use of Simpleadapter simple adapters in Android applications _android

Source: Internet
Author: User

Simpleadapter, like the name, a simple adapter, simple enough to be designed to do simple applications, such as the binding of static data, but still have a custom space, For example, add a button to each ListItem and a response event. First of all, let's look at the definition of simpleadapter, directly translate the SDK Doc:

This is a simple adapter that maps static data to a defined view in an XML file. You can specify the data of a list (such as ArrayList) that consists of a map. Each entry in the ArrayList corresponds to a row in the list. Maps contains data for each row. You can specify an XML layout to specify a view of each row, depending on the data map keyword in the map to the specified view. Binding data to a view is divided into two phases, first, if the Simpleadapter.viewbinder is set, then the Viewbinder setviewvalue (Android.view.View, Object, String) of this setting. will be invoked. If the return value of Setviewvalue is true, the binding is complete and the system default binding implementation will no longer be invoked. If the return value is false, the view binds the data in the following order:
If view implements checkable (such as a checkbox), the expected bound value is a Boolean type.
TextView. The expected bound value is a string type, bound by calling Setviewtext (TextView, String).
ImageView, the expected bound value is a resource ID or a string that binds the data by calling Setviewimage (ImageView, int) or Setviewimage (ImageView, String).
IllegalStateException will be thrown if no suitable binding occurs.

Let's take a look at the constructor:

Copy Code code as follows:

Public Simpleadapter (context, list<? extends Map<string,?>> data, int resource, string[] from, int[] To)

Simpleadapter basically acknowledges the meaning of its parameters, which is much simpler to use.

Simpleadapter's parameter description:

    • The first parameter represents access to the entire Android application interface, and basically all components require
    • The second parameter represents the generation of a map (String, Object) list option
    • The third parameter represents the ID of the interface layout that represents the file as a component of the list item
    • The fourth parameter represents which key of the map object corresponds to value to generate the list item
    • The fifth parameter indicates that the resources of the component Map object key that are populated are filled with the corresponding relationship
    • Note that the map object can be found without the key, but the component must have a resource fill because the key will return NULL if it is not found. Actually, it's equivalent to giving a null resource

In the following program, if new string[] {"Name", "Head", "desc", "name"} new int[] {r.id.name,r.id.head,r.id.desc,r.id.head}

The head component will be overwritten by the name resource

Sample code

 <linearlayout xmlns:android= "Http://schemas.android.com/apk/res/android" xmlns: tools= "Http://schemas.android.com/tools" android:layout_width= "match_parent" android:layout_height= "Match_parent" "Android:orientation=" Horizontal "tools:context=". Mainactivity "> <listview android:id=" @+id/lt1 "android:layout_width=" Match_parent "Android:layo" ut_height= "Wrap_content" > </ListView> </LinearLayout> 
<?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:orientation=" Horizontal "> <imageview android:id=" @+id/head "android:layout_width=" Wrap_content "android:layout_height= "Wrap_content" android:paddingleft= "10DP"/> <linearlayout android:layout_width= "Match_parent" a ndroid:layout_height= "wrap_content" android:orientation= "vertical" > <textview android:id= "@ +id/name "android:layout_width=" wrap_content "android:layout_height=" Wrap_content "android:textSize=" 20DP "android:textcolor=" #f0f "android:paddingleft=" 10DP "/> <textview and Roid:id= "@+id/desc" android:layout_width= "wrap_content" android:layout_height= "Wrap_content" Android: Textsize= "14DP" android:paddingleft= "10DP"/> </LinearLayout> </LinearLayout> 
 


Package com.example.simpleadptertest; 
Import java.util.ArrayList; 
Import Java.util.HashMap; 
Import java.util.List; 
 
Import Java.util.Map; 
Import android.app.Activity; 
Import Android.os.Bundle; 
Import Android.view.Menu; 
Import Android.widget.ListView; 
 
Import Android.widget.SimpleAdapter; 
 
  public class Mainactivity extends activity {private string[] name = {"Sword Xiao Dance Butterfly", "John", "Hello", "poetic"}; Private string[] desc = {"Magic domain Player", "Hundred Execution", "Senior Rich Generation", "Sister please come over." A girl who is good at running her sister. 
 
  " }; 
   
  Private int[] Imageids = {R.drawable.libai, R.drawable.nongyu, R.drawable.qingzhao, R.drawable.tiger}; 
 
  Private ListView LT1; 
    @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); 
    Setcontentview (R.layout.activity_main); 
    list<map<string, object>> listems = new arraylist<map<string, object>> (); for (int i = 0; i < name.length i++) {map<string, object> listem = new HashMap<string, object> (); 
      Listem.put ("Head", Imageids[i]); 
      Listem.put ("name", Name[i]); 
      Listem.put ("desc", Desc[i]); 
    Listems.add (Listem);  
     Parameter description of/*simpleadapter * The first parameter represents access to the entire Android application interface, and basically all components need * The second parameter represents generating a map (String, Object) list option * The third parameter indicates the ID of the interface layout representing the file as the component of the list item * The fourth parameter indicates which key of the map object corresponds to the value to generate the resources of the component Map object key that is populated by the list item * Fifth parameter represents the resource in which to populate the component The order has a corresponding relationship * Note that the Map object can not be found by the key but the component must have a resource fill because the key will return NULL if it is not found the equivalent to a null resource * in the following program if new string[] {"Name" 
    , "Head", "desc", "name"} new int[] {r.id.name,r.id.head,r.id.desc,r.id.head} * The component of this head will be covered by the name resource * */ Simpleadapter simplead = new Simpleadapter (this, Listems, R.layout.simple_item, new string[] {"Name", "Head", "D 
     
    ESC "}, new int[] {r.id.name,r.id.head,r.id.desc}); 
    lt1= (ListView) Findviewbyid (R.ID.LT1); 
     
  Lt1.setadapter (Simplead); 
@Override public boolean Oncreateoptionsmenu (Menu menu) {    Inflate the menu; 
    This adds items to the action bar if it is present. 
    Getmenuinflater (). Inflate (R.menu.main, menu); 
  return true; 
 } 
 
}

Related Article

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.