<span id="Label3"></p><p><p>A ListView is a very wide-ranging component used in a mobile phone system that displays list items in a vertical list.</p></p><p><p><br></p></p><p><p>Today we learn how to display the System's SMS to the LISTVIEW.</p></p><p><p>For information on how to get the system, see:</p></p><span class="link_title"><span class="link_title">Android Four Components learning ContentProvider three</span></span><p><p></p></p><p><p>Let's take a look at our layout file:</p></p><p><p></p></p><pre name="code" class="html"><pre name="code" class="html"><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 " tools:context= "${relativepackage}.${activityclass}" ><listview android:id= "@+id/lv" android: Layout_width= "match_parent" android:layout_height= "match_parent" ></listview></ Linearlayout></pre></pre><br>There is only one simple listview Component.<p><p></p></p><p><p></p></p><p><p>You can see that the ListView Preview interface is a list of items, each line is an item item, and the display style for each row is the same, so each row also needs a layout file</p></p><p><p>Look at the layout file for each line:</p></p><p><p></p></p><pre name="code" class="html"><pre name="code" class="html"><?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:o rientation= "vertical" > <textview android:id= "@+id/tv_phone" android:layout_height= "wrap_ Content " android:layout_width=" wrap_content " /> <textview android:id=" @+id/tv_body " android:layout_height= "wrap_content" android:layout_width= "wrap_content" android:singleline= " True " android:ellipsize=" End " /></linearlayout></pre></pre>Layout file, A total of 2 textview, the first display number, the second display text message content<p><p></p></p><p><p>Now that you know the layout file, Let's look at the code in the activity<br></p></p><pre name="code" class="java">public class Mainactivity extends Activity {list<message> List; @Override protected void onCreate (Bundle Savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (r.layout.activity_main); List = new Arraylist<message> (); Get contentresolver contentresolver cr = Getcontentresolver (); Query the system for sms, only need to query the fields we care about cursor cursor = cr.query (uri.parse ("content://sms"), new string[]{"address", "date", "type", "bo Dy "}, null, null, null); Remove the queried information while (cursor.movetonext ()) {String address = cursor.getstring (cursor.getcolumnindex ("address")); String date = cursor.getstring (cursor.getcolumnindex ("date")); String type = cursor.getstring (cursor.getcolumnindex ("type")); String BODY = cursor.getstring (cursor.getcolumnindex ("body")); Put each text message in the list collection message msg = new Message (address, date, type, body); List.add (msg); }//gets to the listview listview ListView = (listview)Findviewbyid (r.id.lv); Set adapter Listview.setadapter (new Myadapter ()); }//define myadapter inherit Baseadapter class Myadapter extends baseadapter {@Overridepublic int GetCount () {//return Return the number of elements returned list.size ();} Return each element, can not care @overridepublic Object getItem (int arg0) {return null;} @Overridepublic long Getitemid (int arg0) {//returns The ID of the current element without concern for return 0;} @Overridepublic View GetView (int position, view convertview, viewgroup arg2) {//returns An object displayed in the Listview//object message message = List.get (position);//create A layout filler object layoutinflater inflater = Layoutinflater.from (mainactivity.this);// Get view object from layout filler view view = inflater.inflate (r.layout.item_list, null);//find TextView object TextView phone = (TextView) View.findviewbyid (r.id.tv_phone);p hone.settext (message.getaddress ());//find TextView object TextView BODY = (TextView) View.findviewbyid (r.id.tv_body); body.settext (message.getbody ()); return view;} }}</pre><br>Where the message class Is:<p><p></p></p><p><p></p></p><pre name="code" class="java"><pre name="code" class="java">public class Message {private string Address;//number private string date; Time Private String type; Type: 1 for the occurrence, 2 for the acceptance of the private String body; SMS content public String getaddress () {return address;} public void setaddress (String Address) {this.address = address;} Public String getDate () {return date;} public void SetDate (String Date) {this.date = date;} Public String getType () {return type;} public void SetType (String Type) {this.type = type;} Public String getbody () {return body;} public void Setbody (String body) {this.body = body;}}</pre></pre>The display effect is:<p><p></p></p><p><p></p></p><p><p>The effect is there, if we have a lot of data, of course, a screen is not displayed, you can swipe up and down to show the following, the ListView has been done in the Package.</p></p><p><p><br></p></p><p><p>Now that it shows up, let's refine the display of a listview:</p></p><p><p></p></p><p><p>I add a print to Myadapter's getview: as shown, we run the program</p></p><p><p></p></p><p><p>You can see that postion is where the ListView appears, starting with 0. At this point convertview is null at First. 12 rows are now Displayed. I'm pulling down a line</p></p><p><p></p></p><p><p>You can see the Drop-down 12th, Convertview is still null, and then the 13th ONE.</p></p><p><p></p></p><p><p>You can see that 13 of them are not Empty. And 13 Convertview also have a Value.</p></p><p><p>Then someone asked what Convertview Was. In fact, Convertview is the buffer, when we pull 13 out of time, 0 has gone out, when 0 in the buffer, so Convertview is 0</p></p><p><p>Since there is data in the buffer, we do not need to recreate the view object each time, if there is a buffer, directly from the buffer row, it is a waste of resources</p></p><p><p>Modify the above code To:</p></p><p><p></p></p><pre name="code" class="java"><pre name="code" class="java">Create a layout filler object layoutinflaterinflater = Layoutinflater.from (mainactivity.this); View view = null;if (convertview = = Null) {//get View object from the layout filler view = inflater.inflate (r.layout.item_list, null);} else {view = convertview;}</pre></pre><br><br><p><p></p></p>So when we have very large data in practice, there is no problem of system resource OVERFLOW.<br> <p style="font-size:12px;"><p style="font-size:12px;">Copyright Notice: This article for Bo Master original article, without Bo Master permission not Reproduced.</p></p> <p><p>The Android UI Learning ListView</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