Title, this article will describe the click events of the ListView, the principles and use of simpleadapter and Arrayadapter.
1.ListView of registered click events
//Register Click event Personlistview.setonitemclicklistener (New Adapterview.onitemclicklistener () {/*** *@param Parent Current ListView *@param view represents the currently clicked entry *@param position position of current entry *ID of the entry that @param ID is currently clicked */@OverridePublicvoidOnitemclick(adapterview<?> parent, view view,int position, long id) {LOG.D (Tag, "clicked!"); /* Method 1: */TextView Tv_name = (TextView) View.findviewbyid (r.id.tv_name); Toast.maketext (myactivity. This, "name is 1:" +tv_name.gettext (), Toast.length_short). Show (); /* Method 2: */String name = Persons.get (position). GetName (); Toast.maketext (myactivity. This, "name is 2:" +name,toast.length_short). Show (); /* Method 3: */person p= (person) parent.getitematposition (position); Toast.maketext (myactivity. This, "name is 3:" +p.getname (), Toast.length_short). Show ();}});
Note: Register a Onitemclick event, Method 1: Use the view of the currently clicked entry to find the Tv_name based on the ID, and then use the toast to display the clicked content.
Method 2: Take advantage of the location of the currently clicked entry, then take the name based on the location;
Method 3:parent here represents the location of the ListView, where it gets the location of the clicked entry, takes it to the person object and displays it.
Com/amos/android_db/myactivity.java
Package com.amos.android_db;Import android.app.Activity;Import Android.os.Bundle;Import Android.util.Log;Import Android.view.LayoutInflater;Import Android.view.View;Import Android.view.ViewGroup;Import android.widget.*;Import Com.amos.android_db.dao.Person;Import Com.amos.android_db.dao.PersonDao;Import java.util.List;PublicClass MyActivityExtendsActivity {Private ListView Personlistview;private list<person> persons; Layoutinflater Inflater;//Pump String tag="Myactivity.class";@OverridePublicvoid OnCreate (Bundle savedinstancestate) {Super.oncreate (savedinstancestate);//Inflater is a system service that initializes the system service and uses Inflater to convert a layout file into an object inflater = (layoutinflater)This.getsystemservice (Layout_inflater_service); Setcontentview (R.layout.main); Persondao Persondao =New Persondao (this); persons = Persondao.findall ();//The first step is to get a reference to the ID of the component Personlistview = (ListView)This.findviewbyid (R.id.listview_show_data);//The second step, set the contents of the component to be displayed, the ListView to display the content is more complex, needs the data adapter Personlistview.setadapter (New Mylistadapter ());//Register Click event Personlistview.setonitemclicklistener (New Adapterview.onitemclicklistener () {/*** *@param Parent Current ListView *@param view represents the currently clicked entry *@param position position of current entry *ID of the entry that @param ID is currently clicked */@OverridePublicvoid Onitemclick (adapterview<?> parent, view view,int position,Long id) {LOG.D (tag,"Clicked!");/*Method 1: */TextView Tv_name = (TextView) View.findviewbyid (r.id.tv_name); Toast.maketext (myactivity.This"Name is 1:" +tv_name.gettext (), Toast.length_short). Show ();/*Method 2: */String name = Persons.get (position). GetName (); Toast.maketext (myactivity.This"Name is 2:" +name,toast.length_short). Show ();/*Method 3: */person p= (person) parent.getitematposition (position); Toast.maketext (myactivity.This"Name is 3:" +p.getname (), Toast.length_short). Show ();}});PublicClass MylistadapterExtendsBaseadapter {/*** Returns the current number of entries *@return */@OverridePublicint GetCount () {return Persons.size (); }/*** The Object object that returns the entry corresponding to the current position position *@param position *@return */@OverridePublic Object GetItem (int position) {return Persons.get (position); }/*** Returns the ID of the corresponding entry for the current position location *@param position *@return */@OverridePublicLong Getitemid (int position) {return position; }/*** Returns the specific content of an entry * calculates how many entries appear in the current interface * 1. Get the height of each TextView * 2. Get the height of the listview * 3.listview height/textview height = Get a screen to show the number of TextView * each entry in the ListView needs to be called once GetView method * How many GetView methods are called with multiple item displays on the screen *@param position *@param Convertview *@param parent *@return */@Overridepublic view getView (int position, View Convertview, ViewGroup parent) { span class= "comment" >// TextView TextView = new TextView (myactivity.this); Textview.settext (Persons.get (position). GetName () + ":" +persons.get (position). Getage ()); span class= "comment" >// return textView; View view = Inflater.inflate (R.layout.item, null); Person person = persons.get (position); TextView tv_name = (TextView) View.findviewbyid (r.id.tv_name); TextView tv_age = (TextView) View.findviewbyid (r.id.tv_age); Tv_name.settext ( "name:" + person.getname ()); Tv_age.settext ( "Age:" + person.getage ()); LOG.D ( "item:", "+position"); return view;} }}Use of 2.simpleAdapter
The second step, set the contents of the component to be displayed, the ListView to display the content is more complex, needs the data adapter//Personlistview.setadapter (New Mylistadapter ());//self-implementation baseadapter List<Map<string,string>> data = new ArrayList<Map<string,String>> (); for (person P:persons) { map< span class= "tag" ><string, string> DataValue = new Hashmap<string, string> (); Datavalue.put ("name", P.getname ()); Datavalue.put ("Age", P.getage (). toString ()); Span class= "indent" > Data.add (datavalue); } Personlistview.setadapter (New Simpleadapter ( This, data, R.layout.item, new string[]{' name ', ' age '}, new Int[]{r.id.tv_name, r.id.tv_age});
The previous adapter was inherited Baseadapter, implemented GetCount (), getItem (int position), getitemid (int position), getView (int position, View Convertview, ViewGroup Parent) method, where the Simpleadapter method is called, and the Simpleadapter implementation can also be found to inherit the Baseadapter class.
Simpleadapter (Android.content.Context Context, java.util.list<? extends Java.util.map<java.lang.string,?> > data, int resource, java.lang.string[] from, int[] to) {throw new RuntimeException ("stub!");}
The context represents the contexts, and data represents the content to be displayed, and resource represents the Id,from of the resource file, which is the key in the map, and then its ID in turn corresponds to Tv_name, and Tv_age.
Use of 3.ArrayAdapter
3) Arrayadapter//Public arrayadapter (android.content.Context Context, int resource, int Textviewresourceid, t[] objects) {throw new RuntimeException ("stub!");} new String[persons.size ()]; For (int i=0;i<persons.size (); i++) {personarray[i] = persons. Get (i). GetName (); } personlistview.setadapter (new Arrayadapter (this,r.layout.item,r.id.tv_name,personarray));
This shows only the side of the content, R.id.tv_name, here only the name, Arrayadapter suitable for only one column of the content of the data display.
This article source: https://github.com/amosli/android_basic/tree/android_db
Reference: Http://www.tuicool.com/articles/Eb6773B