Different data sources
1. String []: arrayadapter
2. List <Map <string,?> : Simpleadapter
3. Database cursor: simplecursoradapter
To use arrayadapter (array adapter), you need to put data into an array for display.
Android. R. layout. simple_list_item_1 is a layout file defined by the system. Only one line of text is displayed.
Simpleadapter can define various la S. You can put imageview, button, and checkbox)
// ================================ Simpleadapter ======== ============================
Public class list3 extends listactivity {
List <Map <string, Object> list;
@ Override
Public void oncreate (bundle savedinstancestate ){
Super. oncreate (savedinstancestate );
List = getlistforsimpleadapter (); // list is a data set.
Simpleadapter adapter = new simpleadapter (this, list,
R. layout. item,
New String [] {"bigtext", "littletext", "IMG "},
New int [] {R. Id. bigtext, R. Id. littletext, R. Id. IMG });
// You may want to put a layout file with data in this interface.
Setlistadapter (adapter );
}
Private list <Map <string, Object> getlistforsimpleadapter (){
List <Map <string, Object> List = newarraylist <Map <string, Object> (3 );
Map <string, Object> map = new hashmap <string, Object> ();
Map. Put ("bigtext", "android ");
Map. Put ("littletext", "Google phone .");
Map. Put ("IMG", R. drawable. N );
List. Add (MAP );
Map = new hashmap <string, Object> ();
Map. Put ("bigtext", "Lenovo ");
Map. Put ("littletext", "ophone ");
Map. Put ("IMG", R. drawable. O );
List. Add (MAP );
Map = new hashmap <string, Object> ();
Map. Put ("bigtext", "droid ");
Map. Put ("littletext", "Motorola ");
Map. Put ("IMG", R. drawable. Droid );
List. Add (MAP );
Return list;
}
}
// ================================ Simplecursoradapter ======== ================
Note: simplecursoradapter is related to the database, and the auto-increment ID required by the SQLite database must be in the _ id format.
Public class list2 extends activity {
Private listview;
@ Override
Protected void oncreate (bundle savedinstancestate ){
Super. oncreate (savedinstancestate );
Listview = new listview (this );
Cursorc = getcontentresolver (). Query (people. content_uri,
Null, null );
Startmanagingcursor (C );
Listadapter adapter = new simplecursoradapter (this,
Android. R. layout. simple_list_item_1,
C,
New String [] {People. name },
New int [] {Android. R. Id. text1 });
Listview. setadapter (adapter );
Setcontentview (listview );
}
}
// ================================== Arrayadapter ======================== ============================
Public class list1 extends activity {
Private listveiw listview;
@ Override
Public void oncreate (bundle savedinstancestate ){
Super. oncreate (savedinstancestate );
Listview = new listveiw (this );
Listview. setadapter (New arrayadapter <string> (this,
Android. R. layout. simple_list_item_1, mstrings ));
Setcontentvieww (listview );
}
Private string [] mstrings = {
"Abbaye de Belloc", "abbaye du Mont des cats ",
"Acorn", "adelost", "affidelice au Chablis ",
"Aisy censid", "allgauer emmentaler", "alverca ",
"Ami du Chambertin", "anejo enchilado", "anneau
"Aragon", "Ardi gasna", "ardrahan", "Armenian
"Asadero", "Asiago", "aubisque Pyrenees", "autun ",
"Babybel", "baguette laonnaise", "bakers", "Bal "};
}