There are two main functions of the ListView:
- Populating data into layouts
- Handle the user's choice of clicks and other actions
The display of a list requires three elements :
- Listveiw view that is used to display lists
- Adapters (Adapter) are used to map data to mediations on a ListView
- The data is specific to the string, picture, or base component that will be mapped
The first thing to know is what the adapter is :
The adapter is a bridge that connects data and Adapterview (a ListView is a typical adapterview) that effectively separates the data from the Adapterview, making it easier to bind the adapterview with the data. Modifications are more convenient.
Android offers a lot of adapter, commonly used in 4 kinds:
- arrayadapter<t>----> used to bind an array to support generic operations
- Simpleadapter----> used to bind the data corresponding to the controls defined in the XML
- Simplecursoradapter---data used to bind cursors
- Baseadapter----> Universal Base Adapter
Today we're going to learn about the ListView & Baseadapter
1.Layout.xml
<?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= "vertical" > <ListView android:id= "@+id/listview1" android:layout_width= "Match_parent" android:layout_height= "Match_parent" > </ListView> </LinearLayout>
2. set the display style for the item added by the ListView
<?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:id= "@+id/imageview1"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:layout_margin= "10DP"android:src= "@drawable/tou1"/> <LinearLayout android:layout_weight= "1"Android:layout_width= "Match_parent"Android:layout_height= "80DP"android:orientation= "Vertical"Android:layout_margin= "5DP" > <TextView Android:id= "@+id/tv_username"Android:layout_width= "Match_parent"Android:layout_height= "20DP"/> <TextView Android:id= "@+id/tv_lastmessage"Android:layout_width= "Match_parent"Android:layout_height= "15DP"/> </LinearLayout> <TextView Android:id= "@+id/tv_datetime"Android:layout_width= "150DP"Android:layout_height= "15DP"/> </LinearLayout>
3. simulate database Insert, create an entity class first
Packagecom.example.entity; Public classMessage {PrivateString tou1; PrivateString UserName; PrivateString Lastmessage; PrivateString datetime; PublicString getTou1 () {returntou1; } Public voidsetTou1 (String tou1) { This. TOU1 =tou1; } PublicString GetUserName () {returnUserName; } Public voidsetusername (String userName) { This. UserName =UserName; } PublicString getlastmessage () {returnLastmessage; } Public voidsetlastmessage (String lastmessage) { This. Lastmessage =Lastmessage; } PublicString GetDateTime () {returndatetime; } Public voidsetdatetime (String datetime) { This. DateTime =datetime; } }
4. Data Acquisition of the ListView
Packagecom.example.winxin2; Importjava.util.ArrayList; Importjava.util.List; ImportCom.example.entity.Message; Importandroid.app.Activity; ImportAndroid.os.Bundle; ImportAndroid.util.Log; ImportAndroid.view.ContextMenu; ImportAndroid.view.ContextMenu.ContextMenuInfo; ImportAndroid.view.LayoutInflater; ImportAndroid.view.Menu; ImportAndroid.view.MenuItem; ImportAndroid.view.View; ImportAndroid.view.View.OnCreateContextMenuListener; ImportAndroid.view.ViewGroup; ImportAndroid.widget.AdapterView; ImportAndroid.widget.AdapterView.OnItemClickListener; ImportAndroid.widget.BaseAdapter; ImportAndroid.widget.ListView; ImportAndroid.widget.TextView; ImportAndroid.widget.Toast; Public classMainactivityextendsActivity {PrivateListView LV; PrivateList<message> messagelist =NewArraylist<message>(); @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); Setcontentview (r.layout.weixin); //analog Read database or Internet for(inti = 0; I < 100; i++) {Message m=NewMessage (); M.SETTOU1 ("XXX"); M.setusername ("Zhang San Crazy" +i); M.setlastmessage ("Tonight there's a date, together Huashan on the sword"); M.setdatetime ("November 11"); Messagelist.add (m); } LV=(ListView) Findviewbyid (R.ID.LISTVIEW1); //simple understanding of the VC tied togetherLv.setadapter (NewBaseadapter () {//how many records are returned@Override Public intGetCount () {//TODO auto-generated Method Stub returnmessagelist.size (); } //Each item item returns an interface@Override PublicView GetView (intposition, View Convertview, ViewGroup parent) {View View=NULL; //the following half of the paragraph is designed to allow us to stretch more smoothly, that is, the data we extracted before we can get in the cache, do not need to retrieve it again//the layout is constant and the data changes//if the cache is empty, we generate a new layout as 1 item if(convertview==NULL) {log.i ("Info:", "no cache, regenerate" +position); Layoutinflater Inflater= Mainactivity. This. Getlayoutinflater (); //because the object returned by GetView (), adapter is automatically assigned to the ListViewView = Inflater.inflate (R.layout.listview_item_layout,NULL); }Else{log.i ("Info:", "have cache, do not need to regenerate" +position); View=Convertview; } Message M=Messagelist.get (position); TextView Tv_username=(TextView) View.findviewbyid (r.id.tv_username); Tv_username.settext (M.getusername ()); Tv_username.settextsize (15); TextView Tv_lastmessage=(TextView) View.findviewbyid (r.id.tv_lastmessage); Tv_lastmessage.settext (M.getlastmessage ()); Tv_lastmessage.settextsize (12); TextView Tv_datetime=(TextView) View.findviewbyid (r.id.tv_datetime); Tv_datetime.settext (M.getdatetime ()); Tv_datetime.settextsize (12); returnview; } @Override PublicObject GetItem (intposition) { //TODO auto-generated Method Stub return NULL; } @Override Public LongGetitemid (intposition) { //TODO auto-generated Method Stub return0; } } ); //Add a Click event to a ListViewLv.setonitemclicklistener (NewOnitemclicklistener () {@Override Public voidOnitemclick (adapterview<?>Parent, view view,intPositionLongID) {Message getList=Messagelist.get (position); String infoid= GETLIST.GETTOU1 ();//Get information IDString infotitle = Getlist.getusername ();//Get information titleString infodetails = Getlist.getlastmessage ();//Get information Details//Toast Display TestToast.maketext (mainactivity. This, "Information ID:" +infoid,toast.length_short). Show (); } }); //Long Press Menu displayLv.setoncreatecontextmenulistener (NewOncreatecontextmenulistener () { Public voidOncreatecontextmenu (ContextMenu conmenu, view view, Contextmenuinfo info) {/** Parameters Menu: Parameter menu for event context. Parameter V: Parameter V is the event Source view, which may receive an event response for the method when the view has the focus. The parameter Info:info object encapsulates additional information about the context menu, which depends on the event source view. */Conmenu.setheadertitle (Menu); Conmenu.add (0, 0, 0, "entry One"); Conmenu.add (0, 1, 1, "Entry II"); Conmenu.add (0, 2, 2, "entry three"); } }); } //long-press Menu handling functions Public Booleanoncontextitemselected (MenuItem aitem) {adapterview.adaptercontextmenuinfo info=(Adapterview.adaptercontextmenuinfo) aitem.getmenuinfo (); Switch(Aitem.getitemid ()) { Case0: Toast.maketext (mainactivity. This, "You clicked on the entry one", Toast.length_short). Show (); return true; Case1: Toast.maketext (mainactivity. This, "You clicked on item two", Toast.length_short). Show (); return true; Case2: Toast.maketext (mainactivity. This, "You clicked on item three", Toast.length_short). Show (); return true; } return false; } }
Thank you for your attention. To find the moon to the Sea, just before dawn flying Dream Watanabe Kun Ocean
Android Listview & Adapter