Then, in the "Android database Transaction" above, insert 50 data into the person data table
Public voidTestadd ()throwsException {Persondao dao=NewPersondao (GetContext ()); //. Add ("Wangwu", "123", 50000); //Dao.add ("Zhangsan", "234", 17000); intNumber = 857600001; Random Random=NewRandom (); for(inti = 0; I < 50; i++) {Dao.add ("Wuyudong" + I, long.tostring (number +i), Random.nextint (5000)); } }General method Display Data
Instead of using Listviewview, the program is used to render the data in the data table, with the following code:
Packagecom.wuyudong.db;Importjava.util.List;ImportCom.wuyudong.db.dao.PersonDao;ImportCom.wuyudong.db.domain.Person;ImportAndroid.os.Bundle;ImportAndroid.text.style.LeadingMarginSpan;Importandroid.widget.LinearLayout;ImportAndroid.widget.TextView;Importandroid.app.Activity;ImportAndroid.graphics.Color; Public classMainactivityextendsActivity {@Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); Setcontentview (R.layout.activity_main); LinearLayout Ll_root=(LinearLayout) Findviewbyid (r.id.ll_root); Persondao DAO=NewPersondao ( This); List<Person> persons =Dao.findall (); for(person person:persons) {String info=person.tostring (); TextView TV=NewTextView ( This); Tv.settextsize (20); Tv.settextcolor (Color.Black); Tv.settext (info); Ll_root.addview (TV); } }}
The code in Activity_main.xml is as follows:
<ScrollViewxmlns:android= "Http://schemas.android.com/apk/res/android"Xmlns:tools= "Http://schemas.android.com/tools"Android:layout_width= "Match_parent"Android:layout_height= "Match_parent" > <LinearLayoutAndroid:id= "@+id/ll_root"Android:layout_width= "Match_parent"Android:layout_height= "Match_parent"android:orientation= "vertical"Tools:context=". Mainactivity " > </LinearLayout></ScrollView>
Running results such as:
Displaying Data using the ListView
Data Display (ListView)
Requirements: The contents of the database are displayed on the interface
Conform to the MVC model
M:model data Model--person
V:view View-ListView
C:controller Controller--adapter Data adapter
The following uses Listviewview to implement this function
Modify the code in Activity_main.xml as follows
<LinearLayoutxmlns:android= "Http://schemas.android.com/apk/res/android"Xmlns:tools= "Http://schemas.android.com/tools"Android:id= "@+id/ll_root"Android:layout_width= "Match_parent"Android:layout_height= "Match_parent"android:orientation= "vertical"Tools:context=". Mainactivity " > <ListViewAndroid:id= "@+id/lv"Android:layout_width= "Match_parent"Android:layout_height= "Match_parent" > </ListView></LinearLayout>
The interface is as follows:
The code is as follows:
Packagecom.wuyudong.db;Importjava.util.List;ImportCom.wuyudong.db.dao.PersonDao;ImportCom.wuyudong.db.domain.Person;ImportAndroid.os.Bundle;ImportAndroid.text.style.LeadingMarginSpan;ImportAndroid.util.Log;ImportAndroid.view.View;ImportAndroid.view.ViewGroup;ImportAndroid.widget.BaseAdapter;Importandroid.widget.LinearLayout;ImportAndroid.widget.ListView;ImportAndroid.widget.TextView;Importandroid.app.Activity;ImportAndroid.graphics.Color; Public classMainactivityextendsActivity {PrivateListView LV; PrivateList<person>persons; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); Setcontentview (R.layout.activity_main); Persondao DAO=NewPersondao ( This); Persons=Dao.findall (); LV=(ListView) Findviewbyid (r.id.lv); Lv.setadapter (NewMyadapter ()); } //default implementation classes such as: Simplexxx, basexxx, defaultxxx Private classMyadapterextendsBaseadapter {Private Static FinalString TAG = "Myadapter"; /*** Control The total number of items in the ListView*/@Override Public intGetCount () {returnPersons.size ();//number of entries = = size of the collection} @Override PublicObject GetItem (intposition) { //TODO auto-generated Method Stub return NULL; } @Override Public LongGetitemid (intposition) { //TODO auto-generated Method Stub return0; } @Override PublicView GetView (intposition, View Convertview, ViewGroup parent) {log.i (TAG,"Return to the View object, location:" +position); TextView TV=NewTextView (Getapplicationcontext ()); Tv.settextsize (20); Tv.settextcolor (Color.Black); //get the Person object that corresponds to a locationPerson person =Persons.get (position); Tv.settext (Person.tostring ()); returnTV; } }}
The results of the operation are as follows:
Use the Logcat filter to view:
Show only 5 locations, indicating that the phone screen can only display so much, if you swipe the phone screen, the number of locations will be more and more
Android Listviewview Getting Started