Android: Daily Learning Notes (8) ——— Explore UI Development (5) Simple use of ListView control
Public classMainactivityextendsappcompatactivity {PrivateString[] data={"Apple", "Banana", "Orange", "Watermelon", "Pear", "Grape", "Pineapple", "Strawberry",
"Apple", "Banana", "Orange", "Watermelon", "Pear", "Grape", "Pineapple", "Strawberry",
"Apple", "Banana", "Orange", "Watermelon", "Pear", "Grape", "Pineapple", "Strawberry"}; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); Arrayadapter<String> adapter =NewArrayadapter<string>
(Mainactivity. This, R.layout.support_simple_spinner_dropdown_item,data); ListView ListView=(ListView) Findviewbyid (R.id.list_view); Listview.setadapter (adapter); }}
Description
ARRAYADAPTER Specifies the data type to be adapted by generics, and then in the constructor the current context, the ID of the ListView Child layout, and the data to be adapted are passed in.
We used the built-in layout file for Android here.
The ListView sets the adapter, Setadapter (), and passes the built-in adapter object in order to establish the relationship between the data and the ListView.
Customizing the ListView Interface
1. Defining an entity class
Public classFruit {PrivateString name; Private intImageID; PublicFruit (String name,intImageID) { This. Name =name; This. ImageID =ImageID; } PublicString GetName () {returnname; } Public voidsetName (String name) { This. Name =name; } Public intGetimageid () {returnImageID; } Public voidSetimageid (intImageID) { This. ImageID =ImageID; }}
2. Customizing the Sub-item layout file
<?XML version= "1.0" encoding= "Utf-8"?><LinearLayoutxmlns:android= "Http://schemas.android.com/apk/res/android"Android:layout_width= "Match_parent"Android:layout_height= "Match_parent"> <ImageViewAndroid:id= "@+id/fruit_img"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content" /> <TextViewAndroid:id= "@+id/fruit_text"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"android:layout_gravity= "Center_vertical"Android:layout_marginleft= "10DP"/></LinearLayout>
3. Writing an adaptation class
Public classFruitadapterextendsArrayadapter<fruit> { Private intResourceID; PublicFruitadapter (@NonNull context context, @LayoutResintResource, @IdResintTextviewresourceid, @NonNull list<fruit>objects) { Super(context, resource, Textviewresourceid, objects); ResourceID=Textviewresourceid; } @NonNull @Override PublicView GetView (intposition, @Nullable View Convertview, @NonNull viewgroup parent) {Fruit Fruit=GetItem (position); View View= Layoutinflater.from (GetContext ()). Inflate (Resourceid,parent,false); ImageView Fruitimage=(ImageView) View.findviewbyid (r.id.fruit_img); TextView Fruittext=(TextView) View.findviewbyid (R.id.fruit_text); Fruitimage.setimageresource (Fruit.getimageid ()); Fruittext.settext (Fruit.getname ()); returnview; }}
4. Application
Public classMainactivityextendsappcompatactivity {PrivateList<fruit> fruitlist=NewArraylist<>(); protected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);Setcontentview (R.layout.activity_main); Initfruits (); Fruitadapter Adapter=NewFruitadapter (mainactivity. This, r.layout.activity_main,r.layout.entry_fruit_item,fruitlist); ListView ListView=(ListView) Findviewbyid (R.id.list_view); Listview.setadapter (adapter); } Public voidinitfruits () { for(inti=0;i<100;i++) {Fruitlist.add (NewFruit ("XXX", r.drawable.next_24px)); } }}
I
Android: Daily Learning Notes (8) ——— Explore UI Development (5)