Adapter is a bridge class that binds data to a user interface view and is responsible for creating a child view that represents each entry in the parent view and provides access to the underlying data.
Public classMainactivityextendsActivity {@Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); Setcontentview (R.layout.activity_main); ListView Todolistview=(ListView) Findviewbyid (R.id.todolistview); FinalArraylist<string> Todoitems =NewArraylist<string>();//Final Arrayadapter<string> adapter = new Arrayadapter<string> (this, Android. R.layout.simple_list_item_1, todoitems); intResID =R.layout.todolist_item; Finalarrayadapter<string> adapter =NewArrayadapter<string> ( This, ResID, Todoitems); //bind the adapter to the ListViewTodolistview.setadapter (adapter); } }
//if an array of complex objects, the display of the data needs to be customized, then rewrite the adapter Geview methodAbstract Public classMylistadapter<t>extendsBaseadapter {Private intListcellid; PublicMylistadapter (Context context,intResId, list<t>items) { Super(context, resId, items); Listcellid=resId; } @Override PublicView GetView (intposition, View Convertview, ViewGroup parent) { if(Convertview = =NULL) {Convertview= Layoutinflater.from (GetContext ()). Inflate (Listcellid,NULL); } ((TextView) (Convertview)). SetText ((String) GetItem (position)); returnConvertview; }}
iOS TableView use
@implementationContactsviewcontroller- (ID) init{if(self =[Super Init]) {contacttable=[[UITableView alloc] InitWithFrame:self.view.bounds Style:uitableviewstyleplain]; Contacttable.frame= CGRectMake (0,0, Self.view.bounds.size.width, self.view.bounds.size.height); Contacttable.Delegate=Self ; Contacttable.datasource=Self ; [Self.view addsubview:contacttable]; } returnSelf ;}-(UITableViewCell *) TableView: (UITableView *) TableView Cellforrowatindexpath: (Nsindexpath *) indexpath{StaticNSString *identifier =@"Cell"; UITableViewCell*cell =[TableView Dequeuereusablecellwithidentifier:identifier]; if(!cell) {Cell=[[UITableViewCell alloc] Initwithstyle:uitableviewcellstyledefault reuseidentifier:identifier]; } Cell.textLabel.text=[[_contactlist ObjectAtIndex:indexPath.row] objectforkey:personname]; Cell.detailTextLabel.text=[[_contactlist ObjectAtIndex:indexPath.row] Objectforkey:persontel]; returncell;}-(Nsinteger) TableView: (UITableView *) TableView numberofrowsinsection: (nsinteger) section{NSLog (@"%d", _contactlist.count); return_contactlist.count;}
The difference between iOS and Android table usage:
iOS defines an array member variable in Viewcontroller, sets Viewcontroller to TableView DataSource object, and then creates or re-uses the UI in Cellforrowatindexpath. The array member variables are updated to the UI after reading the corresponding data based on the row location.
Android defines an array variable in activity to hold data, creating a adapter using the array, the context of the activity, the style (XML) Resources of the cell (child view), Set the adapter to the adapter property of the ListView, then create the UI from the style resource ID in the adapter GetView method, and use the position and GetItem methods to read out and update the object of the underlying array's corresponding row to the UI.
Adapter is the equivalent of Tableviewcontroller,listview or tableview some key callbacks are implemented in them, but some tableviewcontroller integrated viewcontroller methods, There is no way to integrate activity in adapter, or it is done by the activity in which the ListView is located (the adapter and activity itself classes are inconsistent).
That is, adapter only completes callbacks related to the ListView.
Android-Fit Adapter