Shortly after getting started with Android, I found two ways to use listview. If not, please correct me.
As an important display control, listview needs to dynamically load items in many scenarios. Three requirements are selected: Click item event and flexible layout.
Directly inherit the baseadapter class, override the getcount (), getitem (INT position), getitemid (INT position), getview (INT position, view convertview, viewgroup parent) methods, if the data changes or a new item needs to be dynamically loaded, call the notifydatasetchanged method. The sample code is as follows:
Public class userlistadapter
Extends baseadapter {
Private arraylist <userinfo>
Userlist;
Private Context
Mcontext;
Public userlistadapter (context, arraylist <userinfo> userlist ){
Mcontext = context;
This. userlist = userlist;
}
Public
Int getcount (){
Return
Userlist. Size ();
}
Public object getitem (INT position ){
Return position;
}
Public
Long getitemid (INT position ){
Return position;
}
Public View getview (INT position, view convertview, viewgroup parent ){
Useritemview userview;
Userinfo user =
Userlist. Get (position );
If (convertview =
Null)
Userview =
New useritemview (mcontext, user );
Else
Userview = (useritemview) convertview;
Return userview;
}
}
In the above code, the implementation of useritemview (mcontext, user) is the focus of this article.
Method 1: useritemview inherits the linearlayout class (or other layout classes such as relativelayout) and integrates the setonclicklistener method in the initialization function to respond to click events; load new controls and positional relationships in the code to implement layout. The code example is as follows:
Public class useritemview
Extends linearlayout {
Private imageview
Userimageview;
Private textview
Usernameview;
Private userinfo
User;
Private Context
Mcontext;
Private linearlayout
Firstlayout;
Public useritemview (context,
Final userinfo user ){
Super (context );
Mcontext = context;
This. User = user;
Setlayout (context );
This. setonclicklistener (New onclicklistener (){
Public
Void onclick (view arg0 ){
// Respond to click events
}
});
}
Private
Void setlayout (context ){
Firstlayout =
New linearlayout (context );
Firstlayout. setorientation (linearlayout. Vertical );
Userimageview = getusericon (user. GETID (),
User. geticonid ());
Firstlayout. addview (userimageview );
Usernameview =
New textview (context );
Usernameview. settext (user. getname ());
Firstlayout. addview (usernameview,
New linearlayout. layoutparams (
Layoutparams. wrap_content, layoutparams. wrap_content ));
Addview (firstlayout,
New linearlayout. layoutparams (
Layoutparams. wrap_content, layoutparams. wrap_content ));
}
}
The above method is to use the code method to implement layout, which is complicated and reusable, so the following introduces another method:
Directly let useritemview inherit the onitemclicklistener method to respond to click events and add a layout file to implement layout.
The final code looks like this:
Call userlistadapter in the project:
If (userlistadapter =
Null ){
Userlistadapter =
New userlistadapter (slimtasklistactivity. This,
Muserlist );
Userlistview. setadapter (userlistadapter );
// Userlistview is a control bound to listview.
Userlistview. setonitemclicklistener (userlistadapter );
// Bind a click event
}
Tasklistadapter. notifydatasetchanged (); // The list will be updated based on the content in the muserlist.
The specific implementation of the userlistadapter class is as follows:
Public class userlistadapter
Extends baseadapter implements onitemclicklistener {
Private arraylist <userinfo>
Userlist; // the data content to be displayed in listview
Private layoutinflater
Inflater; // used to load the layout File
Public userlistadapter (context, arraylist <userinfo> userlist ){
This. Inflater = layoutinflater. From (context );
This. userlist = userlist;
}
Public
Int getcount (){
Return
Userlist. Size ();
}
Public object getitem (INT position ){
Return position;
}
Public
Long getitemid (INT position ){
Return position;
}
Public View getview (INT position, view convertview, viewgroup parent ){
Convertview =
Inflater. Inflate (R. layout. user_list_item_view,
Null );
Settaskitemcontent (convertview,
Muserlist. Get (position ));
Return convertview;
}
Private
Void settaskitemcontent (view convertview, userinfo user ){
(Textview) convertview. findviewbyid (R. Id. user_name). settext (user. getname ());
}
@ Override
Public
Void onitemclick (adapterview <?> Arg0, view arg1,
Int position, long arg3 ){
}
}