Android开发之初级开发_动态加载数据库中数据到Listview中

项目目录:

MainActivity.java:


import java.util.List;

import com.hql.sqlite.dao.Student;
import com.hql.sqlite.domain.StudentInfo;

import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.app.Activity;

public class MainActivity extends Activity
{
    private ListView lv;
    List<StudentInfo> infos;

    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Student dao = new Student(this);
        infos = dao.findAll();
        lv = (ListView) findViewById(R.id.List);
        lv.setAdapter(new MyAdapter());
    }

    public class MyAdapter extends BaseAdapter
    {

        public int getCount()
        {
            return infos.size();
        }

        public Object getItem(int position)
        {
            return null;
        }

        public long getItemId(int position)
        {
            return 0;
        }

        public View getView(int position, View convertView, ViewGroup parent)
        {
            // TextView tvTextView=new TextView(getApplicationContext());
            // tvTextView.setTextSize(22);
            // StudentInfo dao = infos.get(position);
            // tvTextView.setText(dao.toString());
            // return tvTextView;
            StudentInfo dao = infos.get(position);
            View view = View.inflate(MainActivity.this, R.layout.list, null);
            TextView id = (TextView) view.findViewById(R.id.id);
            id.setText("ID:" + dao.getid());
            TextView name = (TextView) view.findViewById(R.id.name);
            name.setText("姓名:" + dao.getName());
            TextView number = (TextView) view.findViewById(R.id.pbone);
            number.setText("电话:" + dao.getNumber());
            return view;
        }

    }
}

 

 

StudentOpenhelper.java:

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class StudentOpenhelper extends SQLiteOpenHelper
{

    public StudentOpenhelper(Context context)
    {
        super(context, "student.db", null, 1);
    }

    public void onCreate(SQLiteDatabase db)
    {
        //创建一张表
        db.execSQL("create table student(id integer primary key autoincrement,name varchar(8),number varchar(12))");
    }

    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
    {

    }

}

Student.java:

import java.util.ArrayList;
import java.util.List;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.hql.sqlite.StudentOpenhelper;
import com.hql.sqlite.domain.StudentInfo;

public class Student
{
    private StudentOpenhelper helper;

    public Student(Context context)
    {
        helper = new StudentOpenhelper(context);
    }

    /**
     * 添加一条记录
     * 
     * @param name
     * @param number
     */
    public long add(String name, String number)
    {
        SQLiteDatabase db = helper.getWritableDatabase();
        ContentValues values = new ContentValues();
        values.put("name", name);
        values.put("number", number);
        long id = db.insert("student", null, values);
        db.close();
        return id;
    }

    /**
     * 查看
     * 
     * @param name
     * @return
     */
    public boolean find(String name)
    {
        SQLiteDatabase db = helper.getReadableDatabase();
        Cursor cursor = db.query("student", null, "name=?", new String[]
        { name }, null, null, null);
        boolean result = cursor.moveToNext();
        cursor.close();
        db.close();
        return result;
    }

    /**
     * 更新记录
     * 
     * @param name
     * @param newnumber
     * @return
     */
    public int update(String name, String newnumber)
    {
        SQLiteDatabase db = helper.getWritableDatabase();
        ContentValues values = new ContentValues();
        int number = db.update("student", values, "name=?", new String[]
        { name });
        db.close();
        return number;
    }

    /**
     * 删除记录
     * 
     * @param name
     * @return
     */
    public int delete(String name)
    {
        SQLiteDatabase db = helper.getWritableDatabase();
        int delete = db.delete("student", "name=?", new String[]
        { name });
        db.close();
        return delete;
    }

    /**
     * 查询所有记录
     * 
     * @return
     */
    public List<StudentInfo> findAll()
    {
        List<StudentInfo> infos = new ArrayList<StudentInfo>();
        SQLiteDatabase db = helper.getWritableDatabase();
        Cursor cursor = db.query(true, "student", new String[]
        { "id", "name", "number" }, null, null, null, null, null, null);
        while (cursor.moveToNext())
        {
            int id = cursor.getInt(cursor.getColumnIndex("id"));
            String name = cursor.getString(cursor.getColumnIndex("name"));
            String number = cursor.getString(cursor.getColumnIndex("number"));
            StudentInfo info = new StudentInfo(id, name, number);
            infos.add(info);
        }
        cursor.close();
        db.close();
        return infos;
    }
}

 

 StudentInfo.java:

