What does Adapter mean?

Source: Internet
Author: User

What does Adapter mean?

AdapterIt is an adapter interface connecting back-end data and front-end display, and an important link between data and UI. Adapter is required in common View and other places

ListView:

Here we will first introduce two basic types:
Public ArrayAdapter (Context context, int resourceId, String []/List <String> objects)

Here, the three parameters are Content: the content of the context
ResourceId: the Id of the called view, which is used to display the style View of the item. You can use the system or custom view.
String []/List <String>: indicates the data source, which can be an array or a collection.

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

Context: context, such as this. Associate the view context running by SimpleAdapter

Parameter data: Map list, the data to be displayed in the list, which needs to be implemented by yourself. For example, in the example, getData (), the type must be consistent with the above, each project must be consistent with the specified entry in from.
Parameter resource: the Id of the individual layout file of ListView. This layout is your custom layout. The layout of what you want to display is in this layout. The Control id defined in to must be included in the layout.
Parameter from: a list of columns added to Map associated with the names of each project column. The array contains the Map key.
Parameter to: an int array. The id in the array is the id of each control in the custom layout.

Custom adapter
Here is a brief introduction to custom SimpleAdapter2016-01-07
Here is an example:

1 package org. mobile. tral. example. listview_baseadpter; 2 3 import java. util. arrayList; 4 import java. util. hashMap; 5 import java. util. list; 6 import java. util. map; 7 8 import android. app. activity; 9 import android. OS. bundle; 10 import android. widget. listView; 11 import android. widget. textView; 12 13 public class MainActivity extends Activity {14 private ListView listView; 15 private TextView textView; 16 private List <Map <String, Object> list = null; 17 18 @ Override19 protected void onCreate (Bundle savedInstanceState) {20 super. onCreate (savedInstanceState); 21 setContentView (R. layout. activity_main); 22 getLayoutInflater (); 23 // initialization data 24 initData (); 25 initViews (); 26} 27 28 private void initViews () {29 // TODO Auto-generated method stub30 listView = (ListView) findViewById (R. id. listView_Main); 31 textView = (TextView) findViewById (R. id. textView_Main); 32 listView. setEmptyView (textView); 33 MyAdapter adapter = new MyAdapter (this, list, 34 R. layout. item_listview_main, new String [] {"userimage", 35 "username", "usernum"}, new int [] {R. id. userimage, 36 R. id. textview, R. id. numview}); 37 38 listView. setAdapter (adapter); 39} 40 41 private void initData () {42 // TODO Auto-generated method stub43 list = new ArrayList <Map <String, Object> (); 44 for (int I = 1; I <11; I ++) {45 Map <String, Object> map = new HashMap <String, Object> (); 46 map. put ("userimage", R. drawable. ic_launcher); 47 map. put ("username", "I am" + I); 48 map. put ("usernum", I); 49 list. add (map); 50} 51} 52}
MyActivity
1 package org. mobile. tral. example. listview_baseadpter; 2 3 import java. util. list; 4 import java. util. map; 5 6 import android. app. activity; 7 import android. view. view; 8 import android. view. viewGroup; 9 import android. widget. baseAdapter; 10 import android. widget. imageView; 11 import android. widget. textView; 12 13/** 14*15 * @ author Earn implement custom adapter16 * 17 */18 public class MyAdapter extends BaseAdapter {19 20 private List <Map <String, object> list = null; 21 22 private Activity activity; 23 private int resouseId; 24 private String form [] = null; 25 private int [] to = null; 26 27 public MyAdapter (Activity activity, List <Map <String, Object> list, 28 int resouseId, String [] form, int [] to) {29 super (); 30 this. list = list; 31 this. activity = activity; 32 this. resouseId = resouseId; 33 this. form = form; 34 this. to = to; 35} 36 37/** 38 * return the number of items in the ListView, data source length 39 */40 @ Override41 public int getCount () {42 // TODO Auto-generated method stub43 return this. list. size (); 44} 45 46/** 47 * returns the mapped data in each Item, that is, the position of the data source element: is to click the position of the Item 48 */49 @ Override50 public Object getItem (int position) {51 // TODO Auto-generated method stub52 return list. get (position); 53} 54 55 @ Override56 public long getItemId (int position) {57 // TODO Auto-generated method stub58 return position; 59} 60 61/** 62 * return the View parent of each Item View: the parent View of the Item 63 */64 @ Override65 public View getView (int position, View convertView, ViewGroup parent) {66 // TODO Auto-generated method stub67 // 68 View = activity. getLayoutInflater (). inflate (resouseId, parent, 69 false); 70 71 ImageView imageView = (ImageView) view. findViewById (to [0]); 72 TextView nameView = (TextView) view. findViewById (to [1]); 73 TextView numView = (TextView) view. findViewById (to [2]); 74 75 int resId = (Integer) (list. get (position ). get (form [0]); 76 imageView. setImageResource (resId); 77 nameView. setText (String) (list. get (position ). get (form [1]); 78 numView. setText (list. get (position ). get (form [2]) + ""); 79 return view; 80} 81}

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.