Android various adapter usage

Source: Internet
Author: User

Arrayadapter is relatively simple, but it can only be used to display text.

1  Public classMainactivityextendsappcompatactivity {2     PrivateSpinner Myspinner;3     PrivateArraylist<string> mlist=NewArraylist<string>();4     PrivateArrayadapter Myadapter;5 @Override6     protected voidonCreate (Bundle savedinstancestate) {7         Super. OnCreate (savedinstancestate);8 Setcontentview (r.layout.activity_main);9Myspinner=(Spinner) Findviewbyid (r.id.spinner);TenMyadapter=NewArrayadapter<string> ( This, Android. R.layout.select_dialog_item,getdata (mlist)); One Myspinner.setadapter (myadapter); A     } -     PrivateArraylist<string> GetData (arraylist<string>marraylist) { -Marraylist.add ("中文版")); theMarraylist.add ("China"); -         returnmarraylist; -}
Arrayadapter main function
1   <Spinner2         android:id= "@+id/spinner"3         android:layout_width= "Match_parent" 4         android:layout_height= "Wrap_content"5         android:layout_weight= "1"/>
about the XML layout of spinner

The context is the current environment variable, Textviewresourceid for the page layout,list<t> represents the data source.

Simpleadapter is highly scalable and can be customized to a variety of effects

1 Importandroid.support.v7.app.AppCompatActivity;2 ImportAndroid.os.Bundle;3 ImportAndroid.widget.SimpleAdapter;4 ImportAndroid.widget.Spinner;5 Importjava.util.ArrayList;6 ImportJava.util.HashMap;7 Importjava.util.List;8 9 /**Ten  * @authorLinjintang One  */ A  Public classMainactivityextendsappcompatactivity { -     PrivateSpinner Myspinner; -     PrivateSimpleadapter Mysimpleadapter; the     PrivateListMyhashmaps; -     PrivateHashmap<string,object>Mymap; -  - @Override +     protected voidonCreate (Bundle savedinstancestate) { -         Super. OnCreate (savedinstancestate); + Setcontentview (r.layout.activity_main); AMyspinner =(Spinner) Findviewbyid (r.id.spinner); atMysimpleadapter=NewSimpleadapter ( This, GetData (), R.layout.spinner_layout,Newstring[]{"image", "title", "Info"},New int[]{r.id.img,r.id.title,r.id.info}]; - Myspinner.setadapter (mysimpleadapter); -     } -     PrivateListGetData () { -myhashmaps=NewArraylist(); -mymap=NewHashmap<string,object>(); inMymap.put ("image", R.DRAWABLE.IMG1); -Mymap.put ("title", "China"); toMymap.put ("info", "I like China"); + Myhashmaps.add (mymap); -  themymap=NewHashmap<string,object>(); *Mymap.put ("image", R.DRAWABLE.IMG2); $Mymap.put ("title", "FRANCE");Panax NotoginsengMymap.put ("info", "I like FRANCE"); - Myhashmaps.add (mymap); the  +mymap=NewHashmap<string,object>(); AMymap.put ("image", R.DRAWABLE.IMG3); theMymap.put ("title", "中文版")); +Mymap.put ("info", "I like 中文版"); - Myhashmaps.add (mymap); $     returnMyhashmaps; $     } -}
simpleadapter Function Code
1<?xml version= "1.0" encoding= "Utf-8"?>2<linearlayout xmlns:android= "Http://schemas.android.com/apk/res/android"3xmlns:app= "Http://schemas.android.com/apk/res-auto"4Android:layout_width= "Match_parent"5android:layout_height= "Match_parent"6android:orientation= "Horizontal" >7 8<ImageView9Android:id= "@+id/img"TenAndroid:layout_width= "100DP" Oneandroid:layout_height= "80DP" Aapp:srccompat= "@mipmap/ic_launcher"/> -  -<LinearLayout theAndroid:layout_width= "Match_parent" -android:layout_height= "Match_parent" -android:orientation= "Vertical" > -  +<TextView -Android:id= "@+id/title" +Android:layout_width= "Match_parent" Aandroid:layout_height= "50DP" atAndroid:textsize= "20DP" -android:gravity= "Center" -android:text= "TextView"/> -  -<TextView -Android:id= "@+id/info" inAndroid:layout_width= "Match_parent" -android:layout_height= "30DP" toandroid:gravity= "Center" +android:text= "TextView"/> -</LinearLayout> the</LinearLayout>
simpleapadter Page Layout code
1<?xml version= "1.0" encoding= "Utf-8"?>2<linearlayout xmlns:android= "Http://schemas.android.com/apk/res/android"3xmlns:app= "Http://schemas.android.com/apk/res-auto"4Xmlns:tools= "Http://schemas.android.com/tools"5Android:layout_width= "Match_parent"6android:layout_height= "Match_parent"7tools:context= "Angrypig.lookingstar.MainActivity" >8 9 Ten<Spinner OneAndroid:id= "@+id/spinner" AAndroid:layout_width= "Match_parent" -android:layout_height= "Wrap_content" -android:layout_weight= "1"/> the</LinearLayout>
about the XML layout of spinner

Context is the current environment variable, Textviewresourceid for the page layout,list<t> represents the data source, the form corresponding to map key,to corresponding to the layout file ID.

Android various adapter usage

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.