. Net programmers play Android development-ListView Click Event, android-listview
Public class ListViewClickActivity extends Activity {
Private ListView lv;
SimpleAdapter adp; // define the adapter
Private List <map <string, object> mapList; // defines the data source.
Protected void onCreate (Bundle savedInstanceState)
{
// TODO Auto-generated method stub
Super. onCreate (savedInstanceState );
SetContentView (R. layout. listviewclicklay );
Lv = (ListView) findViewById (R. id. listView1 );
MapList = new ArrayList <map <string, object> ();
For (int I = 0; I <10; I ++)
{
Map <string, object> map = new HashMap <string, object> ();
Map. put (code, encoding: 1000 + I );
Map. put (name, name: Ipad + I );
Map. put (price, price: + I );
Map. put (model, unit: + I );
MapList. add (map );
}
Adp = new SimpleAdapter (ListViewClickActivity. this, mapList, R. layout. listdetail, new String [] {code, name, price, model}, new int [] {R. id. tvcode, R. id. tvname, R. id. tvprice, R. id. tvmodel });
Lv. setAdapter (adp );
Lv. setOnItemClickListener (new OnItemClickListener (){
@ Override
Public void onItemClick (AdapterView <! --? --> Arg0, View arg1, int arg2,
Long arg3 ){
TextView tname = (TextView) arg1.findViewById (R. id. tvname); // name
TextView tmodel = (TextView) arg1.findViewById (R. id. tvmodel); // Specification
TextView tprice = (TextView) arg1.findViewById (R. id. tvprice); // unit price
TextView tcode = (TextView) arg1.findViewById (R. id. tvcode); // code
Toast. makeText (getApplicationContext (), current product name: + tname. getText () +, encoding: + tcode. getText (), 30). show ();
}
});
}
} </String, object> </string, object> </map <string, object> </map <string, object>