MainActivity.
这段代码的作用是从中获取到数据并显示在界面上
-
- 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>();
-
-
- 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);
-
-
-
- Person p = new Person(_id, name, phone, salary);
- personList.add(p);
- }
-
- LinearLayout ll = (LinearLayout) findViewById(R.id.ll);
-
- for (Person p : personList) {
-
-
-
-
- TextView tv = new TextView(this);
-
- tv.setText(p.toString());
- tv.setTextSize(18);
-
-
-
- ll.addView(tv);
- }
- }
- }
注:当我们数据很多的时候,那么new出来的person也很多,与此同时,
new出来的TextView也很多,那么此时内存有可能扛不住;所以我们应该
做的就是:我们需要什么数据显示在界面上的时候,就创建什么数据,
而不是一下子全部创建出来,所以我们在尽可能使用ListView对其进行
进一步优化。
- 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);
-
- }
-
-
- @Override
- public void onCreate(SQLiteDatabase db) {
- db.execSQL(create table person(_id integer primary key autoincrement, name char(10), salary char(20), phone integer(20)));
-
- }
-
-
- @Override
- public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
- System.out.println(数据库升级了);
- }
- }
这段代码的作用是添加数据进数据库
- 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;
-
- @Override
- protected void setUp() throws Exception {
- super.setUp();
-
- oh = new MyOpenHelper(getContext());
- db = oh.getWritableDatabase();
- }
-
-
- @Override
- protected void tearDown() throws Exception {
-
- super.tearDown();
- db.close();
- }
-
- public void insertApi(){
-
- for (int i = 0; i < 50; i++) {
- ContentValues values = new ContentValues();
- values.put(name, 赵+i);
- values.put(phone, 159+i+i);
- values.put(salary, 160+i+i);
- db.insert(person, null, values);
- }
- }
- }
-
转载:http://blog.csdn.net/chaoyu168/article/details/50720888