Android--database Data Display to screen

Source: Internet
Author: User
<span id="Label3"></p><p style="border-width:0px; padding-top:0px; padding-bottom:0px; margin-top:0px; margin-bottom:8px; list-style:none; text-indent:2em; color:rgb(51,51,51); font-family:宋体; font-size:14px; line-height:28px"><p style="border-width:0px; padding-top:0px; padding-bottom:0px; margin-top:0px; margin-bottom:8px; list-style:none; text-indent:2em; color:rgb(51,51,51); font-family:宋体; font-size:14px; line-height:28px"><strong>Mainactivity.java</strong></p></p><p style="border-width:0px; padding-top:0px; padding-bottom:0px; margin-top:0px; margin-bottom:8px; list-style:none; text-indent:2em; color:rgb(51,51,51); font-family:宋体; font-size:14px; line-height:28px"><p style="border-width:0px; padding-top:0px; padding-bottom:0px; margin-top:0px; margin-bottom:8px; list-style:none; text-indent:2em; color:rgb(51,51,51); font-family:宋体; font-size:14px; line-height:28px"><strong>The purpose of this code is to get the data from the database and display it on the Interface.</strong></p></p><p style="border-width:0px; padding-top:0px; padding-bottom:0px; margin-top:0px; margin-bottom:8px; list-style:none; text-indent:2em; color:rgb(51,51,51); font-family:宋体; font-size:14px; line-height:28px"><p style="border-width:0px; padding-top:0px; padding-bottom:0px; margin-top:0px; margin-bottom:8px; list-style:none; text-indent:2em; color:rgb(51,51,51); font-family:宋体; font-size:14px; line-height:28px"><strong></strong></p></p><pre code_snippet_id="1585325" snippet_file_name="blog_20160223_1_8426703" name="code" class="java"> Import Java.util.arraylist;import java.util.List; Import com.itheima.showdata.domain.Person; Import Android.os.bundle;import Android.app.activity;import Android.database.cursor;import Android.database.sqlite.sqlitedatabase;import Android.view.menu;import Android.widget.linearlayout;import android.widget.TextView; public class Mainactivity extends Activity {list<person> personlist; @Override protected void onCreate (Bundle Savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (r.layout.activity_main); personlist = new Arraylist<person> (); Query the database data//write this here, because in the Myopenhelper Super constructor, has already written dead three other parameters; myopenhelper oh = new Myopenhelper (this); Sqlitedatabase db = Oh.getwritabledatabase (); cursor cursor = db.query (person, null, null, null, null, null, null, null); While (cursor.movetonext ()) {String _id = cursor.getstring (0); String name = Cursor.getstriNg (1); String salary = cursor.getstring (2); String phone = cursor.getstring (3); To encapsulate these values in a class, this idea must be learned; since p is a local variable here, the container that defines the global variable of the//list to hold the variable p of person type; the key to learn the thought of others; human p = NE W person (_id, name, phone, salary); Personlist.add (p); } linearlayout ll = (linearlayout) Findviewbyid (r.id.ll); Display data to screen for (person P:personlist) {//note that TextView can be created separately, except in Layout,//because It is also a class, is a sub-class below the view class, but at this time the Textview//and layout is not associated, so remember to add the 3rd step//1. each element in the collection is a new TextView TEXTV Iew TV = new TextView (this); 2. Set the Character's information to the text box content Tv.settext (p.tostring ()); Tv.settextsize (18); Setting the above two statements does not display the TextView on the interface,//so a third step is needed to correlate it with layout;//3. set TextView as a sub-node of a linear layout ll.addvi EW (tv); } } }</pre><br><p><p></p></p><p style="border-width:0px; padding-top:0px; padding-bottom:0px; margin-top:0px; margin-bottom:8px; list-style:none; text-indent:2em; color:rgb(51,51,51); font-family:宋体; font-size:14px; line-height:28px"><p style="border-width:0px; padding-top:0px; padding-bottom:0px; margin-top:0px; margin-bottom:8px; list-style:none; text-indent:2em; color:rgb(51,51,51); font-family:宋体; font-size:14px; line-height:28px"><strong>Note:</strong> When we have a lot of data, then new person is also a lot, at the same time,</p></p><p style="border-width:0px; padding-top:0px; padding-bottom:0px; margin-top:0px; margin-bottom:8px; list-style:none; text-indent:2em; color:rgb(51,51,51); font-family:宋体; font-size:14px; line-height:28px"><p style="border-width:0px; padding-top:0px; padding-bottom:0px; margin-top:0px; margin-bottom:8px; list-style:none; text-indent:2em; color:rgb(51,51,51); font-family:宋体; font-size:14px; line-height:28px">New out of the TextView are also many, then the memory may not be able to carry;</p></p><p style="border-width:0px; padding-top:0px; padding-bottom:0px; margin-top:0px; margin-bottom:8px; list-style:none; text-indent:2em; color:rgb(51,51,51); font-family:宋体; font-size:14px; line-height:28px"><p style="border-width:0px; padding-top:0px; padding-bottom:0px; margin-top:0px; margin-bottom:8px; list-style:none; text-indent:2em; color:rgb(51,51,51); font-family:宋体; font-size:14px; line-height:28px">To do is: what data do we need to create the data when it is displayed on the interface,</p></p><p style="border-width:0px; padding-top:0px; padding-bottom:0px; margin-top:0px; margin-bottom:8px; list-style:none; text-indent:2em; color:rgb(51,51,51); font-family:宋体; font-size:14px; line-height:28px"><p style="border-width:0px; padding-top:0px; padding-bottom:0px; margin-top:0px; margin-bottom:8px; list-style:none; text-indent:2em; color:rgb(51,51,51); font-family:宋体; font-size:14px; line-height:28px">Instead of creating them all at once, so we use the ListView as much as possible</p></p><p style="border-width:0px; padding-top:0px; padding-bottom:0px; margin-top:0px; margin-bottom:8px; list-style:none; text-indent:2em; color:rgb(51,51,51); font-family:宋体; font-size:14px; line-height:28px"><p style="border-width:0px; padding-top:0px; padding-bottom:0px; margin-top:0px; margin-bottom:8px; list-style:none; text-indent:2em; color:rgb(51,51,51); font-family:宋体; font-size:14px; line-height:28px">Further Optimization.</p></p><pre code_snippet_id="1585325" snippet_file_name="blog_20160223_2_2919042" name="code" class="java"><pre code_snippet_id="1585325" snippet_file_name="blog_20160223_2_2919042" name="code" class="java">import java.sql.ResultSet; Import Android.content.context;import Android.database.sqlite.sqlitedatabase;import Android.database.sqlite.sqlitedatabase.cursorfactory;import android.database.sqlite.SQLiteOpenHelper; public class Myopenhelper extends Sqliteopenhelper {public myopenhelper (context context) {super (context, peop le.db, null, 1); TODO auto-generated Constructor}//when The database is created, this method calls @Override public void OnCreate (sqlitedatabase Db) { Db.execsql (create Table Person (_id integer PRIMARY key autoincrement, name Char (ten), salary char), phone intege R (20))); }//database upgrade, This method calls @Override public void Onupgrade (sqlitedatabase db, int oldversion, int Newversion) {Sy Stem.out.println (database upgraded); }}</pre></pre><span style="color:rgb(51,51,51); font-family:宋体; font-size:14px; line-height:28px; text-indent:28px">the <span style="color:rgb(51,51,51); font-family:宋体; font-size:14px; line-height:28px; text-indent:28px">purpose of this code is to add data into the database</span></span><p style="border-width:0px; padding-top:0px; padding-bottom:0px; margin-top:0px; margin-bottom:8px; list-style:none; text-indent:2em; color:rgb(51,51,51); font-family:宋体; font-size:14px; line-height:28px"><p style="border-width:0px; padding-top:0px; padding-bottom:0px; margin-top:0px; margin-bottom:8px; list-style:none; text-indent:2em; color:rgb(51,51,51); font-family:宋体; font-size:14px; line-height:28px"><strong></strong></p></p><pre code_snippet_id="1585325" snippet_file_name="blog_20160223_3_1905287" name="code" class="java">Import com.itheima.showdata.MyOpenHelper; Import Android.content.contentvalues;import Android.database.sqlite.sqlitedatabase;import android.test.AndroidTestCase; public class TestCase extends Androidtestcase {private myopenhelper oh; Private Sqlitedatabase db; After the test framework is initialized, This method calls @Override protected void SetUp () throws Exception {super.setup () before the test method executes; Oh = new Myopenhelper (getcontext ()); db = Oh.getwritabledatabase (); This method calls @Override protected void TearDown () throws Exception {//TODO auto-generated method After the test method has finished Executing) Stub Super.teardown (); Db.close (); } public void Insertapi () {//wrap The data to be inserted into the Contentvalues object for (int i = 0; I <; i++) { Contentvalues values = new Contentvalues (); Values.put (name, Zhao +i); Values.put (phone, 159+i+i); Values.put (salary, 160+i+i); Db.insert (person, null, values); } } }</pre><br><br><br><p><p></p></p><p><p>Android--database Data Display to screen</p></p></span>

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.