Android UI Learning-ListView (Android. R.layout.simple_list_item_1 is a thing)

Source: Internet
Author: User

Android UI Learning-ListView .- .- -  -: +: *Tags: Android UI Mobile Development ListView Listactivity original works, allow reprint, please be sure to use hyperlinks in the form of the original source of the article, author information and this statement. Otherwise, the legal liability will be investigated. http://android.blog.51cto.com/268543/336162listactivity Listactivity is an activity class that specifically displays the ListView, which contains a ListView object that is automatically displayed as soon as we set up the data source. Use Custom View forScreen Layout Although listactivity has a ListView object built in, we can still use custom view to invoke Setcontentview (resources ID) in OnCreate (). Note, however, that in the custom layout, you want to set the ID of the ListView object to"@android: Id/list"; use android.r.id.list in Java code. In the following example, by adding an ID of android:empty TextView, when there is no data in the ListView, it will be displayed"No Data". NoData Custom View (listview.xml):<?xml version="1.0"encoding="Utf-8"? ><linearlayout xmlns:android="http://schemas.android.com/apk/res/android"android:orientation="Vertical"Android:layout_width="fill_parent"Android:layout_height="fill_parent"> <listview android:id="@id/android:list"Android:layout_width="fill_parent"Android:layout_height="fill_parent"Android:layout_weight="1"/> <textview android:id="@id/android:empty"Android:layout_width="fill_parent"Android:layout_height="wrap_content"Android:text="No Data"Android:textcolor="#ff0000"/></linearlayout>load layout: @Override Public voidonCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);         Setcontentview (R.layout.listview); //data source not setThe Row Layout official provides a variety of listitem layout (r.layout), the following is more commonly used, more see API Doc r.layout http://androidappdocs.appspot.com/reference/android/r.layout.html:? android. R.layout.simple_list_item_1 a line text?android. R.layout.simple_list_item_2 a row of title, a row of text?android. R.layout.simple_list_item_single_choice radio button? Android. R.layout.simple_list_item_multiple_choice Multi-select button? android. r.layout.simple_list_item_checked checkbox We can customize our own layout (List_item.xml):<?xml version="1.0"encoding="Utf-8"? ><linearlayout xmlns:android="http://schemas.android.com/apk/res/android"android:orientation="Horizontal"Android:layout_width="fill_parent"Android:layout_height="fill_parent"> <imageview android:id="@+id/icon"Android:layout_width="48dip"Android:layout_height="48dip"/> <textview android:id="@+id/text"android:layout_gravity="center_vertical"Android:layout_width="0dip"Android:layout_weight="1"Android:layout_height="wrap_content"/></linearlayout>when used, the R.layout.list_item reference is OK. You can refer to http://androidappdocs.appspot.com/resources/tutorials/views/hello-listview.html. binding data can be implemented by calling Setlistadapter (ListAdapter adapter). We can implements listadapter to customize our own data source. The API contains several implements ListAdapter Adapter:baseadapter,simpleadapter (storing static data as a map), Simplecursoradapter (for the results of cursor queries) Wait a minute. Often we extends baseadapter to write our own adapter class, because the Baseadapter class is the base class for other Apdater classes. Extending the Baseadapter class generally requires overriding the following methods:intGetCount () Gets the items number of the current adapter Object GetItem (intposition) Get the item for the corresponding positionLongGetitemid (intposition) Gets the corresponding position item in the list in the row ID View getView (intposition, view Convertview, ViewGroup parent) Gets the view of the data to be displayed in the specified position For more information, you can view the method of inheriting android.widget.Adapter for the Baseadapter class, and sometimes you need to override the ListAdapter Boolean isenabled (intposition) to achieve some effect. Let's look at some examples of binding data:1. Using Arrayadapter Public voidonCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); //reference to the Arrayadapter constructorSetlistadapter (NewArrayadapter<string> ( This, Android.         R.layout.simple_list_item_1, mstrings)); //entering letters on the ListView will automatically filter out items that begin with this contentGetlistview (). settextfilterenabled (true); }PrivateString[] Mstrings = {"A","Android","Robot","Google"}; Arrayadapter Arrayadapter2. Use Simplecursoradapter This is the sample inside the List3 example, by reading the address Book Android.provider.Contacts.Phones information, displayed. protected voidonCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); //Get a cursor with all phonesCursor C = getcontentresolver (). Query (Phones.content_uri,NULL,NULL,NULL,NULL);                  Startmanagingcursor (c); //Map Cursor columns to defined in Simple_list_item_2.xmlListAdapter adapter =NewSimplecursoradapter ( This, Android. R.layout.simple_list_item_2, C,Newstring[] {phones.name, phones.number},New int[] {Android. R.id.text1, Android.         R.ID.TEXT2}); Setlistadapter (adapter); }     3. ListItem as a radio button Public voidonCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); //reference to the Arrayadapter constructorSetlistadapter (NewArrayadapter<string> ( This, Android.         R.layout.simple_list_item_single_choice, mstrings)); Final ListView ListView=Getlistview (); Listview.setitemscanfocus (false); Listview.setchoicemode (Listview.choice_mode_single); //Set the radio mode}PrivateString[] Mstrings = {"A","Android","Robot","Google"};singliechoice Singliechoice4. ListItem for the multi-select button to set the example 3 to Android. R.layout.simple_list_item_multiple_choice and select Mode Listview.choice_mode_multiple. For more examples, refer to the official sample, which lists examples of the relevant list: List1-Use Arrayadapter and settextfilterenabled (true) List2-use Simplecursoradapter to read contacts People.namelist3-use Simplecursoradapter to read address book phones, two lines display ItemList4-use custom adapter and custom ItemViewList5-ListView with separator, with custom adapter, overriding the Boolean isenabled (intposition) List6-use custom adapter and custom Itemview to stretch hidden content List7-use Simplecursoradapter to read data List8-demonstrate the use of Setemptyview effects List9-involving OnScrollListenerList10-ListItem is a radio button List11-ListItem is a multi-select button List12-ListItemList13 can be added dynamically-How to speed up the operation display, during scrolls or flings when List14-How to write an efficient list Adapter which List14, the official told us: to work efficiently the Adapter implemented here uses two techniques:*-it reuses the Convertview passed to GetView () to avoid inflating View when it isNot necessary*-it uses the Viewholder pattern to avoid calling Findviewbyid () when it isNot necessary also tells us the role of the Viewholder class:* The Viewholder pattern consistsinchStoring a data structureinchThe tag of the view returned by*GetView (). This data structures contains references to the views we want to bind data to, thus* Avoiding calls to Findviewbyid () every time GetView () isinvoked. In the above example, we learned that using a custom adapter, when the data changes need to call notifydatasetchanged () to refresh the ListView, but in the List12 example, using Arrayadapter did not call this method, And then write your own code, in the exception that occurs to understand that Baseadapter,arrayadapter will call their own notifydatasetchanged ().  You can see the examples in the following article, "Learning about Android Threading"! The event response of the ListView usually we respond to the ListItem click event:protected voidOnlistitemclick (ListView L, View V,intPositionLongID), in this not detailed, understand the function inside the parameter meaning is OK. This article is from the "Learn Android" blog, make sure to keep this source http://android.blog.51cto.com/268543/336162

Android UI Learning-ListView (Android. R.layout.simple_list_item_1 is a thing)

Contact Us

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

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.