In Android, the use of listview is more complex, that is, to configure its Adapter. There are several kinds of adapters, including arrayadapter and simpleadapter. First, you must generate a listview (of course, you can use listactivity, this activity integrates the listview) and uses the adapter to set the display data and layout of the listview, and then responds to the onitemclick event, or rewrite the onlistitemclick RESPONSE event function in the listactivity. The following code demonstrates the use of listactivity:
Import java. util. arraylist;
Import java. util. hashmap;
Import java. util. Map;
Import Android. App. listactivity;
Import Android. OS. Bundle;
Import Android. View. view;
Import Android. widget. listview;
Import Android. widget. simpleadapter;
Public class test extends listactivity {
@ Override
Protected void onlistitemclick (listview L, view V, int position, long ID ){
// Todo auto-generated method stub
Super. onlistitemclick (L, V, position, ID );
This. settitle (this. mmodeldata. Get (position). Get ("type"). tostring ());
}
Simpleadapter adapter = NULL;
Private arraylist <Map <string, Object> mmodeldata = NULL;
/** Called when the activity is first created .*/
@ Override
Public void oncreate (bundle savedinstancestate ){
Super. oncreate (savedinstancestate );
Initmodeldata ();
Adapter = new simpleadapter (this, mmodeldata, android. r. layout. two_line_list_item, new string [] {"name"}, new int [] {android. r. id. text1 });
This. setlistadapter (adapter );
// Setcontentview (R. layout. Main );
}
Public void initmodeldata ()
{
Mmodeldata = new arraylist <Map <string, Object> ();
Map <string, Object> item = new hashmap <string, Object> ();
Item. Put ("name", "Linux"); item. Put ("type", "OS ");
Mmodeldata. Add (item );
Map <string, Object> item2 = new hashmap <string, Object> ();
Item2.put ("name", "android"); item2.put ("type", "Platform ");
Mmodeldata. Add (item2 );
Map <string, Object> item3 = new hashmap <string, Object> ();
Item3.put ("name", "tomato"); item3.put ("type", "Fruit ");
Mmodeldata. Add (item3 );
}
}
The following code shows how to use listview + activity:
Import Android. App. activity;
Import Android. OS. Bundle;
Import Android. View. view;
Import Android. widget. adapterview;
Import Android. widget. arrayadapter;
Import Android. widget. listview;
Import Android. widget. adapterview. onitemclicklistener;
Public class teststringlist extends activity implements onitemclicklistener {
@ Override
Public void onitemclick (adapterview <?> Arg0, view arg1, int arg2, long arg3 ){
This. settitle ("You click item:" + String. valueof (arg2 ));
}
Private listview mlistview = NULL;
@ Override
Protected void oncreate (bundle savedinstancestate ){
// Todo auto-generated method stub
Super. oncreate (savedinstancestate );
Mlistview = new listview (this );
Mlistview. setonitemclicklistener (this );
Arrayadapter <string> adapter = new arrayadapter <string> (this, Android. R. layout. simple_list_item_checked,
New String [] {"itema", "itemb", "itemc "});
Mlistview. setadapter (adapter );
This. setcontentview (mlistview );
}
}