Because often forget, summarize the various adapter that I have learned before.
1. Arrayadapter
constructor function
Public Arrayadapter (context context, int resource, t[] objects)
Parameters
Context: Contexts.
Resource: Resource ID, which contains a TextView component (the resource ID for a layout file containing a TextView-use time instantiating views) .
Objects: The content to render on the ListView (the objects to represent in the ListView).
Public class arrayadaptertest extends Activity { @Override protected void onCreate(Bundle savedinstancestate) {//TODO auto-generated method stub Super. OnCreate (Savedinstancestate); Setcontentview (R.layout.array_adapter); Toast.maketext ( This,"Button1 pressed!",0). Show (); ListView ls= (ListView) This. Findviewbyid (R.id.listview1); String arr[]=Newstring[]{"Hello","World","Arrayadapter"}; Arrayadapter adapter=NewArrayadapter ( This, R.layout.array_adapter_item,arr); Ls.setadapter (adapter); }
2. Simpleadapter
public SimpleAdapter (ContextcontextMap<StringStringto)
Parameters
Context : The context where the View associated with this
Simpleadapter is running
data : A List of Maps. Each entry in the list corresponds to one row in the list. The Maps contain the data for each row, and should include all the entries specified in ' from '
Resource: Resource identifier of a view layout that defines the views
For this list item. The layout file should include at least those
Named views defined in "to"
from: A List of column names, that'll be added to the MAP associated with each item.
to: The should display column in the ' from ' parameter. These should all is textviews. The first n views in this list is given the values of the first n columns in the from parameter.
for (int i=0; i<names.length;i++) {Map<string,object> map=new hashmap<string, object> ();Map. Put("Name", Names[i]);Map. Put("desc", Desc[i]);Map. Put("Image", Imageids[i]);ListItem. Add(map);}/* The content in the from parameter is Key.set () in map */Simpleadapter sa=new Simpleadapter (This, ListItem, R. Layout. Simple_adapter_item, New string[]{"Name","desc","Image"}, New Int[]{r. ID. NameR. ID. DescR. ID. ImageView1});Ls. Setadapter(SA);
3. Custom Adapter
A brief description of what is the original reason:
(0) butterknifed Framework : The basic knowledge about Butterknife can refer to my previous blog, which is equivalent to supplementing its application in Viewholder
http://blog.csdn.net/csp277/article/details/46787691
(1)Settag function : In order to pass the holder reference, the implementation of the cache after the trial holder to avoid each page of each item is redefined holder and get the corresponding physical components.
Implementation Principle :
A.java If a real reference exists in an object, the JVM cannot reclaim the object
B.listview has a loop mechanism, only the mobile phone display can be presented (containing) the item (that is, the first page of items) need to create a new view, in order to achieve efficiency, when the next stroke, Android reused the previously built view (Convertview), Which means no more new.
C. Combined, when the ListView downward slide, the system is reused before the convertview generated, which can be carried by the tag, that is, holder object to get out, to avoid the new object again, repeatedly findviewbyid and so on.
Reference:
Http://stackoverflow.com/questions/11945563/how-listviews-recycling-mechanism-works
Http://stackoverflow.com/questions/25966689/what-is-the-working-of-settag-and-gettag-in-viewholder-pattern
Public class myadapter extends baseadapter{ PrivateLayoutinflater Minflater; Public Myadapter(Context context) { This. Minflater=layoutinflater.from (context); }@Override Public int GetCount() {//TODO auto-generated method stub returnSimpleAdapterTest.names.length; }@Override PublicObjectGetItem(intPosition) {//TODO auto-generated method stub return NULL; }@Override Public Long Getitemid(intPosition) {//TODO auto-generated method stub returnPosition }@Override PublicViewGetView(intPosition, View Convertview, ViewGroup parent) {//TODO auto-generated method stubViewholder holder=NULL;if(convertview==NULL) {Convertview=minflater.inflate (R.layout.my_adapter_item,NULL); Holder=NewViewholder (Convertview);the function of//settag is to use the holder reference, to implement the cache after the trial holder //Avoid redefining holder and getting corresponding physical components for each item on each page //principle: 1.java If an object has a real reference, the JVM cannot reclaim the object //2.listview has a looping mechanism that only the first mobile phone display can present the item //need to create a new view, in order to achieve efficiency, Android reused the previously built view (Convertview) //3. Together, when the ListView is sliding downward, the convertview generated before the system is reused //So can carry the tag, that is, holder object to get out, to avoid the new object again, repeated Findviewbyid and other operationsConvertview.settag (holder); }Else{holder= (Viewholder) Convertview.gettag (); } holder.myNameView.setText (Simpleadaptertest.names[position]); Holder.myDescView.setText (Simpleadaptertest.desc[position]); Holder.imageView.setImageResource (Simpleadaptertest.imageids[position]); Holder.myButton.setText ("bn["+ (position+1)+"]");returnConvertview; } Public Final class viewholder{ @InjectView(R.id.my_imageview) ImageView ImageView;@InjectView(R.id.my_name) TextView Mynameview;@InjectView(R.ID.MY_DESC) TextView Mydescview;@InjectView(R.ID.MY_BN) Button MyButton; Public Viewholder(View view) {Butterknife.inject ( This, view); } }}
4. Running
Here's a rough look at the rough effect of the demo you wrote.
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Arrayadapter, Simpleadapter, Baseadapter summary in Android