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));
}
}
});
}
} |