Chinaunix首页 | 论坛 | 博客
  • 博客访问: 900111
  • 博文数量: 322
  • 博客积分: 6688
  • 博客等级: 准将
  • 技术积分: 3626
  • 用 户 组: 普通用户
  • 注册时间: 2010-09-19 11:26
文章分类

全部博文(322)

文章存档

2013年(5)

2012年(66)

2011年(87)

2010年(164)

分类: Java

2010-12-31 14:17:34

控件

DBActivity

Java代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
package org.wp.db;  
 
/** 
 * 三种适配器 
 * ArrayAdapter 
 * SimpleAdapter 
 * SimpleCursorAdapter 
 */  
 
import java.util.ArrayList;  
import java.util.HashMap;  
import java.util.List;  
import org.wp.domain.Person;  
import org.wp.service.PersonService;  
import android.app.Activity;  
import android.database.Cursor;  
import android.os.Bundle;  
import android.util.Log;  
import android.view.View;  
import android.widget.AdapterView;  
import android.widget.ListView;  
import android.widget.SimpleAdapter;  
import android.widget.SimpleCursorAdapter;  
 
public class DBActivity extends Activity {  
    private static final String TAG = "DBActivity";  
 
    private ListView listView;  
    private PersonService personService;  
 
    @Override  
    public void onCreate(Bundle savedInstanceState) {  
        super.onCreate(savedInstanceState);  
        setContentView(R.layout.main);  
 
        ListView listView = (ListView) findViewById(R.id.listView);  
        personService = new PersonService(this);  
 
        /** 
        // 保存数据 
        personService.save(); 
 
        // 获得数据 
        List persons = personService.getScrollData(0, 10); 
        // 声明数据显示集合 
        List> data = new ArrayList>(); 
        // 添加标题 
        HashMap title = new HashMap(); 
        title.put("personid", "编号"); 
        title.put("name", "姓名"); 
        title.put("age", "年龄"); 
        data.add(title); 
        // 添加内容 
        for (Person person : persons) { 
            HashMap hashMap = new HashMap(); 
            hashMap.put("personid", String.valueOf(person.getPersonid())); 
            hashMap.put("name", person.getName()); 
            hashMap.put("age", String.valueOf(person.getAge())); 
            data.add(hashMap); 
        } 
        // 添加适配器 
        SimpleAdapter simpleAdapter = new SimpleAdapter(DBActivity.this, data, 
                R.layout.personitem, 
                new String[] { "personid", "name", "age" }, new int[] { 
                        R.id.personid, R.id.name, R.id.age }); 
        listView.setAdapter(simpleAdapter); 
        */  
 
        // 获得游标集  
        Cursor cursor = personService.getCursorScrollData(0, 10);  
        // 添加适配器  
        SimpleCursorAdapter simpleCursorAdapter = new SimpleCursorAdapter(  
                DBActivity.this, R.layout.personitem, cursor, new String[] {  
                        "_id", "name", "age" }, new int[] { R.id.personid,  
                        R.id.name, R.id.age });  
        listView.setAdapter(simpleCursorAdapter);  
 
 
        // 添加选中事件  
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {  
            @Override  
            public void onItemClick(AdapterView parent, View view,  
                    int position, long id) {  
                ListView listView = (ListView) parent;  
                /* 
                 * 第一种 
                HashMap itemData = (HashMap) listView 
                        .getItemAtPosition(position); 
                String personid = itemData.get("personid"); 
                String name = itemData.get("name"); 
                String age = itemData.get("age"); 
                Log.i(TAG, "className=" + view.getClass().getName()); 
                Log.i(TAG, "personid=" + personid + ",name=" + name + ",age=" 
                        + age); 
                Log.i(TAG, "result=" + (position == id)); 
                */  
 
                // 第二种  
                Cursor cursor = (Cursor) listView.getItemAtPosition(position);  
                while (cursor.moveToNext()) {  
                    Log.i(TAG, "personid" + cursor.getInt(0) + ",name"  
                                    + cursor.getString(1) + ",age"  
                                    + cursor.getShort(2));  
                }  
            }  
        });  
 
    }  
}


DataBaseOpenHelper

Java代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
package org.wp.service;  
 
import android.content.Context;  
import android.database.sqlite.SQLiteDatabase;  
import android.database.sqlite.SQLiteOpenHelper;  
 
public class DataBaseOpenHelper extends SQLiteOpenHelper {  
 
    private static final String DBNAME = "wp";  
    private static final int VERSION = 1;  
 
    public DataBaseOpenHelper(Context context) {  
        super(context, DBNAME, null, VERSION);  
    }  
 
    @Override  
    public void onCreate(SQLiteDatabase db) {  
        db.execSQL("create table person(personid integer primary key autoincrement,name varchar(20),age integer)");  
    }  
 
    @Override  
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {  
        db.execSQL("drop table if exists person");  
        onCreate(db);  
    }  
}

PersonService

Java代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
package org.wp.service;  
 
import java.util.ArrayList;  
import java.util.List;  
import org.wp.domain.Person;  
import android.content.Context;  
import android.database.Cursor;  
import android.database.sqlite.SQLiteDatabase;  
import android.util.Log;  
 
public class PersonService {  
 
    private static final String TAG = "PersonService";  
 
    private DataBaseOpenHelper dataBaseOpenHelper;  
    private SQLiteDatabase database;  
 
