During Android app development, the Listview is a frequently used data presentation control that is often used to display data in the form of a list.
Suppose that only displaying data tends to be very monotonous. Very often, depending on the need to define different item background options. For example, define the title of the data. The table header or interval displays the ListView item background content.
The context of the view, and now uses a Weatherdemo model to analyze the correlation usage of the ListView.
Implementation idea: Define a different background for the ListView, first define the two different XML layout files (city_item.xml,content_item.xml) for the title and the table content.
When populating the data, it is often used to construct a adapter data type, infer different data types based on the constructed data type, and use the Layoutinflater class to populate different layout files to return different view.
Constructs the Weatheradapter class inherits the Baseadapter and implements the related interface function, realizes the filling to the list data.
The structure of the data is more critical, it can control the related title and content of the relationship.
Weatheradapter classes are mostly implemented and populated with the corresponding data. Displays the relevant data.
program code such as the following:
public class Weatheradapter extends Baseadapter {//Data source private list
Mainactivity.java
public class Mainactivity extends Activity {@Overridepublic void onCreate (Bundle savedinstancestate) {super.oncreate ( Savedinstancestate); Setcontentview (R.layout.main); ListView list = (ListView) Findviewbyid (R.id.list_items); arraylist
Three XML layout files:
City_item.xml
<?xml version= "1.0" encoding= "Utf-8"?><!--Items Option--><relativelayout xmlns:android= "Http://schemas.android.com/apk/res/android" Android:id = "@+id/relativelayout" android:layout_width= "fill_parent" android:layout_height= "Wrap_content" Android:background = "#9ACD32" android:paddingbottom= "4dip" android:paddingleft= "12dip" android:paddingright= "12dip" > <imag Eview android:id= "@+id/weather_image" android:layout_width= "wrap_content" android:layout_height= "wrap _content "android:layout_alignparentright=" true "android:paddingtop=" 4dip "android:contentdescription = "@string/weather_image"/> <textview android:id= "@+id/city" android:layout_width= "Fill_parent" android:layout_height= "Wrap_content" android:paddingtop= "6dip" android:textcolor= "@color/black" an Droid:textsize= "18SP"/></relativelayout>
Content_item.xml
<?xml version= "1.0" encoding= "Utf-8"?><!--Items Option--><relativelayout xmlns:android= "Http://schemas.android.com/apk/res/android" Android:id = "@+id/relativelayout02" android:layout_width= "fill_parent" android:layout_height= "Wrap_content" Android:backgrou nd= "@color/white" android:paddingbottom= "4dip" android:paddingleft= "12dip" android:paddingright= "12dip" ><! --Ability to delete <imageview android:id= "@+id/image" android:layout_width= "Wrap_content" Android:layout_ height= "Wrap_content" android:layout_alignparentright= "true" android:paddingtop= "22dip" Android:conte ntdescription= "@string/image"/>--and <textview android:id= "@+id/content" android:layout_width= "f Ill_parent "android:layout_height=" wrap_content "android:paddingtop=" 6dip "android:textsize=" 15SP "/&G T <textview android:id= "@+id/date" android:layout_width= "fill_parent" android:layout_height= "Wrap_co Ntent "Android:layout_below= "@+id/content" android:paddingright= "20dip"/></relativelayout>
Main.xml<?xml version= "1.0" encoding= "Utf-8"? ><linearlayout xmlns:android= "http://schemas.android.com/apk/res/ Android " android:layout_width=" fill_parent " android:layout_height=" fill_parent " android:o rientation= "vertical" > <listview android:id= "@+id/list_items" android:layout_width= "Wrap_ Content " android:layout_height=" wrap_content " android:divider=" @color/white " android:dividerheight = "1dip" > </ListView></LinearLayout>
Source code: http://download.csdn.net/detail/yangweixing10/7214057
Android Style Layout---> ListView (attach source code)