码迷,mamicode.com
首页 > 移动开发 > 详细

android 70 使用ListView把数据显示至屏幕

时间:2015-11-01 16:35:34      阅读:284      评论:0      收藏:0      [点我收藏+]

标签:

使用单元测试添加数据:

package com.itheima.showdata;

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
    }

    //数据库创建时,此方法会调用
    @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("数据库升级了");
    }
    
    

}
package com.itheima.sqlitedatabase.test;


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 {
        // TODO Auto-generated method stub
        super.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);
        }
    }
    
    
}

第一种页面显示:

<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>
package com.itheima.showdata;

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);//这里上下文传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一个textView
            TextView tv = new TextView(this);
            //2.把人物的信息设置为文本框的内容
            tv.setText(p.toString());
            tv.setTextSize(18);
            //3.把textView设置为线性布局的子节点
            ll.addView(tv);
        }
    }


    
}

 

android 70 使用ListView把数据显示至屏幕

标签:

原文地址:http://www.cnblogs.com/yaowen/p/4927859.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!