Android 70 displays data to the screen using the ListView

Source: Internet
Author: User

To add data using a unit test:

 PackageCom.itheima.showdata;ImportJava.sql.ResultSet;ImportAndroid.content.Context;Importandroid.database.sqlite.SQLiteDatabase;Importandroid.database.sqlite.SQLiteDatabase.CursorFactory;ImportAndroid.database.sqlite.SQLiteOpenHelper; Public classMyopenhelperextendsSqliteopenhelper { PublicMyopenhelper (Context context) {Super(Context, "people.db",NULL, 1); //TODO auto-generated Constructor stub    }    //when the database is created, this method invokes the@Override Public voidonCreate (Sqlitedatabase db) {Db.execsql ("CREATE table person (_id Integer primary key autoincrement, name Char (ten), Salary char (), phone Integer (20))"); }    //when the database is upgraded, this method invokes the@Override Public voidOnupgrade (Sqlitedatabase db,intOldversion,intnewversion) {System.out.println ("The database has been upgraded"); }        }
 Packagecom.itheima.sqlitedatabase.test;ImportCom.itheima.showdata.MyOpenHelper;Importandroid.content.ContentValues;Importandroid.database.sqlite.SQLiteDatabase;Importandroid.test.AndroidTestCase; Public classTestCaseextendsAndroidtestcase {PrivateMyopenhelper Oh; PrivateSqlitedatabase DB; //after the test framework has been initialized, this method calls before the test method executes.@Overrideprotected voidSetUp ()throwsException {Super. SetUp (); Oh=NewMyopenhelper (GetContext ()); DB=oh.getwritabledatabase (); }    //when the test method finishes executing, this method calls the@Overrideprotected voidTearDown ()throwsException {//TODO auto-generated Method Stub        Super. TearDown ();    Db.close (); }             Public voidInsertapi () {//encapsulates all data to be inserted into the Contentvalues object         for(inti = 0; I < 50; i++) {contentvalues values=Newcontentvalues (); Values.put ("Name", "Zhao" +i); Values.put ("Phone", "159" +i+i); Values.put ("Salary", "+i+"i); Db.insert ("Person",NULL, values); }    }        }

The first type of page displays:

<ScrollViewAndroid:layout_width= "Match_parent"Android:layout_height= "Match_parent"xmlns:android= "Http://schemas.android.com/apk/res/android"Xmlns:tools= "Http://schemas.android.com/tools"    ><LinearLayoutAndroid:id= "@+id/ll"Android:layout_width= "Match_parent"Android:layout_height= "Match_parent"Tools:context=". Mainactivity "android:orientation= "vertical"    ></LinearLayout></ScrollView>
 PackageCom.itheima.showdata;Importjava.util.ArrayList;Importjava.util.List;ImportCom.itheima.showdata.domain.Person;ImportAndroid.os.Bundle;Importandroid.app.Activity;ImportAndroid.database.Cursor;Importandroid.database.sqlite.SQLiteDatabase;ImportAndroid.view.Menu;Importandroid.widget.LinearLayout;ImportAndroid.widget.TextView; Public classMainactivityextendsActivity {List<Person>personlist; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);                Setcontentview (R.layout.activity_main); Personlist=NewArraylist<person>(); //to query the database data .Myopenhelper Oh =NewMyopenhelper ( This);//here, the Interfax this is available. 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); Person P=NewPerson (_id, name, phone, salary);        Personlist.add (P); } linearlayout ll=(LinearLayout) Findviewbyid (R.ID.LL); //display data to the screen         for(person p:personlist) {//1. Each element in the collection is a new TextViewTextView TV =NewTextView ( This); //2. Set the character's information to the contents of the text boxTv.settext (p.tostring ()); Tv.settextsize (18); //3. Set TextView as a sub-node of a linear layoutLl.addview (TV); }    }    }

Android 70 displays data to the screen using the ListView

Related Article

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.