Android-display database data to the screen
MainActivity. java
This code retrieves data from the database and displays the data on the interface.
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
PersonList; @ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); personList = new ArrayList
(); // Query the database data. // write this here because the other three parameters have been written in the super constructor of MyOpenHelper; myOpenHelper oh = new MyOpenHelper (this); SQLiteDatabase db = oh. getWritableDatabase (); Cursor cursor = db. query (person, 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); // encapsulate these values in a class. This idea should be learned. Since p is a local variable, therefore, the container that defines a global variable of // List is used to store the Variable p of the Person type. The key is to learn the idea of others. Person p = new Person (_ id, name, phone, salary); personList. add (p);} LinearLayout ll = (LinearLayout) findViewById (R. id. ll); // display the data to the screen for (Person p: personList) {// note that TextView can be created separately in addition to layout, // because it is also a class, it is a subclass of the View class, but the TextView // and layout are not associated at this time, so remember to add Step 1/1. each element in the set is a new textView TextView TV = new TextView (this); // 2. set the character information to the text box content TV. setText (p. toString (); TV. setTextSize (18); // After the preceding two statements are set, the TextView is not displayed on the interface. // Therefore, step 3 is required to associate the TextView with layout. // 3. set textView to linear layout of the child node ll. addView (TV );}}}
Note:When we have a lot of data, there are also a lot of new persons. At the same time,
There are also a lot of new textviews, so the memory may not be able to carry; so we should
What we need to do is to create any data when the data is displayed on the interface,
Instead of creating them all at once, we try to use ListView
Further optimization.
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, people. db, null, 1); // TODO Auto-generated constructor stub} // when the database is created, this method will call @ Override public void onCreate (SQLiteDatabase db) {db.exe cSQL (create table person (_ id integer primary key autoincrement, name char (10), salary char (20), phone integer (20 )));} // This method calls @ Override public void onUpgrade (SQLiteDatabase db, int oldVersion, int newVersion) {System. out. println (Database upgraded );}}
This code is used to add data to the database.
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; // this method is called @ Override protected void setUp () before the test method is executed after the test framework Initialization is complete () throws Exception {super. setUp (); oh = new MyOpenHelper (getContext (); db = oh. getWritableDatabase ();} // after the test method is executed, this method is called @ Override protected void tearDown () throws Exception {// TODO Auto-generated method stub super. tearDown (); db. close ();} public void insertApi () {// encapsulate all the data to be inserted into the ContentValues object for (int I = 0; I <50; I ++) {ContentValues values = new ContentValues (); values. put (name, Zhao + I); values. put (phone, 159 + I); values. put (salary, 160 + I); db. insert (person, null, values );}}}