Android learning notes: Custom common ListView/GridView to implement the ListAdapter class

Source: Internet
Author: User

In android learning, blood is often used for medicine to ListView, and you also need to customize View display to implement ListAdapter interface

Therefore, a common CommonAdapter class is written to simplify the implementation of ListAdapter. extract the parts that Adaapter dynamically implements Based on layout. interface. the static part is made into a common method, and later development will focus only on the binding of the layout part. simplified development.

CommonAdapter class

View Code public class CommonAdapter extends BaseAdapter {

Private int customXmlName;
Protected ViewHolderModel Holder = null;
Protected final List <Map <String, Object> list;
Private final Context context;

/*
* Constructor setting Context and Data Source
*/
Public CommonAdapter (ViewHolderModel vModel ){

CustomXmlName = com.gz h. search. R. layout. list_item;
This. context = vModel. context;
This. list = vModel. list;
This. Holder = vModel;
}

@ Override
Public int getCount (){
Return list. size ();
}

Public void setLayout_Name (int name ){
If (name <= 0 ){
Return;
}
CustomXmlName = name;
}

@ Override
Public Object getItem (int position ){
If (position <list. size ()){
Return list. get (position );
} Else {

Return null;
}
}

@ Override
Public long getItemId (int position ){

Return position;
}

Public void HolderModel (ViewHolderModel vModel ){
This. Holder = vModel;
}

@ Override
Public View getView (int position, View convertView, ViewGroup parent ){

If (convertView = null ){
ConvertView = LayoutInflater. from (context). inflate (customXmlName,
Null );
Holder. InitViewHoler (convertView );
ConvertView. setTag (Holder );

} Else {
Holder = (ViewHolderModel) convertView. getTag ();
}
Holder. SetViewHolerValues (convertView, position );
Return convertView;
}

}

CustomXmlName = com.gz h. search. R. layout. list_item: indicates the default layout.

Java Holder abstract base class corresponding to ViewHolderModel layout

 

View Code/* initialize ItemView
* Set View
*/
Interface IViewHolderController {
/*
* Initialization
*/
Void InitViewHoler (View convertView );

/*
* Set the display content
*/
Void SetViewHolerValues (View convertView, int position );
}

Public abstract class ViewHolderModel implements IViewHolderController {
Public ViewHolderModel Holder = null;
Protected List <Map <String, Object> list;
Protected Context context;

Public ViewHolderModel (Context context, List <Map <String, Object> list ){
This. context = context;
This. list = list;

}
}

You only need to implement the abstract class ViewHolderModel to complete the development of the custom Adapter.

Example:

Implementation: ViewHolderModel base class

 

 

Public class HolderView extends ViewHolderModel {

Private ImageView itemLeftImage;
Private TextView itemText;
Private ImageView itemRightImage;
Private Button itemButton;

Public HolderView (Context context, List <Map <String, Object> list ){
Super (context, list );

// TODO Auto-generated constructor stub
}

@ Override
Public void InitViewHoler (View convertView ){
HolderView viewHolder = (HolderView) Holder;
ViewHolder. itemLeftImage = (ImageView) convertView
. FindViewById (R. id. item_left );
ViewHolder. itemText = (TextView) convertView
. FindViewById (R. id. item_text );
ViewHolder. itemRightImage = (ImageView) convertView
. FindViewById (R. id. item_right );
ViewHolder. itemButton = (Button) convertView. findViewById (R. id. btnOK );
}

/*
* (Non-Javadoc)
*
* @ See
* Com.gz h. common. IViewHolderController # SetViewHolerValues (android. view.
* View, int)
*/
@ Override
Public void SetViewHolerValues (View convertView, int position ){
HolderView viewHolder = (HolderView) Holder;
Map <String, Object> item = list. get (position );

ViewHolder. itemLeftImage. setImageResource (Integer. parseInt (item. get (
"Left"). toString ()));
ViewHolder. itemText. setText (item. get ("Text"). toString ());
ViewHolder. itemRightImage. setImageResource (Integer. parseInt (item. get (
"Right"). toString ()));
ViewHolder. itemButton. setTag (position );

}
}

 

 

 

 

 

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.