Android simple adapter Encapsulation

Source: Internet
Author: User

Android simple adapter Encapsulation

In development, listview is a control that every project must use. BaseAdapter is required when listview is used. Generally, some reusable items are extracted when the boss builds a framework, it is convenient for every developer to use and highly reusable, so it must be well encapsulated. Today, we simply write what we usually use and encapsulate the BaseAdapter,

MyBaseAdapter. java

public abstract class MyBaseAdapter
 
   extends BaseAdapter {private List
  
    datas;private Context context;public MyBaseAdapter(List
   
     datas, Context context) {super();this.datas = datas;this.context = context;}@Overridepublic int getCount() {return datas.size();}@Overridepublic Object getItem(int position) {return datas.get(position);}@Overridepublic long getItemId(int position) {return position;}@Overridepublic abstract View getView(int position, View arg1, ViewGroup arg2);}
   
  
 

In fact, when we use the adapter, the return values of the other three methods are the same, so this can be processed well in the parent class. The only thing that every subclass does not know is getView () therefore, you only need to let each subclass implement its own getView () method, so it is extracted into the above MyBaseAdapter class. Now, write a simple and practical framework for this class.

MainActivity. java

Public class MainActivity extends Activity {private ListView listview; private ArrayList
 
  
Datas; private LayoutInflater inflater; private MyAdapter adapter; @ Overrideprotected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); listview = (ListView) findViewById (R. id. listview); inflater = LayoutInflater. from (this); initData (); adapter = new MyAdapter (datas, this); listview. setAdapter (adapter);} private void initData () {datas = new ArrayList
  
   
(); For (int I = 0; I <100; I ++) {datas. add ("test -------") ;}} class MyAdapter extends MyBaseAdapter
   
    
{Public MyAdapter (List
    
     
Datas, Context context) {super (datas, context) ;}@ Overridepublic View getView (int position, View converView, ViewGroup arg2) {ViewHolder holder = null; if (converView = null) {converView = inflater. inflate (R. layout. item, null); holder = new ViewHolder (); holder. tvContent = (TextView) converView. findViewById (R. id. tvContent); converView. setTag (holder);} else {holder = (ViewHolder) converView. getTag ();} holder. tvContent. setText (datas. get (position); return converView;} class ViewHolder {TextView tvContent ;}}}
    
   
  
 

In fact, it depends on the open-source framework or the framework in your own project. Generally, the framework is used, and the interfaces, abstract classes, interface callbacks, generics, inheritance, and internal classes are written several times, in fact, the framework is not that difficult.

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.