In many cases, you do not need to implement a adapter, you can directly use Google to provide us with the adapter.
Because adapter is responsible for both providing data and creating a view that represents each entry, all adapter can fundamentally modify the appearance and functionality of the space they are bound to.
The following highlights the two most common and useful OST adapter:
1.arrayadapter:arrayadapter uses generics to bind the adapter view to an array of objects of a specified class. By default, Arrayadapter uses the ToString value of each element of an object array to populate the TextView in the specified layout.
Such as:
public class Mylistview extends Activity { private listview listview; Private list<string> data = new arraylist<string> (); @Override public void OnCreate (Bundle savedinstancestate) { super.oncreate (savedinstancestate); ListView = New ListView (this); Listview.setadapter (New arrayadapter<string> (this, Android. R.layout.simple_expandable_list_item_1,getdata ())); Setcontentview (ListView); } Private List<string> GetData () { list<string> data = new arraylist<string> (); Data.add ("test data 1"); Data.add ("Test Data 2"); Data.add ("test Data 3"); Data.add ("test data 4"); return data;} }
In most cases, custom arrayadapter are required to populate the layout used by each view to represent the underlying array data. To do this, you need to extend arrayadapter with a specific type of variant and override the GetView method to assign object properties to the layout view. Such as:
Package Com.example.myarrayadapter;import Java.util.list;import Android.content.context;import Android.view.layoutinflater;import Android.view.view;import Android.view.viewgroup;import Android.widget.arrayadapter;import Android.widget.linearlayout;public class Myarrayadapter extends ArrayAdapter< User>{int Resource;public Myarrayadapter (context context, int resource,list<user> objects) {Super (context, Resource, objects); this.resource = resource;} @Overridepublic view GetView (int position, view Convertview, ViewGroup parent) {//Create and populate the view to be displayed LinearLayout newview;if ( Convertview = = null) {//If it is not an update, a new view is populated newview = new LinearLayout (GetContext ()); String inflater = Context.layout_inflater_service; Layoutinflater Li;li = (layoutinflater) getcontext (). Getsystemservice (Inflater); Li.inflate (resource, newView,true);} else{//Otherwise update the existing view Newview = (linearlayout) Convertview;} User user = GetItem (position); return Super.getview (position, convertview, parent);}}
The GetView method is used to construct, populate the view that will be added to the parent Adapterview class (such as a ListView), which is bound to the underlying array using this adapter by the parent Adatperview class.
To apply a adapter to a class derived from Adapterview, you can invoke the Setadapter method of the view and pass it to a adapter instance:
arraylist<string> myStringArray = new arraylist<string> (); int layoutid = Android. R.layout.simple_list_item_1; arrayadapter<string> myadapterinstance;myadapterinstance = new arrayadapter<string> (GetContext (), Layoutid,mystringarray); Mylistview.setadapter (myadapterinstance);