Don't say much, just look at the code: Create a person entity object first.
Import Java.io.serializable;public class Person implements serializable{private static final long serialversionuid=1l; Private integer id;private string name;private string phone;private integer amount;public integer getId () {return ID;} public void SetId (Integer id) {this.id = ID;} Public String GetName () {return name;} public void SetName (String name) {this.name = name;} Public String Getphone () {return phone;} public void Setphone (String phone) {this.phone = phone;} Public Integer Getamount () {return amount;} public void Setamount (Integer amount) {this.amount = amount;} Public person (string name, string phone, Integer amount) {super (); this.name = Name;this.phone = Phone;this.amount = Amount ;} public person (int id,string name, String phone, Integer amount) {super (); this.id=id;this.name = Name;this.phone = phone;th Is.amount = amount;} <span style= "font-family:arial, Helvetica, Sans-serif;" ></span>
Import Android.content.context;import Android.database.sqlite.sqlitedatabase;import Android.database.sqlite.sqlitedatabase.cursorfactory;import Android.database.sqlite.sqliteopenhelper;import Android.util.log;public class Dbopenhelper extends Sqliteopenhelper {private static final String tag= "Dbsqlitehelper"; private static final String name= "bobge.db";p rivate static final int version=1;public Dbopenhelper (context context) {Supe R (context, name, null, version); LOG.V (Tag, "constructor");} @Overridepublic void OnCreate (Sqlitedatabase db) {db.execsql ("CREATE TABLE person (ID integer primary key autoincrement, Name varchar ((), phone varchar (), amount integer) "); LOG.V (tag, "Database creation executes once"); } @Overridepublic void Onupgrade (sqlitedatabase db, int oldversion, int newversion) {db.execsql ("DROP TABLE IF EXISTS person "); OnCreate (db);}}
Create a Persondao class to enable adding and removing changes to person entity data. Both the Getwritabledatabase () and Getreadabledatabase () methods can obtain a sqlitedatabase instance that is used to manipulate the database. However, the Getwritabledatabase () method opens the database in read-write mode, and once the database is full of disk space, the database is read-only and cannot be written, and if you open the database using Getwritabledatabase (), an error occurs. The Getreadabledatabase () method opens the database in read-write mode, and if the database has a full disk space, it will fail to open and will continue to attempt to open the database as read-only when the open fails.
Note: getwritabledatabase (), getreadabledatabase the difference is when the database is full, call the former will be error, call the latter will not, so if not update the database, it is best to call the latter to obtain a database connection. <span style= "font-family:arial, Helvetica, Sans-serif;" >import java.util.arraylist;</span>
Import Java.util.list;import Android.content.context;import Android.database.cursor;import Android.database.sqlite.sqlitedatabase;import Dbsqliteopenhelper.db.domain.person;import Dbsqliteopenhelper.service.dbopenhelper;public class Persondao {private Dbopenhelper dbopenhelper;public PersonDao ( Context context) {This.dbopenhelper = new Dbopenhelper (context);} public void Save (person person) {Sqlitedatabase db=dbopenhelper.getwritabledatabase ();d b.execsql ("Insert to Person" ( Name,phone,amount) VALUES (?,?,?) ", New Object[]{person.getname (), Person.getphone (), Person.getamount ()}); public void Delete (Integer id) {sqlitedatabase db=dbopenhelper.getwritabledatabase ();d b.execsql (' Delete from ' Where id=? ", New Object[]{id}); public void update (person person) {Sqlitedatabase db=dbopenhelper.getwritabledatabase ();d b.execsql ("The Update person set Name=?,phone=?,amount=? Where id=? ", New Object[]{person.getname (), Person.getphone (), Person.getamount (), Person.getid ()}); Public person find (Integer ID) {SQLiTedatabase db=dbopenhelper.getreadabledatabase (); Cursor cursor=db.rawquery ("select * from person where id=?", New String[]{id.tostring ()}); if (Cursor.movetofirst ()) {int Personid=cursor.getint (Cursor.getcolumnindex ("id")); String name=cursor.getstring (Cursor.getcolumnindex ("name")); String phone=cursor.getstring (Cursor.getcolumnindex ("Phone")), int money=cursor.getint (Cursor.getcolumnindex (" Amount ")); return new person (Personid,name,phone,money);} Cursor.close (); return null;} Public list<person> getscrolldata (int offset,int maxresult) {list<person> persons=new ArrayList<Person > (); Sqlitedatabase db=dbopenhelper.getreadabledatabase (); Cursor cursor=db.rawquery ("SELECT * from person ORDER BY ID ASC limit?,?", New string[]{string.valueof (offset), string.val Ueof (Maxresult)}); while (Cursor.movetonext ()) {int Personid=cursor.getint (CURSOR.GETCOLUMNINDEX ("id")); String name=cursor.getstring (Cursor.getcolumnindex ("name")); String phone=cursor.getstring (Cursor.getcolumnindex ("Phone")); int MonEy=cursor.getint (Cursor.getcolumnindex ("Amount"));p Ersons.add (new Person (Personid,name,phone,money));} Cursor.close (); return persons;} Public long GetCount () {sqlitedatabase db=dbopenhelper.getreadabledatabase (); Cursor cursor=db.rawquery ("SELECT count (*) from person", null); Cursor.movetofirst (); Long result=cursor. Getlong (0); return result;}} <span style= "color: #ff0000;" ></span>android project, you can enter your name, phone number and deposit. Clicking the Save button allows you to save the above data to the SQLite database. and set up a display database Data button, the ability to display data into the ListView control, to implement page jumps. Import Dbsqliteopenhelper.db.dao.persondao;import Dbsqliteopenhelper.db.domain.person;import android.app.Activity ; Import Android.content.intent;import Android.content.sharedpreferences.editor;import Android.os.Bundle;import Android.view.menu;import Android.view.menuitem;import Android.view.view;import Android.view.View.OnClickListener; Import Android.widget.button;import Android.widget.edittext;import Android.widget.toast;public class MainActivity Extends Activity {Button listdate=null; @Overrideprotected void OnCreate (Bundle savedinstancestate) {super.oncreate ( Savedinstancestate); Setcontentview (R.layout.activity_main); listdate= (Button) Findviewbyid (R.id.list_show); Listdate.setonclicklistener (New Onclicklistener () {@Overridepublic void OnClick (View v) {Intent intent=new Intent (); Intent.setclass (Mainactivity.this, Secondactivity.class); startactivity (intent);}}); public void Testsave (View v) throws Exception{edittext nametext= (EditText) Findviewbyid (r.id.name); EditText phonetext= (EditText) FIndviewbyid (R.id.phone); EditText amounttext= (EditText) Findviewbyid (R.id.amount); String Name=nametext.gettext (). toString (); String Phone=phonetext.gettext (). ToString (); int Amount=integer.parseint (Amounttext.gettext (). ToString ()); Persondao personservice=new Persondao (V.getcontext ()); Person Person=new person (name,phone,amount);p ersonservice.save (person); Toast.maketext (V.getcontext (), r.string.successful, 1). Show (); @Overridepublic boolean Oncreateoptionsmenu (Menu menu) {//Inflate the menu; This adds items to the action bar if it is PR Esent.getmenuinflater (). Inflate (R.menu.main, menu); return true;} @Overridepublic boolean onoptionsitemselected (MenuItem Item) {//Handle Action Bar item clicks here. The action bar will//automatically handle clicks on the Home/up button so long//as you specify a parent activity in and RoidManifest.xml.int id = item.getitemid (); if (id = = r.id.action_settings) {return true;} return super.onoptionsitemselected (item);}}
The secondactivity is used to display the contents of the ListView control, and a return button is set to return to the previous page.
Import Java.util.arraylist;import Java.util.hashmap;import Java.util.list;import Dbsqliteopenhelper.db.dao.persondao;import Dbsqliteopenhelper.db.domain.person;import android.app.Activity; Import Android.content.intent;import Android.content.sharedpreferences.editor;import Android.os.Bundle;import Android.view.menu;import Android.view.menuitem;import Android.view.view;import Android.view.View.OnClickListener; Import Android.widget.button;import Android.widget.edittext;import Android.widget.listview;import Android.widget.simpleadapter;import Android.widget.toast;public class Secondactivity extends Activity {private List <Person> persons = new arraylist<person> (); Private ListView ListView; Private button button, @Overrideprotected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate ); Setcontentview (r.layout.activity_listshow); listview= (ListView) This.findviewbyid (R.id.listview); Show (); button = (Button) This.findviewbyid (r.id.back); Button.setonclicklistener(New Onclicklistener () {@Overridepublic void OnClick (View v) {Intent intent=new Intent (); Intent.setclass ( Secondactivity.this, Mainactivity.class); startactivity (intent);}}); private void Show () {Persondao person=new Persondao (Getapplicationcontext ());p ersons=person.getscrolldata (0, 5); listActivity_listshow.xml Sets the page that displays the ListView control (that is, the second page that jumps).
<?xml version= "1.0" encoding= "Utf-8"?> <linearlayout xmlns:android= "http://schemas.android.com/apk/res/ Android "Android:layout_width=" Fill_parent "android:layout_height=" fill_parent "android:orientation=" vertic Al "> <linearlayout android:layout_width=" match_parent "android:layout_height=" Wrap_content " android:orientation= "Horizontal" > <textview android:textsize= "22SP" Android:layout_width= "100DP" android:layout_height= "wrap_content" android:text= "@str Ing/name "/> <textview android:textsize=" 22sp "android:layout_width=" 100DP " android:layout_height= "Wrap_content" android:text= "@string/phone"/> <text View android:textsize= "22SP" android:layout_width= "100DP" android:layout_height= "WR Ap_content "Android:text= "@string/amount"/> </LinearLayout> <listview android:id= "@+id/listview" Android:layout_width= "Match_parent" android:layout_height= "wrap_content" > </ListView> <button android:layout_width= "fill_parent" android:layout_height= "Wrap_content" Android:text = "@string/back" android:id= "@+id/back"/> </LinearLayout>
activity_main.xml the main interface. Used to enter data that needs to be deposited. </pre><pre name= "code" class= "HTML" ><?xml version= "1.0" encoding= "UTF-8"?> <linearlayout xmlns: Android= "http://schemas.android.com/apk/res/android" android:orientation= "vertical" android:layout_width= "Fill_ Parent "android:layout_height=" fill_parent "> <textview android:layout_width=" fill_parent "Andro id:layout_height= "Wrap_content" android:text= "@string/name"/> <edittext android:layout_width= "fil L_parent "android:layout_height=" wrap_content "android:id=" @+id/name "/><textview Andro Id:layout_width= "Fill_parent" android:layout_height= "wrap_content" android:text= "@string/phone"/> <edittext android:layout_width= "fill_parent" android:layout_height= "wrap_content" android:id= "@+id/phone"/> ; <textview android:layout_width= "fill_parent" android:layout_height= "Wrap_content" android:text= " @string/amount "/><editText android:layout_width= "fill_parent" android:layout_height= "wrap_content" android:id= "@+id/amount"/><Li Nearlayout android:orientation= "Horizontal" android:layout_width= "Fill_parent" android:layout_height= "fill_parent "><button android:layout_width=" wrap_content "android:layout_height=" wrap_content "android:text=" @strin G/button "android:id=" @+id/button "android:onclick=" Testsave "/><button android:layout_width=" Wrap_cont Ent "android:layout_height=" wrap_content "android:text=" @string/list_show "android:id=" @+id/list_show "Android : onclick= "Listshow"/></linearlayout></linearlayout>
The item.xml is used to display field information for the ListView control.
<pre name= "code" class= "HTML" ><?xml version= "1.0" encoding= "Utf-8"?> <linearlayout xmlns:android = "Http://schemas.android.com/apk/res/android" android:layout_width= "match_parent" android:layout_ height= "Match_parent" android:orientation= "Horizontal" > <textview android:id= "@+id/name" android:layout_width= "100DP" android:layout_height= "wrap_content" android:textsize= "22SP"/> <textview android:id= "@+id/phone" android:layout_width= "100DP" android:layout_height= " Wrap_content " android:textsize=" 22sp "/> <textview android:id=" @+id/amount " android: Layout_width= "100DP" android:layout_height= "wrap_content" android:textsize= "22sp"/> </ Linearlayout>
Page String Configuration page (String.xml)
<?xml version= "1.0" encoding= "Utf-8"?><resources> <string name= "app_name" >listview application </ string> <string name= "name" > name </string> <string name= "Phone" > Phone </string> <string name= "Amount" > Amount </string> <string name= "button" > Save </string><string name= " Action_settings "> Set </string><string name=" Successful "> Saved successfully </string><string name=" List_ Show "> Show database Data </string><string name=" Back "> Return </string></resources>
Android database additions and deletions and ListView and page jump implementation