Mobile Contacts Add, view

Source: Internet
Author: User

1. Create the Databasehelper class with the Sqliteopenhelper object to create a database of contacts.

1) Declare a static final object Databasename= "Csdn.db" as the database csdn, declaring a static integer databaseversion;

private static final String databasename= "csdn.db";p rivate static int databaseversion=1;
Public Databasehelper (Context context) {super (context,databasename,null,databaseversion);}
<span style= "FONT-SIZE:18PX;" > Note:</span> each time you use the Onupgrade () method to upgrade the version databaseversion to add 1.

2) Create a contact's database table in the OnCreate () method using the Execsql () mode

@Overridepublic void OnCreate (Sqlitedatabase db) {db.execsql ("CREATE TABLE Users (UserID integer primary key Autoincrement,username varchar (a), userage integer,usersalary double,userphone varchar (12));}

3) If you need to change the database list, call the Onupgrade () method when upgrading the database version

@Overridepublic void Onupgrade (sqlitedatabase db, int oldversion, int newversion) {System.out.println ("----------upgraded ........");}

2. Change of the contact person's additions and deletions:

Note: creating, upgrading, adding, modifying, and deleting content in the database uses the Execsql () method, while the Rawquery () method is used to locate data objects in the database.

The dependent object private Databasehelper databasehelper;//is instantiated through the constructor public Userdaoimpl (Databasehelper databasehelper) { This.databasehelper = Databasehelper;} @Overridepublic the Boolean insert (Users entity) {try {//) get the database operand sqlitedatabase db = Databasehelper.getwritabledatabase ( );//Db.execsql ("INSERT into users (username,userage,usersalary) VALUES ('" +entity.getuaername () + "'," + Entity.getuserage () + "," +entity.getusersalary () + ")");d B.execsql ("INSERT INTO Users" (Username,userage,usersalary, Userphone) VALUES (?,?,?,?) ", new object[] {entity.getusername (), Entity.getuserage (), Entity.getusersalary (), Entity.getuserphone ()});d b.close ();} catch (SQLException e) {e.printstacktrace (); return false;} return true;} @Overridepublic Boolean update (Users entity) {try {///Get the database operand sqlitedatabase db = Databasehelper.getwritabledatabase ( );d b.execsql ("Update users set username=?,userage=?,usersalary=?,userphone=?"). Where userid=? ", new object[] {entity.getusername (), Entity.getuserage (), Entity.getusersalary (), Entity.getuserphone(), Entity.getuserid ()});d b.close ();} catch (SQLException e) {e.printstacktrace (); return false;} return true;} @Overridepublic Boolean Delete (Users entity) {return Deletebyid (Entity.getuserid ());} @Overridepublic Boolean Deletebyid (Integer id) {try {//Get the database operand sqlitedatabase db = Databasehelper.getwritabledatabase ();d b.execsql ("Delete from users where userid=?", new object[] {ID});d b.close ();} catch (SQLException e) {e.printstacktrace (); return false;} return true;} @Overridepublic users FindByID (Integer ID) {users entity = NULL; Sqlitedatabase db = Databasehelper.getwritabledatabase (); Cursor C = db.rawquery ("Select Userid,username,userage,usersalary,userphone from Users where userid=?", new string[] {ID + ""}), if (C.movetonext ()) {entity = new Users (); Entity.setuserid (C.getint (C.getcolumnindex ("userid")); Entity.setusername (C.getstring (C.getcolumnindex ("username")), Entity.setuserage (C.getint (C.getcolumnindex (" Userage "))); Entity.setusersalary (C.getdouble (C.getcolumnindex (" Usersalary")); Entity.setuserphone (C.getstring (C.getcolumnindex (" Userphone "));} Db.close (); return entity;} @Overridepublic list<users> FindAll () {list<users> entities = new arraylist<> (); Sqlitedatabase db = Databasehelper.getwritabledatabase (); Cursor C = db.rawquery ("Select Userid,username,userage,usersalary,userphone from the users", null); while (C.movetonext ()) { Users entity = new users (); Entity.setuserid (C.getint (C.getcolumnindex ("userid")); Entity.setusername (C.getstring ( C.getcolumnindex ("username")), Entity.setuserage ((C.getint (C.getcolumnindex ("Userage"))); entity.setusersalary (C.getdouble (C.getcolumnindex ("usersalary")); Entity.setuserphone (C.getstring (C.getcolumnindex ("Userphone")); Entities.add (entity);} Db.close (); return entities;}

3. Get the contact information to the interface

1) Construction of the interface



Display data using the ListView plugin.

2) The steps to get the contact information to the interface:

① declaration gets the displayed control

② getting the data to display from the database

③ Create a custom adapter object that takes the adapter object as the control layer

④ using inherited Baseadapter objects to create myadapter to prepare for creating custom adapter objects

⑤ understand the methods in Myadapter and implement them, the GetCount () method returns the total number of entries; GetItem () returns the object corresponding to the current entry; the GetView () method returns each entry

⑥ returns the implementation of each entry for the GetView () method. Half of them are available in three ways:

One is: Achieve the effect of a single selection

Private View singlechoice (int position) {//Gets the current position of the real object of the users entity = entities.get (position);//Create a control object to display each entry// TextView tv=new TextView (mainactivity.this); View v = view.inflate (mainactivity.this,android. R.layout.simple_list_item_single_choice, NULL); Checkedtextview TV = (Checkedtextview) V.findviewbyid (Android. R.ID.TEXT1); Tv.settext (Entity.getusername () + "-" + Entity.getuserphone ()); Tv.setheight (+); return TV;}

One is: the effect of multi-selection

Private View multiplechoice (int position) {//Gets the current position of the real object of the users entity = entities.get (position);//Create a control object to display each entry// TextView tv=new TextView (mainactivity.this); View v = view.inflate (mainactivity.this,android. R.layout.simple_list_item_multiple_choice, NULL); Checkedtextview TV = (Checkedtextview) V.findviewbyid (Android. R.ID.TEXT1); Tv.settext (Entity.getusername () + "-" + Entity.getuserphone ()); Tv.setheight (+); return TV;}

One is: The effect of a custom method

Private View definedchoice (int position) {//Gets the location of the current entry users entity = Entities.get (position);//view Inflate method View V = view . Inflate (Mainactivity.this, r.layout.list_item_users,null);//Gets the control of the V object TextView tv_id = (TextView) V.findviewbyid ( R.ID.TV_ID); TextView tv_name = (TextView) V.findviewbyid (r.id.tv_name); TextView tv_age = (TextView) V.findviewbyid (r.id.tv_age); TextView Tv_phone = (TextView) V.findviewbyid (r.id.tv_phone);//Set Tv_id.settext for the control ("" + Entity.getuserid ()); tv_ Name.settext ("Name:" + entity.getusername ()), Tv_age.settext ("Age:" + entity.getuserage ()); Tv_phone.settext ("Phone:" + Entity.getuserphone ()); return v;}



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.