Android Universal Adapter Use example

Source: Internet
Author: User

Android Adapter If you inherit from Baseadapter, you have to implement four methods

GetCount (); GetItem (int position); Getitemid (int position); GetView (int position, View Convertview, viewgroup parent);

in fact, the real change of each adapter is the GetView method. This is used to control the effect each item displays. So I customized a baseadapter, called Appbaseadapter. The code is as follows:

/** * Adapter base class */public abstract class Appbaseadapter<t> extends Baseadapter {public list<t& Gt        List = new arraylist<t> ();        Public Layoutinflater Inflater;        Public Context Mcontext;        Public Appapplication AppContext;            Public Appbaseadapter (Context context) {This.mcontext = context;            AppContext = Appapplication.getinstance ();        Inflater = Layoutinflater.from (Mcontext);        } @Override public int getcount () {return list.size ();        } @Override public T getItem (int position) {return list.get (position);        } @Override public long getitemid (int position) {return position; } @Override public View getView (int position, view Convertview, ViewGroup parent) {return conver        TView; }//Add data to list public void Refreshadapter (list<t> arrayList) {List.addalL (arrayList);        Notifydatasetchanged ();            }//Empty list collection public void Clear () {list.clear ();        Notifydatasetchanged ();            }//Removes the object at the specified location public void remove (int index) {list.remove (index);        Notifydatasetchanged ();            }//Remove object public void Remove (object obj) {list.remove (obj);        Notifydatasetchanged (); }    }
Using a simple generic, the data type of the list is not considered for the moment. One benefit of appbaseadapter is the ability to implement delayed binding of data. Suppose you want to get the data from the server first, and then through the ListView, using Appbaseadapter can be setadatper, wait until the data load completes, by calling Refreshadapter (List collection) to display the data
Here is a simple example of use:
Import Android.app.activity;import android.content.context;import android.os.bundle;import Android.view.View; Import Android.view.viewgroup;import Android.widget.listview;import Android.widget.textview;import    Java.util.arraylist;public class MyActivity extends Activity {private ListView mlistview;    Private View Memptyview;    Private Myadapter adapter;        @Override public void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);        Setcontentview (R.layout.main);        Mlistview = (ListView) This.findviewbyid (R.id.listview);        Memptyview = This.findviewbyid (r.id.empty);        adapter = new Myadapter (this);        Mlistview.setadapter (adapter);    Setemptyview in the view must be included in the root View Mlistview.setemptyview (Memptyview);        } @Override protected void Onresume () {super.onresume ();    LoadData (); } private void LoadData () {new Thread () {@Override public void Run () {sUper.run ();                try {thread.sleep (5 * 1000);                } catch (Interruptedexception e) {e.printstacktrace ();                } Final arraylist<string> strings = new arraylist<string> ();                for (int i = 0, count = ten; i < count; i++) {Strings.add ("Count is:" + i);                        } runonuithread (New Runnable () {@Override public void run () {                    Adapter.refreshadapter (strings);            }                });    }}.start ();        } class Myadapter extends Appbaseadapter {public Myadapter (context context) {super (context); } @Override public View getView (int position, view Convertview, ViewGroup parent) {Viewholder            Holder = null;                if (Convertview = = null) {holder = new Viewholder (); ConVertview = inflater.inflate (R.layout.list_item, NULL);                Holder.textview = (TextView) Convertview.findviewbyid (R.id.textview);            Convertview.settag (holder);            } else {holder = (Viewholder) convertview.gettag ();            } String item = (string) list.get (position);            Holder.textView.setText (item);        return convertview;        } class Viewholder {TextView TextView; }    }}
deme:http://www.oschina.net/code/snippet_924286_44753

Android Universal Adapter Use example

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.