Basic data type with class object as Arrayadapter binding (similar to simpleadater effect)
The basic data type of the general Arrayadapter binding is string, and the following describes the class object as the basic data type;
First, create a new class news, which is the basic data type
Packagecom.example.news;ImportAndroid. R.integer;ImportAndroid.widget.ImageView; Public classNews {PrivateString title; PrivateString content; Private intimageId; News (String title,string content,intimageId) { This. title=title; This. content=content; This. imageid=imageId; } PublicString GetTitle () {returntitle; } PublicString getcontent () {returncontent; } Public intGetimageid () {returnimageId; } Public voidSettitle (String title) { This. title=title; } Public voidsetcontent (String content) { This. content=content; }}
Next, the ListView item's layout is determined, there is a textview and imageview;
<LinearLayoutxmlns:android= "Http://schemas.android.com/apk/res/android"Android:layout_width= "Match_parent"Android:layout_height= "Match_parent"android:orientation= "vertical" > <TextViewAndroid:id= "@+id/news_title"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"/> <ImageViewAndroid:id= "@+id/news_image"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"/></LinearLayout>
Then customize an adapter Newsadapter, inherit Arrayadapter, and implement two of these methods, ResourceID is the ID of the layout above the ListView item;
Packagecom.example.news;Importjava.util.List;ImportAndroid.content.Context;ImportAndroid.view.LayoutInflater;ImportAndroid.view.View;ImportAndroid.view.ViewGroup;ImportAndroid.widget.ArrayAdapter;ImportAndroid.widget.ImageView;ImportAndroid.widget.TextView; Public classNewsadapterextendsArrayadapter<news>{ Private intresourceId; PublicNewsadapter (Context context,intResource, list<news>objects) { Super(Context, resource, objects); //TODO auto-generated Constructor stubResourceid=resource; } @Override PublicView GetView (intposition, View Convertview, ViewGroup parent) { //TODO auto-generated Method StubNews News=GetItem (position); View view; if(convertview==NULL) {View=layoutinflater.from (GetContext ()). Inflate (ResourceId,NULL); }Else{View=Convertview; } TextView news_title=(TextView) View.findviewbyid (r.id.news_title); ImageView News_image=(ImageView) View.findviewbyid (r.id.news_image); News_title.settext (News.gettitle ()); News_image.setimageresource (News.getimageid ()); returnview; }}
Then add a ListView control to the main layout, which is a simple one that you will definitely
<Relativelayoutxmlns:android= "Http://schemas.android.com/apk/res/android"Xmlns:tools= "Http://schemas.android.com/tools"Android:layout_width= "Match_parent"Android:layout_height= "Match_parent"Tools:context= "${relativepackage}.${activityclass}" > <ListViewAndroid:id= "@+id/list_title"Android:layout_width= "Wrap_content"Android:layout_height= "Match_parent"></ListView></Relativelayout>
Then rewrite the main activity
Importjava.util.ArrayList;Importjava.util.List;Importandroid.app.Activity;ImportAndroid.os.Bundle;ImportAndroid.view.Menu;ImportAndroid.view.MenuItem;ImportAndroid.widget.ListView; Public classMainactivityextendsActivity {PrivateListView List_title; PrivateList<news> list=NewArraylist<news>(); PrivateNewsadapter Adapter; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); Setcontentview (R.layout.activity_main); List_title=(ListView) Findviewbyid (r.id.list_title); Initlist (); Adapter=NewNewsadapter ( This, r.layout.news_item,list); List_title.setadapter (adapter); } Private voidinitlist () {News news1=NewNews ("Heading 1", "1", R.drawable.ic_launcher); List.add (NEWS1); News news2=NewNews ("Heading 2", "2", R.drawable.ic_launcher); List.add (NEWS2); News NEWS3=NewNews ("Heading 3", "3", R.drawable.ic_launcher); List.add (NEWS3); News News4=NewNews ("Heading 4", "4", R.drawable.ic_launcher); List.add (NEWS4); } }
Interface effect
Don't know how to leave a message
My Android Learning experience 34