Android Part native Adapter introduction of Arrayadapter

Source: Internet
Author: User

In many cases, you do not need to implement a adapter, you can directly use Google to provide us with the adapter.

Because adapter is responsible for both providing data and creating a view that represents each entry, all adapter can fundamentally modify the appearance and functionality of the space they are bound to.

The following highlights the two most common and useful OST adapter:

1.arrayadapter:arrayadapter uses generics to bind the adapter view to an array of objects of a specified class. By default, Arrayadapter uses the ToString value of each element of an object array to populate the TextView in the specified layout.

Such as:

public class Mylistview extends Activity {     private listview listview;    Private list<string> data = new arraylist<string> ();    @Override public    void OnCreate (Bundle savedinstancestate) {        super.oncreate (savedinstancestate);                 ListView = New ListView (this);        Listview.setadapter (New arrayadapter<string> (this, Android. R.layout.simple_expandable_list_item_1,getdata ()));        Setcontentview (ListView);    }                   Private List<string> GetData () {                 list<string> data = new arraylist<string> ();        Data.add ("test data 1");        Data.add ("Test Data 2");        Data.add ("test Data 3");        Data.add ("test data 4");                 return data;}    }

In most cases, custom arrayadapter are required to populate the layout used by each view to represent the underlying array data. To do this, you need to extend arrayadapter with a specific type of variant and override the GetView method to assign object properties to the layout view. Such as:

Package Com.example.myarrayadapter;import Java.util.list;import Android.content.context;import Android.view.layoutinflater;import Android.view.view;import Android.view.viewgroup;import Android.widget.arrayadapter;import Android.widget.linearlayout;public class Myarrayadapter extends ArrayAdapter< User>{int Resource;public Myarrayadapter (context context, int resource,list<user> objects) {Super (context, Resource, objects); this.resource = resource;} @Overridepublic view GetView (int position, view Convertview, ViewGroup parent) {//Create and populate the view to be displayed LinearLayout newview;if ( Convertview = = null) {//If it is not an update, a new view is populated newview = new LinearLayout (GetContext ()); String inflater = Context.layout_inflater_service; Layoutinflater Li;li = (layoutinflater) getcontext (). Getsystemservice (Inflater); Li.inflate (resource, newView,true);} else{//Otherwise update the existing view Newview = (linearlayout) Convertview;} User user = GetItem (position); return Super.getview (position, convertview, parent);}}

The GetView method is used to construct, populate the view that will be added to the parent Adapterview class (such as a ListView), which is bound to the underlying array using this adapter by the parent Adatperview class.

To apply a adapter to a class derived from Adapterview, you can invoke the Setadapter method of the view and pass it to a adapter instance:

arraylist<string> myStringArray = new arraylist<string> (); int layoutid = Android. R.layout.simple_list_item_1; arrayadapter<string> myadapterinstance;myadapterinstance = new arrayadapter<string> (GetContext (), Layoutid,mystringarray); Mylistview.setadapter (myadapterinstance);



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.