A deep understanding of the ListView
Second, proficiency in the use of two adapters:arrayadapter,simpleadapter
data adapter functions:
populating complex data on a specified interface ( array, collection, linked list, database ... ) , which is the bridge connecting the data source and the view interface.
(1) Array adapter:arrayadapter
for binding single-frame data ( data Source: Arrays, Collections ) ;
(2) Simple adapter:simpleaadpter
used to bind the data to a fixed-type complex ( data Source: A collection of only a specific generic type ) ;
implementation process: New Adapter, add data source to adapter, view load adapter;
Mainactivity file:
Package Com.example.mylistview;
Import java.util.ArrayList;
Import Java.util.HashMap;
Import java.util.List;
Import Java.util.Map;
Import android.support.v7.app.ActionBarActivity;
Import Android.os.Bundle;
Import Android.widget.ListView;
Import Android.widget.SimpleAdapter;
public class Mainactivity extends Actionbaractivity {
Private ListView ListView;
Private arrayadapter<string> Arr_adapter;
Private Simpleadapter Simp_adapter;
Private list<map<string,object>> dataList;
@Override
protected void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Setcontentview (R.layout.activity_menu);
listview= (ListView) Findviewbyid (R.id.listview);
/*
* Array Adapter:
* 0. Generate a data source
* 1. Create a new adapter and load the data source
* Three parameters of array adapter:
* Context, the layout file that corresponds to the list item loaded by the current ListView, the data source
* 2. Load adapter with view
*
* */
String []arr_data={"Ma Xuan", "Choose", "carriage", "Live", "Dragon Horse", "Tianma", "Zhao", "Zhao Chen", "True singing", "register", "Sunflower", "Xu Rongkai", "Cheriong"};
Arr_adapter=new arrayadapter<string> (this, Android. R.layout.simple_list_item_1, Arr_data);
Listview.setadapter (Arr_adapter);
/*
* Simple adapter:
* Parameters: Context
*
* Data source list<? Extends map<string,?>> a list collection of maps,
* Each map corresponds to one row in the ListView list
* The keys in each map must contain all the keys specified in the From
*
* Layout file for list items
*
* Record the key names in map
*
* ID in bound data View, corresponding to from
* */
Datalist=new arraylist<map<string,object>> ();
Simp_adapter=new Simpleadapter (this, This.getdata (), R.layout.activity_menusimp, New string[]{"pic", "text"}, new int[ ]{r.id.pic,r.id.text});
Listview.setadapter (Simp_adapter);
}
Private List<map<string,object>> GetData () {
for (int i = 0; i <; i++) {
Map<string,object> map=new hashmap<string, object> ();
Map.put ("Pic", R.drawable.ic_logo);
Map.put ("text", "Cheriong");
Datalist.add (map);
}
return dataList;
}
}
Activity_menu.xml file:
<?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:orientation= "Vertical" >
<listview
Android:id= "@+id/listview"
Android:layout_width= "Match_parent"
android:layout_height= "Wrap_content" >
</ListView>
</LinearLayout>
Activity_menusimp.xml file
<?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:orientation= "Horizontal" >
<imageview
Android:id= "@+id/pic"
Android:layout_width= "86DP"
android:layout_height= "100DP"
android:layout_marginleft= "15DP"
android:src= "@drawable/ic_logo"/>
<textview
Android:id= "@+id/text"
Android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content"
Android:text= "Demo"
Android:textcolor= "#000000"
Android:textsize= "20sp"/>
</LinearLayout>
Three, skilled use two kinds of listener:onscrolllistener,onitemclicklistener
Use of Android-listview space