Custom simple universal adapter and custom Adapter

Source: Internet
Author: User

Custom simple universal adapter and custom Adapter

Adapters are commonly used in android, such as ListView, GridView, and drop-down boxes... Are common,

This blog introduces the 10 thousand adapter, which is simple but useful. (You do not need to create an adapter one by one)

This column creates a virtual class, which must be inherited and implemented accordingly.

Public abstract class SimpleBaseAdapter <T> extends BaseAdapter {// call the data required for the Context context passed in by the class, Using Generics; List <T> data; // constructor, input context and data public SimpleBaseAdapter (Context context, List <T> data) {this. context = context; this. data = null? New ArrayList <T> (): data ;}@ Override public int getCount () {return data. size () ;}@ Override public Object getItem (int position) {if (position> = data. size () {return null;} return data. get (position) ;}@ Override public long getItemId (int position) {return position ;}// method to be implemented by the inherited class, get the layout id of the list item public abstract int getItemRsouce (); // The method to be implemented by the inherited class, get the view public abstract View getItemView (int position, View convertView, viewHolder holder); // the most important method in the adapter. The reason why the returned view calls getItemView () this method // determines @ SuppressWarnings ("unchecked") @ Override public view getView (int position, view convertView, viewGroup parent) {ViewHolder holder; if (convertView = null) {convertView = View. inflate (context, getItemRsouce (), null); holder = new ViewHolder (convertView); convertView. setTag (holder);} else {holder = (ViewHolder) convertView. getTag () ;}return getItemView (position, convertView, holder) ;}// write a new ViewHolder method, to facilitate // The implementation class to obtain the subview in ConvertView, holderView caching mechanism // use SparseArray to cache public class ViewHolder {private SparseArray <View> views = new SparseArray <View> (); private View convertView; public ViewHolder (View convertView) {this. convertView = convertView;} @ SuppressWarnings ("unchecked") public <T extends View> T getView (int resId) {View v = views. get (resId); if (v = null) {v = convertView. findViewById (resId); views. put (resId, v) ;}return (T) v ;}}}
SimpleBaseAdapter is an abstraction of the adapter, and different manifestations of this abstract class are abstracted.

Implementation, because the implementation class must implement the abstract method (it can also be said that the implementation class shows different attributes according to the abstract method)

Implementation class of SimpleBaseAdapter:

Public class myAdapter extends SimpleBaseAdapter <String> {public myAdapter (Context context, List <String> data) {super (context, data) ;}@ Override public int getItemRsouce () {return R. layout. list_item; // The xml file id of the item in the list of implementation classes returned} // get the view based on the id (the IDs of each implementation class are different) @ Override public View getItemView (final int position, View convertView, ViewHolder holder) {TextView TV = holder. getView (R. id. title); TV. setText (String) getItem (position); Button btn = holder. getView (R. id. btn); btn. setText ("select"); btn. setOnClickListener (new View. onClickListener () {@ Override public void onClick (View v) {Toast. makeText (myAdapter. this. context, "" + position, Toast. LENGTH_SHORT ). show () ;}}); return convertView ;}}


Related Article

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.