Androidx学习笔记(22)-- 动态创建TextView展示数据库的数据

来源:互联网 发布:qq群营销软件 编辑:程序博客网 时间:2024/06/06 19:54

创建TextView展示数据库的数据至屏幕

  1. 任意插入一些数据
  2. 定义业务bean:Person.java
  3. 读取数据库的所有数据

Cursor cs = db.query("person", null, null, null, null, null, null);while(cs.moveToNext()){    String name = cs.getString(cs.getColumnIndex("name"));    String phone = cs.getString(cs.getColumnIndex("phone"));    String money = cs.getString(cs.getColumnIndex("money"));    //把读到的数据封装至Person对象    Person p = new Person(name, phone, money);    //把person对象保存至集合中    people.add(p);}

  4. 把集合中的数据显示至屏幕

LinearLayout ll = (LinearLayout) findViewById(R.id.ll); for(Person p : people){     //创建TextView,每条数据用一个文本框显示     TextView tv = new TextView(this);     tv.setText(p.toString());     //把文本框设置为ll的子节点     ll.addView(tv); }

 5.分页查询

Cursor cs = db.query("person", null, null, null, null, null, null, "0, 10");

实现效果图


项目代码

MyOpenHelper

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}//数据库创建时,此方法会调用@Overridepublic void onCreate(SQLiteDatabase db) {db.execSQL("create table person(_id integer primary key autoincrement, name char(10), salary char(20), phone integer(20))");}//数据库升级时,此方法会调用@Overridepublic void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {System.out.println("数据库升级了");}}

测试框架用来添加数据

public class TestCase extends AndroidTestCase {private MyOpenHelper oh;private SQLiteDatabase db;//测试框架初始化完毕之后,在测试方法执行之前,此方法调用@Overrideprotected void setUp() throws Exception {super.setUp();oh = new MyOpenHelper(getContext());db = oh.getWritableDatabase();}//测试方法执行完毕之后,此方法调用@Overrideprotected void tearDown() throws Exception {// TODO Auto-generated method stubsuper.tearDown();db.close();}public void insertApi(){//把要插入的数据全部封装至ContentValues对象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);}}}

单元测试配置

<instrumentation android:name="android.test.InstrumentationTestRunner"    android:targetPackage="com.exp.showdata"></instrumentation>  <uses-library android:name="android.test.runner"/>


模型 Person类

public class Person {private String _id;private String name;private String phone;private String salary;public String get_id() {return _id;}public void set_id(String _id) {this._id = _id;}public String getName() {return name;}public void setName(String name) {this.name = name;}public String getPhone() {return phone;}public void setPhone(String phone) {this.phone = phone;}public String getSalary() {return salary;}public void setSalary(String salary) {this.salary = salary;}@Overridepublic String toString() {return name + ", " + phone + ", " + salary;}public Person(String _id, String name, String phone, String salary) {super();this._id = _id;this.name = name;this.phone = phone;this.salary = salary;}}


添加布局文件

<LinearLayout     android:id="@+id/ll"    android:layout_width="match_parent"    android:layout_height="match_parent"    tools:context=".MainActivity"     android:orientation="vertical"    ></LinearLayout>

动态创建TextView 并展示数据

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) {        //1.集合中每有一条元素,就new一个textViewTextView tv = new TextView(this);//2.把人物的信息设置为文本框的内容tv.setText(p.toString());tv.setTextSize(18);//3.把textView设置为线性布局的子节点ll.addView(tv);}    }    }

让数据能够滑动

<ScrollView     android: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"    ><LinearLayout     android:id="@+id/ll"    android:layout_width="match_parent"    android:layout_height="match_parent"    tools:context=".MainActivity"     android:orientation="vertical"    ></LinearLayout></ScrollView>


1 0