Android Learning Notes: ListView and Baseadapter use
Source: Internet
Author: User
<span id="Label3"></p><p><p>The ListView is one of the most important components in android, and basically all software uses the listview, so you are familiar with the LISTVIEW.</p></p><p><p>First look at the Program:</p></p><p style="text-align:center;"><p style="text-align:center;">650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M02/6D/0A/wKiom1VaoBTSUb_VAAB1s7ztDQ8507.jpg "title=" QQ picture 20150519095047.png "alt=" wkiom1vaobtsub_vaab1s7ztdq8507.jpg "/></p></p><p><p>The ListView has a lot of styles, plain text type, with picture display, with buttons and so On. This demo shows a ListView with a picture.</p></p><p><p>① Layout file:</p></p><p><p>In a ListView program, the layout file has at least one more than the other normal controls, because it also requires a layout file about the content entries in the LISTVIEW.</p></p><p><p>Layout file for content entry Listview_item.xml:</p></p><pre class="brush:xml;toolbar:false"><?xml version= "1.0" encoding= "utf-8"? ><relativelayout xmlns:android= "http// Schemas.android.com/apk/res/android " android:layout_width=" match_parent " android:layout_height= "android:attr/listpreferreditemheight" > <imageview android:id= "@+id/id_ ImageView " android:layout_width=" wrap_content " android:layout_height= "wrap_content" android:background= "@drawable/ic_launcher" android: layout_alignparentleft= "true" android:layout_alignparenttop= " True "/> <textview android:id = "@+id/id_tv_title" &NBSP;&NBSP;&NBSP;&NBSp; android:layout_width= "wrap_content" android:layout_height= "wrap_content" android:layout_torightof= " @id/id_imageview " android:layout_marginleft=" 15DP " android:layout_alignparenttop= "true" android:textsize= "22sp" android:textstyle= "bold" android:text= "title"/> <textview android:id= "@+id/id_tv_desc" android:layout_width= "wrap_content" android:layout_height= "wrap_content" Android:layout_torightof= "@id/id_imageview " android:layout_below=" @id/id_tv_title " android:layout_marginleft= "15dp" android:textsize= "18sp" android:text= "long Long ago story "/> </relativelayout></pre><p><p>Look at the code, you know what I said. the layout of the ListView content entry refers to</p></p><p><p>650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M02/6D/0A/wKiom1VapkfxLmTcAAAngv935pg299.jpg "title=" QQ picture 20150519105434.png "alt=" wkiom1vapkfxlmtcaaangv935pg299.jpg "/> such a layout file</p></p><p><p><br></p></p><p><p>Another layout file, the overall layout file for the entire Android program Activity_main.xml:</p></p><pre class="brush:xml;toolbar:false"><linearlayout xmlns: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" android:orientation= "vertical" tools:context= "com.example.testlistview.MainActivity" > <linearlayout android:id = "@+id/listview_ll" android:layout_width= "match_parent" android:layout_height= "wrap_content" android:orientation= "vertical" > <ListView android:Id= "@id/android:list" android:layout_ Width= "match_parent" android:layout_ height= "wrap_content" android: drawselectorontop= "true" android: scrollbars= "vertical"/> </ Linearlayout></linearlayout></pre><p><p>There is only one <listview/> where scrollbars= "vertical" means that if the entries are many, the scroll bars are vertical when scrolling.<br></p></p><p><p><br></p></p><p><p>②android Code</p></p><p><p><br></p></p><p><p>Mainactivity.java</p></p><pre class="brush:java;toolbar:false">Package com.example.testlistview;import java.util.arraylist;import java.util.hashmap;import java.util.List;import java.util.Map;import android.app.ListActivity;import android.content.context;import android.os.bundle;import android.widget.simpleadapter;/** * Note inherited by listactivity * @author jam * */public class MainActivity extends listactivity {private list<map<string, object>> list;@ Overrideprotected void oncreate (bundle savedinstancestate) {super.oncreate ( savedinstancestate); Setcontentview (r.layout.activity_main);list = new arraylist<map< String, object>> (); Map<string, object> map1 = new hashmap<string, object> (); Map<string, object> map2 = new hashmap<string, object> (); map<string, object> map3 = new  Hashmap<string, object> ();//three items, respectively, are pictures, headlines, content map1.put ("avatar", r.drawable.add_option); map1.put ( "user_name", "zhangsan") map1.put ("user_ip", "192.168.1.0"); map2.put ("avatar", r.drawable.add_ pic), map2.put ("user_name", "lisi"), map2.put ("user_ip", "192.168.1.1"); map3.put ("avatar", R.drawable.ic_launcher) map3.put ("user_name", "wangwu") map3.put ("user_ip", "192.168.1.2"); list.add ( map1); list.add (map2); list.add (map3);//simpleadapter using/** * Four parameters * the first parameter is the current context * the second parameter, the data source list * the third fourth equivalent to the key-value pair *//*setlistadapter (new simpleadapter ( Getapplicationcontext (), list,r.layout.listview_item, new string[] {"user_name", " User_ip "},new int[] {r.id.id_tv_title, r.id.id_tv_desc}"), *///settings Adaptersetlistadapter (new Mybaseadapter (getapplicationcontext (), list));}}</pre><p><p><br></p></p><p><p>where Mybaseadapter.java code:</p></p><pre class="brush:java;toolbar:false">Package com.example.testlistview;import java.util.arraylist;import java.util.hashmap;import java.util.List;import java.util.Map;import android.content.Context;import android.view.layoutinflater;import android.view.view;import android.view.viewgroup;import android.widget.baseadapter;import android.widget.imageview;import android.widget.textview;/** * Inheritance baseadapter * is troublesome relative to simpleadapter, but brings more benefits * @author jam * */ public class mybaseadapter extends baseadapter {private layoutinflater Myinfalater; private list<map<string, object>> list;public mybaseadapter ( Context context, list<map<string, object>> list) {super ();this.list = list;myinfalater = layoutinflater.from (context);} @Overridepublic int getcount () {return list.size ();} @OverridepublIc object getitem (int position) {return position;} @Overridepublic long getitemid (int positionid) {return positionid;} Important overriding Method @overridepublic view getview (int position, view convertview, viewgroup viewgroup) {viewholder viewholder;if (convertview == null) {convertView = myinfalater.inflate (r.layout.listview_item, null); Viewholder = new viewholder (); viewholder.avatar = (ImageView) convertview.findviewbyid (r.id.id_imageview); viewHolder.title = (TextView) convertview.findviewbyid (r.id.id_tv_title);viewholder.desc = (TextView) convertview.findviewbyid (r.id.id_tv_desc); Convertview.settag (viewholder);} else {viewHolder = (viewholder) convertview.gettag ();} ViewHolder.avatar.setBackgroundResource ((Integer) list.get (position). get ("avatar")); ViewHolder.title.setText ((charsequence)  , List.get (position). get ("user_name")), viewHolder.desc.setText ((charsequence) list.get (position). get ("user _ip ")); return convertview;} private class viewholder {imageview avatar; textview title; textview desc;}}</pre><p><p><br></p></p><p><p><strong><span style="font-size:20px;">Description</span></strong></p></p><p><p><br></p></p><p><p>Baseadapter is a very common adapter, an abstract class that needs to be rewritten in many ways.</p></p><p><p>First call the GetCount () method in adapter, get the length of the listview, and draw each row of the ListView one at a length</p></p><p><p>Onvertview is equivalent to a cache, starting at 0, when an entry becomes invisible, it caches its data, the subsequent entry only needs to update the data, which greatly saves the overhead of system data</p></p><p><p><br></p></p><p><p><strong>Application of Viewholder:</strong></p></p><p><p><br></p></p><p><p>The Findviewbyid () method of view is also time-consuming, so you need to consider calling only once and then using Convertview.gettag () to get the Viewholder Object.</p></p><p><p><br></p></p><p><p>In Layman's words, inflate is the equivalent of locating a layout defined in an Xml.</p></p><p><p><br></p></p><p><p>Because if you use Findviewbyid () directly in an activity, the component in the layout of Setconentview () is the corresponding ONE.</p></p><p><p><br></p></p><p><p>So if you use other layout in your activity, such as the layout on the dialog, you also have to set the contents of the Layout on the dialog box (like picture imageview, text textview), you have to use inflate () Find the layout of the dialog box first, and then use the layout object to find the components on it.</p></p><p><p><br></p></p><p><p>In addition here are some Baseadapter related information:</p></p><p><p>Http://www.open-open.com/lib/view/open1339485728006.html</p></p><p><p>Http://android.tgbus.com/Android/tutorial/201104/348009.shtml</p></p><p><p><br></p></p><p><p>Android Learning Notes: ListView and Baseadapter use</p></p></span>
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