The ListView control describes: used to pass data from a database or data in a network through a list of theListView uses the MVC pattern to separate the front-end display from the back-end data. that is, theListView control does not directly use Listview.add when loading data or a similar method of adding data, you need to specify aAdapterobject. The object is equivalent toMVCin the patternC(Controller),ListViewequivalentMVCin the patternV(view), for explicitdisplay data. To beListViewproviding the dataListor array equivalentMVCin the patternM(model) Gets the data that needs to be displayed in the ListView control through the Adapter object, in the Genesis Adapter object, you need to specify the data to display (Listor Array object), so the data to be displayedwith theListViewbetween throughAdapterobjects are connected and are independent of each other, i.e.,ListViewonly know that the data displayed is fromAdapter, it is not known that the data came fromListor an array.
1.ListView Application Example (here is the operation of the ListView object in Java code ):
public class Mylistactivity extends listactivity{
Defines an array of strings that represent our data sources
private static final string[] countries=new string[]{"China", "France", "United States", "Germany", "Japan", "North Korea", "India"};
@Override
protected void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
To show the data we need to show on the ListView.
Setlistadapter (New arrayadapter<string> (this, r.layout.list_item,countries));
Get the ListView object by using the Getlistview () method
ListView Lv=getlistview ();
Lv.settextfilterenabled (TRUE);
LOG.E ("Mylistactivity", "ListView Interface produced");
Lv.setonitemclicklistener (New Onitemclicklistener () {
@Override
public void Onitemclick (adapterview<?> parent, view view,
int position, long ID) {
Gets the option information in the ListView selected by the user through the GetText () method
LOG.E ("mylistactivity", "list options are clicked, see what happens");
Toast.maketext (Getapplicationcontext (), ((TextView) view). GetText (), Toast.length_long). Show ();
}
});
}
}
The list item XML file is as follows:
<?xml version= "1.0" encoding= "Utf-8"?>
<textview xmlns:android= "Http://schemas.android.com/apk/res/android"
Android:layout_width= "Fill_parent"
android:layout_height= "Fill_parent"
Android:padding= "10DP"
Android:textsize= "16SP" >
</TextView>
Java code ①:
public class MyActivity extends activity{
Private ListView ListView;
Private arrayadapter<string> adapter;
Define a data source
Private list<string> Data=null;
@Override
protected void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Setcontentview (R.layout.activity_main);
Get Data source
Data=mydatasource.getdatasource ();
Adapter=new arrayadapter<string> (this, r.layout.list_item, data);
listview= (ListView) Findviewbyid (R.id.listview);
Display the data source through adapter to the ListView view
Listview.setadapter (adapter);
}
}
Java code ②:
public class myDataSource {
Public myDataSource () {
Super ();
}
public static list<string> Getdatasource () {
List<string> list=new arraylist<string> ();
List.add ("Beijing");
List.add ("Shanghai");
List.add ("Guangzhou");
List.add ("Hubei");
List.add ("Hunan");
List.add ("Shenzhen");
List.add ("Xian");
return list;
}
}
Layout file activity_main.xml code:
<relativelayout xmlns:android= "Http://schemas.android.com/apk/res/android"
Android:layout_width= "Match_parent"
android:layout_height= "Match_parent" >
<listview
Android:id= "@+id/listview"
Android:layout_width= "Match_parent"
android:layout_height= "Match_parent"
android:text= "@string/hello_world"/>
</RelativeLayout>
List item list_item.xml code:
<?xml version= "1.0" encoding= "Utf-8"?>
<textview xmlns:android= "Http://schemas.android.com/apk/res/android"
Android:layout_width= "Fill_parent"
android:layout_height= "Fill_parent"
Android:padding= "10DP"
Android:textsize= "16SP" >
</TextView>
2.ListView Application Example:
In the above two examples we use arrayadapter(array adapter), this adapter shows a single data source, and Simpleadapter can define a variety of layouts, Can put on imageview (picture), also can put on button (buttons), CheckBox (check box), etc.;
Let's take a look at how to use Simpleadapter
①Mainactivity.java
public class Mainactivity extends Activity {
Private ListView Mlistview;
Private Simpleadapter Msimpleadapter=null;
Private list<map<string, object>> data=null;
@Override
protected void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Setcontentview (R.layout.activity_main);
mlistview= (ListView) Findviewbyid (R.id.listview);
Data=new arraylist<map<string,object>> ();
/**
* Setadapter Method:
* First parameter: is a context object;
* Second parameter: is a set, using such a form list<map<string, Object>>
* This list<e> is used to store list items in the ListView, and each element in the List<e> collection has its own characteristics
* So we accomplished the diversification of the ListView data display by means of the map collection representing the elements of list;
* Third parameter: is a layout file that is used to display the contents of each list item in the ListView
* Fourth parameter: is the array representation of the key of map<key,value> in our list<map<string, object>>;
* Fifth parameter: Make us list<map<string, the array representation of the value of map<key,value> in object>>;
*/
Map<string, object> mmap1=new hashmap<string, object> ();
Mmap1.put ("title", "I am the first feature");
Mmap1.put ("icon", R.drawable.ic_launcher);
Map<string, object> mmap2=new hashmap<string, object> ();
Mmap2.put ("title", "I am the second function");
Mmap2.put ("icon", R.drawable.ic_launcher);
Map<string, object> mmap3=new hashmap<string, object> ();
Mmap3.put ("title", "I am a third function");
Mmap3.put ("icon", R.drawable.ic_launcher);
Data.add (MMAP1);
Data.add (MMAP2);
Data.add (MMAP3);
Mlistview.setadapter (New Simpleadapter (this, data, R.layout.list_item,
New string[]{"title", "Icon"}, new Int[]{r.id.tv,r.id.iv});
}
}
②Activity_main.xml
<relativelayout xmlns:android= "Http://schemas.android.com/apk/res/android"
Android:layout_width= "Match_parent"
android:layout_height= "Match_parent" >
<listview
Android:id= "@+id/listview"
Android:layout_width= "Match_parent"
android:layout_height= "Match_parent"/>
</RelativeLayout>
③List_item.xml
<linearlayout xmlns:android= "Http://schemas.android.com/apk/res/android"
Android:layout_width= "Match_parent"
android:layout_height= "Match_parent" >
<imageview
Android:id= "@+id/iv"
Android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content"/>
<textview
Android:id= "@+id/tv"
Android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content"/>
</LinearLayout>
3. Custom adapter: In theAndroid interface sometimes need to display a slightly more complex interface, we need to define a adapter, then we must inherit Baseadapter.
Android Development Road (Listview&adapter)