Code:
Import android.app.Activity;
Import Android.os.Bundle;
Import Android.widget.ListView;
Import Android.widget.SimpleAdapter;
Import java.util.*;
public class Mainactivity extends Activity {
Private string[] Names=new string[]{
"Tiger Head", "Get Jade", "Li Qingzhao", "Li Bai"};
Private string[] Descs=new string[]{
"Lovely Child", "a girl who is good at music", "a woman who specializes in literature", "Romantic poet"
};
Private int[] Imageids=new int[]{
R.drawable.tiger,r.drawable.nongyu,r.drawable.qingzhao,r.drawable.libai
};
@Override
public void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Setcontentview (R.layout.activity_main);
List<map<string,object>> listitems=new arraylist<map<string,object>> ();
/**
* Create a list collection, the elements in the list collection are map.list can be understood as linked lists, the list of elements is the map class, map is a string to object mapping.
* ArrayList is a list, it inherits from the list, has all the features of the list, and it is no longer an abstract class, it is stored in continuous storage, this is one of his implementation method.
* There is also a linkedlist is also a list of one, it also inherits from the list, it is not stored in a continuous storage, but through the first I find the first i+1 (if any).
* When you just need to use the list of features, do not pay attention to whether it is ArrayList or LinkedList.
* So the definition of the time is the use of the list and the new entity when the use of ArrayList, this is allowed.
* Only the consequence of this is that ListItems is ArrayList but can not use ArrayList and the list does not have the characteristics.
* HashMap is a map, he has an algorithm that allows it to quickly find value by key.
* It's easy to think that list is looking for objects by subscript and map is looking for objects by key,
*/
for (int i=0;i<names.length;i++) {
Map<string,object> listitem=new hashmap<string,object> ();
Listitem.put ("header", Imageids[i]);
Listitem.put ("PersonName", Names[i]);
Listitem.put ("desc", Descs[i]);
Listitems.add (ListItem);
}
Simpleadapter simpleadapter=new Simpleadapter (This,listitems,r.layout.simple_item,
New string[]{"PersonName", "header", "Desc"},
New Int[]{r.id.name,r.id.header,r.id.desc});
/**
* A simpleadapter is created and requires five parameters:
* Second parameter: should be a list<? Extends a collection object of type map<string,?>> that generates a list item for each Map<string,?> object in the collection
* Third parameter: Specifies the ID of an interface layout, which means that the file is used as a list item component
* Fourth parameter: The parameter should be a string[] type parameter, which determines which key in the Map<string,?> object to extract the value corresponding to the list item
* Fifth parameter: This parameter should be a int[] type parameter that determines which components are populated
*/
ListView list= (ListView) Findviewbyid (R.id.mylist);
List.setadapter (Simpleadapter);
/**
* Add listeners for click events
*/
List.setonitemclicklistener (New Adapterview.onitemclicklistener () {
This method is fired when item position is clicked
@Override
/**
* The parent corresponds to a pointer to the ListView list adapter that can be used to get everything in the list.
* View is a click on an item to get the click of the control ID after the action control
* position is the location of the clicked item in the list adapter (when the ListView is generated, the adapter makes the item one by one, and then puts them in the order of the queue, and puts them in the ListView, meaning that this item is done by number position)
* ID is the location of the first few lines of the clicked item in the ListView, most of the time the value of position and ID is the same
*/
public void Onitemclick (adapterview<?> parent, view view, int position, long ID) {
System.out.println (Names[position] + "clicked");
}
});
/**
* Add listeners for selected events
*/
List.setonitemselectedlistener (New Adapterview.onitemselectedlistener () {
The method is fired when item position is selected
@Override
public void onitemselected (adapterview<?> parent, view view, int position, long ID) {
System.out.println (Names[position] + "selected");
}
@Override
public void onnothingselected (adapterview<?> parent) {
}
});
}
}
Android in Simpleadapter