Android-ArrayAdapter usage example (reproduced), androidadapter
Recently, many Android Developers wrote that the difference between ArrayAdapter and BaseAdapter is not very clear. Here, Android123 briefly describes their relationship and usage. ArrayAdapter is derived from BaseAdapter and has all the functions of BaseAdapter, however, ArrayAdapter is more powerful. It can be directly constructed using generics during instantiation. We can see Android in the android SDK. widget. you can also use the second parameter of ArrayAdapter (Context context, int textViewResourceId) to directly bind a layout. The following example uses Java generic instantiation.
Using the Adapter, we construct an item that supports icons. In the following example, we use imageView to display images in getView. Of course, android123 prompts that TextView can be directly bound to a drawable object for display, void setCompoundDrawables (Drawable left, Drawable top, Drawable right, Drawable bottom), void merge (int left, int top, int right, int bottom), and void Merge (Drawable left, drawable top, Drawable right, Drawable bottom), where the second int type specifies the resource id, and the orientation is the position where textview displays the drawable object
After talking about so many ArrayAdapater instances, let's take a look at the example to instantiate the ArrayAdapter. We can modify the Res/layout/icon_list_item.xml file to implement custom display.
Java code
- Public class IconListAdapter extends ArrayAdapter <IconListAdapter. IconListItem> {
- Protected LayoutInflater mInflater;
- Private static final int mResource = R. layout. icon_list_item; // xml layout File
- Public IconListAdapter (Context context,
- List <IconListItem> items ){
- Super (context, mResource, items );
- MInflater = (LayoutInflater) context. getSystemService (Context. LAYOUT_INFLATER_SERVICE );
- }
- @ Override
- Public View getView (int position, View convertView, ViewGroup parent ){
- TextView text;
- ImageView image;
- View view;
- If (convertView = null ){
- View = mInflater. inflate (mResource, parent, false );
- } Else {
- View = convertView;
- }
- Text = (TextView) view. findViewById (R. id. text1 );
- Text. setText (getItem (position). getTitle ());
- Image = (ImageView) view. findViewById (R. id. icon); // you can use the three methods described above to directly bind the icon to the display using setCompoundDrawables and other methods of the TextView class.
- Image. setImageResource (getItem (position). getResource ());
- Return view;
- }
- Public static class IconListItem {// constructor for each display
- Private final String mTitle;
- Private final int mResource;
- Public IconListItem (String title, int resource ){
- MResource = resource;
- MTitle = title;
- }
- Public String getTitle (){
- Return mTitle;
- }
- Public int getResource (){
- Return mResource;
- }
- }
- }
Of course, where is ArrayAdapter more advanced than BaseAdapter? From the perspective of Array name, we can contact a lot of Array Operations. That's right, Android123 lists all the member methods of this class for practical processing methods, such:
Java code
- Void add (T object) // add an object to the ArrayAdapter
- Void clear () // clear all elements
- Static ArrayAdapter <CharSequence> createFromResource (Context context, int textArrayResId, int textViewResId) // construct an arrayadapter from the layout Resource
- Context getContext () // get the instance
- Int getCount ()
- View getDropDownView (int position, View convertView, ViewGroup parent) // obtain the content of the popup style selection entry in the drop down state. Parameter 1 is the position, parameter 2: You can directly obtain the content of this article through forced conversion.
- Filter getFilter () // use regular expressions to Filter data
- T getItem (int position) // obtain the single content
- Long getItemId (int position)
- Int getPosition (T item) // obtain a certain value through the content
- View getView (int position, View convertView, ViewGroup parent)
- Void insert (T object, int index) // insert a new entry to the index position of the array
- Void notifyDataSetChanged () // notifies the widget bound to the Adapter to update the UI.
- Void remove (T object) // removes an array from which no position is specified.
- Void setDropDownViewResource (int resource) // sets the layout style of dropdown.
- Sets the layout resource to create the drop down views.
- Void setNotifyOnChange (boolean notifyOnChange) // This is the most powerful feature of arrayadapter. android123 strongly recommends that you use this method when processing big data, which can reduce the amount of ui processing and refresh the ui more quickly, you can stop
- (Add (T), insert (T, int), remove (T), clear () operations, of course, can be notified of changes through yydatasetchanged (). Or setNotifyOnChange (true)
- Void sort (Comparator <? Super T> comparator) // This Is A sort that is often used by the android Development Network. arrayadapter can be used for direct sorting, which is very convenient.
In the end, we recommend that you use the arrayadapter and baseadapter in android123. When the number is large, for example, more than 100 items, or frequent dynamic increase or decrease, arrayadapter can be used to conveniently control the ui. The setNotifyOnChanage method saves more resources by simply rendering the result directly from the baseadapter.