This article describes the use of the ListView and Inflater, which will be followed by a previous article.
A. What is a ListView?
in Android Development , the ListView is a more commonly used control, and the ListView control can display items in four different views,1. Large (standard) icons 2. Small Icons 3. List 4. Reports, More commonly used is the form of a list. ListItem objects can contain text and pictures. However, to use a picture you must refer to the ImageList control through the Icons and Smallicons properties.
This article will follow the previous article to display the data of the SQLite database as a list.
two.
Figure 1:
Figure 2:
Figure 3:
Figure 4:
Figure 5:
three. Implement
/com/amos/android_db/myactivity.java
Packagecom.amos.android_db;Importandroid.app.Activity;ImportAndroid.os.Bundle;ImportAndroid.util.Log;ImportAndroid.view.LayoutInflater;ImportAndroid.view.View;ImportAndroid.view.ViewGroup;ImportAndroid.widget.BaseAdapter;ImportAndroid.widget.ListView;ImportAndroid.widget.TextView;ImportCom.amos.android_db.dao.Person;ImportCom.amos.android_db.dao.PersonDao;Importjava.util.List; Public classMyActivityextendsActivity {PrivateListView Personlistview; PrivateList<person>persons; Layoutinflater Inflater;//Pump@Override Public voidonCreate (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 objectInflater = (layoutinflater) This. Getsystemservice (Layout_inflater_service); Setcontentview (R.layout.main); Persondao Persondao=NewPersondao ( This); Persons=Persondao.findall (); //The first step is to get a reference to the ID of the componentPersonlistview = (ListView) This. Findviewbyid (R.id.listview_show_data); //The second step is to set the contents of the component to be displayed, the content of the ListView is more complex, the adapter that needs the dataPersonlistview.setadapter (NewMylistadapter ()); } Public classMylistadapterextendsBaseadapter {/*** Returns the current number of entries *@return */@Override Public intGetCount () {returnpersons.size (); } /*** The Object object that returns the entry corresponding to the current position position *@paramposition *@return */@Override PublicObject GetItem (intposition) { returnPersons.get (position); } /*** Returns the ID of the corresponding entry for the current position location *@paramposition *@return */@Override Public LongGetitemid (intposition) { returnposition; } /*** 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 = The number of TextView to be displayed on a screen * the display of each entry in the ListView needs to be called once GetView method * How many items will be called if there is more than one item displayed on the screen Getv Iew's Method * *@paramposition *@paramConvertview *@paramParent *@return */@Override PublicView GetView (intposition, View Convertview, ViewGroup parent) {//TextView TextView = new TextView (myactivity.this);//Figure 1//Textview.settext (persons.get (position). GetName () + ":" +persons.get (position). Getage ());//return textView;View View= Inflater.inflate (R.layout.item,NULL); //Figure 2-Figure 5 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); returnview; } }}
Note: This is the entry for the program, first initializes the system service (Inflater), then gets a reference to the ListView component and sets the component adapter and the content to be displayed.
ListView Displays the required three elements
- Listveiw The view used to display the list.
- The adapter is used to map data to mediations on the ListView.
- The data is specific to the string, picture, or base component that will be mapped.
Note: The relevant code of the pump is removed to get the results of Figure 1, figure 2--Figure 5 is added to the layout file effect , Figure 4, Figure 5 is the new add some data into the database , Figure 4 and Figure 2 is the content of Logcat, It is intended to indicate that its database is dynamically loaded at each flip.
Item.xml
<?XML version= "1.0" encoding= "Utf-8"?><Relativelayoutxmlns:android= "Http://schemas.android.com/apk/res/android"Android:layout_width= "Fill_parent"Android:layout_height= "Fill_parent"Android:id= "@+id/item" > <TextViewAndroid:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:layout_alignparentleft= "true"Android:id= "@+id/tv_name" /> <TextViewAndroid:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:layout_alignparentright= "true"Android:id= "@+id/tv_age" /></Relativelayout>
Main.xml
<?XML version= "1.0" encoding= "Utf-8"?><LinearLayoutxmlns:android= "Http://schemas.android.com/apk/res/android"android:orientation= "vertical"android:gravity= "Center_horizontal"Android:layout_width= "Fill_parent"Android:layout_height= "Fill_parent" > <TextViewAndroid:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"android:textsize= "20SP"Android:textcolor= "#fe824f"Android:text= "Show all account information below" /> <ListViewAndroid:layout_width= "Fill_parent"Android:layout_height= "Fill_parent"Android:id= "@+id/listview_show_data" > </ListView></LinearLayout>
This article source:https://github.com/amosli/android_basic/tree/android_db