The use of various adapter

Source: Internet
Author: User

Also a ListView, you can use different adapter to show it, such as the most commonly used arrayadapter,simpleadapter,simplecursoradapter, as well as rewrite baseadapter methods.

Arrayadapter is relatively simple, but it can only be used to display text. While Simpleadapter has a strong extensibility, can customize a variety of effects, simplecursoradapter can read data from the database display on the list, through the write Baseadapter can be added to the list of events such as processing.

Arrayadapter:

1  Packagecom.shang.test;2 3  Importjava.util.ArrayList;4 5  Importandroid.app.Activity;6  ImportAndroid.os.Bundle;7  ImportAndroid.widget.ArrayAdapter;8  ImportAndroid.widget.ListView;9 Ten  /** One  *  A  * @authorShangzhenxiang -  * -  */ the   Public classTestarrayadapteractivityextendsactivity{ -  -     PrivateListView Mlistview; -     PrivateArraylist<string> marraylist =NewArraylist<string>(); +      - @Override +     protected voidonCreate (Bundle savedinstancestate) { A         Super. OnCreate (savedinstancestate); at Setcontentview (r.layout.testarrayadapter); -Mlistview =(ListView) Findviewbyid (r.id.myarraylist); -Mlistview.setadapter (NewArrayadapter<string> ( This, Android. R.layout.simple_expandable_list_item_1, GetData ())); -     } -      -     PrivateArraylist<string>GetData () { inMarraylist.add ("Test data 1"); -Marraylist.add ("Test data 2"); toMarraylist.add ("Test Data 3"); +Marraylist.add ("Test Data 4"); -Marraylist.add ("Test Data 5"); theMarraylist.add ("Test Data 6"); *         returnmarraylist; $     }Panax Notoginseng}
View Cod

There's a ListView in the layout:

<?xml version= "1.0" encoding= "Utf-8"?> <linearlayout xmlns:android= "http://schemas.android.com/apk/res/ Android "    android:orientation=" vertical "    android:layout_width=" Match_parent "     Android:layout_height= "Fill_parent" >    <TextView          android:layout_width= "Match_parent"          android:layout_height= "Wrap_content"         android:text= "@string/hello"/>    <ListView        android:id= "@+id/myarraylist"        android:layout_width= "Match_parent"         android:layout_height= "Wrap_content"/>     </LinearLayout>
View Code

The above code is constructed using the Arrayadapter method:

public Arrayadapter (context context, int Textviewresourceid, t[] objects)

This is described in the API:

Where context is the current environment variable, a layout file that can display one line of text, and a collection of lists, that is, the data source.

Layout files can be written on their own, can also use the system, I am here to use the system. Write your own layout contains a TextView, of course, the system also contains a TextView layout file: android. R.layout.simple_expandable_list_item_1, it's easier to call this.

Here is the post-run:

  

Simpleadapter:

 Packagecom.shang.test;Importjava.util.ArrayList;ImportJava.util.HashMap;Importjava.util.List;Importandroid.app.Activity;ImportAndroid.os.Bundle;ImportAndroid.widget.ListView;ImportAndroid.widget.SimpleAdapter;/** *  * @authorShangzhenxiang **/ Public classTestsimpleadapterextendsActivity {PrivateListView Mlistview; PrivateSimpleadapter Madapter; PrivateListMhashmaps; PrivateHashmap<string, object>map; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);        Setcontentview (R.layout.testsimpleadapter); Mlistview=(ListView) Findviewbyid (r.id.mysimplelist); Madapter=NewSimpleadapter ( This, GetData (), R.layout.simpleitem,Newstring[]{"image", "title", "Info"},New int[]{r.id.img, R.id.title, r.id.info}];    Mlistview.setadapter (Madapter); }        PrivateListGetData () {mhashmaps=NewArraylist(); Map=NewHashmap<string, object>(); Map.put ("Image", r.drawable.gallery_photo_1); Map.put ("title", "G1"); Map.put ("Info", "Google 1");                Mhashmaps.add (map); Map=NewHashmap<string, object>(); Map.put ("Image", r.drawable.gallery_photo_2); Map.put ("title", "G2"); Map.put ("Info", "Google 2");                Mhashmaps.add (map); Map=NewHashmap<string, object>(); Map.put ("Image", R.drawable.gallery_photo_3); Map.put ("title", "G3"); Map.put ("Info", "Google 3");        Mhashmaps.add (map); returnMhashmaps; }}
View Code
<?xml version= "1.0" encoding= "Utf-8"?><LinearLayout xmlns:android= "Http://schemas.android.com/apk/res/android"Android:layout_width= "Match_parent"Android:layout_height= "Match_parent"android:orientation= "Horizontal" > <ImageView android:layout_width= "Wrap_content"Android:id= "@+id/img"Android:layout_margin= "5px"Android:layout_height= "Wrap_content" > </ImageView> <LinearLayout Android:id= "@+id/linearlayout1"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"android:orientation= "Vertical" > <TextView Android:id= "@+id/title"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:textcolor= "#ffffff"android:textsize= "22px" ></TextView> <TextView Android:id= "@+id/info"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:textcolor= "#ffffff"android:textsize= "13px" ></TextView> </LinearLayout></LinearLayout>
View Code

Simpleadapter's:


The first parameter and the third argument are the same as in Arrayadapter, the second parameter is a list of HashMap, which is the data source, and the 5th parameter is the key in the map. The last parameter is the ID of the location in the map where the key corresponds to the value to be displayed in the layout.

Look at the effect:

The use of various adapter

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.