. Net programmers play Android development-ListView Click Event
As we all know, ListView is used to display the data list. Each list consists of list items. If we click to select a list, what should I do if I want to get the details in the list or open a new window to pass the list information? This section demonstrates the click event of ListView. Through this section, we will have a deeper understanding of ListVIEW. Let's take a look at it first.
The Demo code is as follows:
Main layout File
List item layout File
Background code
Public class ListViewClickActivity extends Activity {private ListView lv; SimpleAdapter adp; // defines the private List of the adapter.
> MapList; // define the data source protected void onCreate (Bundle savedInstanceState) {// TODO Auto-generated method stubsuper. onCreate (savedInstanceState); setContentView (R. layout. listviewclicklay); lv = (ListView) findViewById (R. id. listView1); mapList = new ArrayList
> (); For (int I = 0; I <10; I ++) {Map
Map = new HashMap
(); 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); // type 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 ();}});}}