public class StudentInfo
{
    private int id;
    private String name;
    private String number;

    public StudentInfo()
    {
    }

    public StudentInfo(int id, String name, String number)
    {
        this.id = id;
        this.name = name;
        this.number = number;
    }

    public String toString()
    {
        return "id=" + id + ",姓名=" + name + ", 电话=" + number;
    }

    public int getid()
    {
        return id;
    }

    public void setid(int id)
    {
        this.id = id;
    }

    public String getName()
    {
        return name;
    }

    public void setName(String name)
    {
        this.name = name;
    }

    public String getNumber()
    {
        return number;
    }

    public void setNumber(String number)
    {
        this.number = number;
    }

}

SQLiteTest.java:

import java.util.List;

import com.hql.sqlite.StudentOpenhelper;
import com.hql.sqlite.dao.Student;
import com.hql.sqlite.domain.StudentInfo;

import android.test.AndroidTestCase;

public class SQLiteTest extends AndroidTestCase
{
    public void testcreate() throws Exception
    {
        StudentOpenhelper helper = new StudentOpenhelper(getContext());
        helper.getReadableDatabase();
    }

    public void testAdd() throws Exception
    {
        Student daoStudent = new Student(getContext());
        // daoStudent.add("脑残", "110");
        for (int i = 0; i < 40; i++)
        {
            daoStudent.add("张三" + i, "13838383800" + i);
        }
    }

    public void testfind() throws Exception
    {
        Student daoStudent = new Student(getContext());
        boolean result = daoStudent.find("脑残");
        assertEquals(true, result);
    }

    public void testupdate() throws Exception
    {
        Student daoStudent = new Student(getContext());
        daoStudent.update("脑残", "110123");
    }

    public void testdelete() throws Exception
    {
        Student daoStudent = new Student(getContext());
        daoStudent.delete("脑残");
    }

    public void testfindall() throws Exception
    {
        Student daoStudent = new Student(getContext());
        List<StudentInfo> list = daoStudent.findAll();
        for (StudentInfo studentInfo : list)
        {
            System.out.println(studentInfo.toString());
        }
    }
}


布局文件:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >

    <ListView
        android:id="@+id/List"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >
    </ListView>

</LinearLayout>

list.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:orientation="horizontal" >

        <RelativeLayout
            android:id="@+id/left"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center" >

            <TextView
                android:id="@+id/id"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="10dp"
                android:text="Id"
                android:textSize="20sp" />
        </RelativeLayout>

        <RelativeLayout
            android:id="@+id/right"
            android:layout_width="wrap_content"
            android:layout_height="50dp"
            android:layout_gravity="center" >

            <TextView
                android:id="@+id/name"
                android:layout_width="wrap_content"
                android:layout_height="25dp"
                android:layout_marginLeft="10dp"
                android:text="姓名"
                android:textSize="20sp" />

            <TextView
                android:id="@+id/pbone"
                android:layout_width="wrap_content"
                android:layout_height="25dp"
                android:layout_below="@+id/name"
                android:layout_marginLeft="10dp"
                android:text="电话"
                android:textSize="20sp" />
        </RelativeLayout>
    </LinearLayout>

</LinearLayout>

 

注册文件:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.hql.sqlite"
    android:versionCode="1"
    android:versionName="1.0" >

    <instrumentation
        android:name="android.test.InstrumentationTestRunner"
        android:targetPackage="com.hql.sqlite" />

    <uses-sdk
        android:minSdkVersion="14"
        android:targetSdkVersion="14" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
         <uses-library android:name="android.test.runner" />
        <activity
            android:name="com.hql.sqlite.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>


//程序运行,应该先执行测试类中的ADD方法(单元测试),将数据添加到数据库中。

//不懂单元测试,请百度

posted @ 2014-05-13 15:47  loneliness__白色  阅读(346)  评论(0)    收藏  举报