    public PersonService(Context context) {  
        dataBaseOpenHelper = new DataBaseOpenHelper(context);  
    }  
 
    public void save() {  
        database = dataBaseOpenHelper.getWritableDatabase();  
        database.beginTransaction();  
        try {  
            database.execSQL("insert into person(name,age) values(?,?)",  
                    new Object[] { "张三", (short) 26 });  
            database.execSQL("insert into person(name,age) values(?,?)",  
                    new Object[] { "田七", (short) 35 });  
            database.setTransactionSuccessful();  
        } catch (Exception e) {  
            Log.i(TAG, e.toString());  
        }  
        database.endTransaction();  
    }  
 
    public List<Person> getScrollData(int startResult, int maxResult) {  
        List<Person> persons = new ArrayList<Person>();  
        database = dataBaseOpenHelper.getReadableDatabase();  
        Cursor cursor = database.rawQuery("select * from person limit ?,?",  
                new String[] { String.valueOf(startResult),  
                        String.valueOf(maxResult) });  
        while (cursor.moveToNext()) {  
            persons.add(new Person(cursor.getInt(0), cursor.getString(1),  
                    cursor.getShort(2)));  
        }  
        return persons;  
    }  
 
    public Cursor getCursorScrollData(int startResult, int maxResult) {  
        database = dataBaseOpenHelper.getReadableDatabase();  
        return database.rawQuery(  
                "select personid as _id,name,age from person limit ?,?",  
                new String[] { String.valueOf(startResult),  
                        String.valueOf(maxResult) });  
    }  
 
}

Person

Java代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
package org.wp.domain;  
 
public class Person {  
 
    private Integer personid;  
    private String name;  
    private Short age;  
 
    public Person(Integer personid, String name, Short age) {  
        this.personid = personid;  
        this.name = name;  
        this.age = age;  
    }  
 
    public Person(String name, Short age) {  
        this.name = name;  
        this.age = age;  
    }  
 
    public Integer getPersonid() {  
        return personid;  
    }  
 
    public void setPersonid(Integer personid) {  
        this.personid = personid;  
    }  
 
    public String getName() {  
        return name;  
    }  
 
    public void setName(String name) {  
        this.name = name;  
    }  
 
    public Short getAge() {  
        return age;  
    }  
 
    public void setAge(Short age) {  
        this.age = age;  
    }  
 
    @Override  
    public String toString() {  
        return "personid=" + personid + ",name=" + name + ",age=" + age;  
    }  
 
}

main.xml

Xml代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
 version="1.0" encoding="utf-8"?>  
 xmlns:android=""  
    android:orientation="vertical"  
    android:layout_width="fill_parent"  
    android:layout_height="fill_parent"  
    >  
      
        xmlns:android=""  
        android:layout_width="fill_parent"  
        android:layout_height="wrap_content"  
        >  
          
            android:layout_width="40px"  
            android:layout_height="wrap_content"  
            android:textSize="20px"  
            android:text="编号"  
            android:id="@+id/personidTitle"  
            />  
          
            android:layout_width="200px"  
            android:layout_height="wrap_content"  
            android:layout_toRightOf="@id/personidTitle"  
            android:layout_alignTop="@id/personidTitle"  
            android:gravity="center_horizontal"  
            android:textSize="20px"  
            android:text="姓名"  
            android:id="@+id/nameTitle"  
            />  
          
            android:layout_width="wrap_content"  
            android:layout_height="wrap_content"  
            android:layout_toRightOf="@id/nameTitle"  
            android:layout_alignTop="@id/nameTitle"  
            android:textSize="20px"  
            android:text="年龄"  
            android:id="@+id/ageTitle"  
            />  
    >  
 
      
        android:layout_width="fill_parent"  
        android:layout_height="wrap_content"  
        android:id="@+id/listView"  
        />   
>

personitem.xml

Xml代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
 version="1.0" encoding="utf-8"?>  
  
    xmlns:android=""  
    android:layout_width="fill_parent"  
    android:layout_height="wrap_content"  
    >  
      
        android:layout_width="40px"  
        android:layout_height="wrap_content"  
        android:textSize="20px"  
        android:id="@+id/personid"  
        />  
      
        android:layout_width="200px"  
        android:layout_height="wrap_content"  
        android:layout_toRightOf="@id/personid"  
        android:layout_alignTop="@id/personid"  
        android:gravity="center_horizontal"  
        android:textSize="20px"  
        android:id="@+id/name"  
        />  
      
        android:layout_width="wrap_content"  
        android:layout_height="wrap_content"  
        android:layout_toRightOf="@id/name"  
        android:layout_alignTop="@id/name"  
        android:textSize="20px"  
        android:id="@+id/age"  
        />  
>

AndroidManifest.xml

Xml代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
 version="1.0" encoding="utf-8"?>  
 xmlns:android=""  
    package="org.wp.db" android:versionCode="1" android:versionName="1.0">  
     android:icon="@drawable/icon" android:label="@string/app_name">  
         android:name="android.test.runner" />  
         android:name=".DBActivity" android:label="@string/app_name">  
            >  
                 android:name="android.intent.action.MAIN" />  
                 android:name="android.intent.category.LAUNCHER" />  
            >  
        >  
    >  
     android:minSdkVersion="7" />  
     android:name="android.test.InstrumentationTestRunner"  
        android:targetPackage="org.wp.db" android:label="Tests for My App" />  
>
阅读(1028) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~