How Android adds a listening method to the ListView
Source: Internet
Author: User
<span id="Label3"></p><p><p><span style="font-size:18px; background-color:rgb(255,102,0)">The first step is to create a ListView</span></p></p><p><p><span style="font-size:18px"><br></span></p></p><p><p><span style="font-size:18px">Get an object by ID</span></p></p><span style="font-size:18px"><span style="white-space:pre"><span style="white-space:pre"></span></span>ListView ListView = (listview) Findviewbyid (r.id.listview1);<br><span style="white-space:pre"><span style="white-space:pre"></span></span>Create an adapter pair<br><span style="white-space:pre"><span style="white-space:pre"></span></span><br><span style="white-space:pre"><span style="white-space:pre"></span></span>The parameter represents the meaning (context object, per-line style type (must be TextView type Android. R..... Is the System's own style, can also use its own definition), the data itself (can be from the resource data, can also be from the array added in java))<br><span style="white-space:pre"><span style="white-space:pre"></span></span>The following method (data from an array of resource files)<br><span style="white-space:pre"><span style="white-space:pre"></span></span>ListAdapter adapter = Arrayadapter.createfromresource (<br><span style="white-space:pre"><span style="white-space:pre"></span></span>this, r.array.ctype, Android. r.layout.simple_list_item_1);<br><span style="white-space:pre"><span style="white-space:pre"></span></span><br><span style="white-space:pre"><span style="white-space:pre"></span></span>Add an adapter to an object<span style="white-space:pre"><span style="white-space:pre"> </span></span><br><span style="white-space:pre"><span style="white-space:pre"></span></span>Listview.setadapter (adapter);<br><br><br><br><br><span style="background-color:rgb(255,102,0)"><span style="background-color:rgb(255,102,0)">//add A listening method to the Object:</span></span><br><span style="white-space:pre"><span style="white-space:pre"></span></span>Listview.setonitemclicklistener (new Onitemclicklistener () {<br><br><br><span style="white-space:pre"><span style="white-space:pre"></span></span>@Override<br><span style="white-space:pre"><span style="white-space:pre"></span></span>public void Onitemclick (adapterview<?> parent, view view,<br><span style="white-space:pre"><span style="white-space:pre"></span></span>The parent refers to the Father View itself, and the second parameter refers to every small item within the ListView that is TestView<br><span style="white-space:pre"><span style="white-space:pre"></span></span>int position, long Id) {//position refers to the line number of the current point<br><span style="white-space:pre"><span style="white-space:pre"></span></span>TODO auto-generated Method Stub<br><span style="white-space:pre"><span style="white-space:pre"></span></span>String result = parent.getitematposition (position). toString ();<br><span style="white-space:pre"><span style="white-space:pre"></span></span>Toast.maketext (listviewactivity.this, result, toast.length_long). show ();<br><br><br><span style="white-space:pre"><span style="white-space:pre"></span></span>}<br><br><br><span style="white-space:pre"><span style="white-space:pre"></span></span><br><span style="white-space:pre"><span style="white-space:pre"></span></span>}<br><span style="white-space:pre"><span style="white-space:pre"></span></span>);<br></span><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>How Android adds a listening method to the 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