3.安卓基础之数据库&listview&对话框

1. Android的xml的解析器

1.1 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
46
47
48
49
50
51
52
53
54
55
56
MainActivity.java
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//使用xml pull 解析器去解析xml文件的内容
XmlPullParser parser = Xml.newPullParser();
try {
InputStream in = getAssets().open("result.xml");
parser.setInput(in, "gbk");
//获取事件的类型
int eventType = parser.getEventType();
/*
<?xml version="1.0" encoding="gbk"?>
<smartresult>
<product type="mobile">
<phonenum>13512345678</phonenum>
<location>重庆移动神州行卡</location>
<phoneJx>有得有失,华而不实,须防劫财,始保平安 吉带凶</phoneJx>
</product>
</smartresult>
*
*/
Product p = null;
while(eventType!=XmlPullParser.END_DOCUMENT){
//判断是否是元素的开始,只要是某个元素的开始位置,那么就会进入
//获得当前元素解析的元素的名称
if("product".equals(parser.getName())){
p = new Product();
String type = parser.getAttributeValue(0);//product
p.setType(type);
}else if("phonenum".equals(parser.getName())){
String phonenum = parser.nextText();
p.setPhonenum(phonenum);
}else if("location".equals(parser.getName())){
String location = parser.nextText();
p.setLocation(location);
}else if("phoneJx".equals(parser.getName())){
String phoneJx = parser.nextText();
p.setPhoneJx(phoneJx);
}
//手动挪动指针
eventType = parser.next();
}
if(p!=null){
Log.d("ProductMessange",p.toString());
}
} catch (Exception e) {
e.printStackTrace();
}
}
1
2
3
4
5
6
7
8
9
10
11
result.xml
<?xml version="1.0" encoding="gbk"?>
<smartresult>
<product type="mobile">
<phonenum>13512345678</phonenum>
<location>重庆移动神州行卡</location>
<phoneJx>有得有失,华而不实,须防劫财,始保平安 吉带凶
</phoneJx>
</product>
</smartresult>
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
Product.java
package com.example.xmlpullparsertest;
/*
*
* 封装数据的 标 准的 javabean
*
<product type="mobile">
<phonenum>13512345678</phonenum>
<location>重庆移动神州行卡</location>
<phoneJx>有得有失,华而不实,须防劫财,始保平安 吉带凶</phoneJx>
</product>
*
*/
public class Product {
private String type;
private String phonenum;
private String phoneJx;
private String location;
public String getLocation() {
return location;
}
public void setLocation(String location) {
this.location = location;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public String getPhonenum() {
return phonenum;
}
public void setPhonenum(String phonenum) {
this.phonenum = phonenum;
}
public String getPhoneJx() {
return phoneJx;
}
public void setPhoneJx(String phoneJx) {
this.phoneJx = phoneJx;
}
@Override
public String toString() {
return "Product [type=" + type + ", phonenum=" + phonenum
+ ", phoneJx=" + phoneJx + "]";
}
}

2. 保存数据到数据库中的作用

如果保存的数据是有规律的,格式是同样的.那么可以去选择使用数据库保存数据.Android提供的数据库(sqlite)数据库.

  • android提供的数据库小应用
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
MainActivity.java
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
MySqliteHelper helper = new MySqliteHelper(MainActivity.this);
helper.getReadableDatabase();
}
MySqliteHelper.java
package com.example.sqlitedemo;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
public class MySqliteHelper extends SQLiteOpenHelper {
public MySqliteHelper(Context context){
//context:应用上下文
//name:数据库的名词
//factory:创建游标的工厂
//version:数据库的版本
super(context, "mydb1", null, 4);
/*
mysql
create table users(
id int primary key auto_increment,
name varchar(30)
);
android 中 :
create table students(
id integer primary key autoincrement,
name varchar(30)
);
*/
}
//在数据库首次被创建时调用
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("create table students ( _id integer primary key autoincrement,name varchar(30), sex varchar(10))");
}
//在数据库升级时调用,当版本比之前的版本高时会执行该方法!
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("alter table students add number varchar(10)");
}
}
  • StudentSystem在SQLiteOpenHelper的应用详细版
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
Student.java
package domain;
public class Student {
private String id;
private String name;
private String sex;
public Student(){
}
public Student(String id,String name,String sex){
this.name = name;
this.id = id;
this.sex = sex;
}
public String getId() {
return id;
}
@Override
public String toString() {
return "Student [id=" + id + ", name=" + name + ", sex=" + sex + "]";
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
}
StudentDao.java
package com.example.studentsystemdemo1;
import java.util.ArrayList;
import java.util.List;
import domain.Student;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
public class StudentDao {
StudentDbOpenHelper helper;
public StudentDao(Context context){
helper = new StudentDbOpenHelper(context);
}
public void add(Student st){
//拿到工具类的实例,然后去操作数据库
SQLiteDatabase db = helper.getReadableDatabase();
//执行sql语句
db.execSQL("insert into students values(null,?,?)",new Object[]{st.getName(),st.getSex()});
}
public void delete(String id){
SQLiteDatabase db = helper.getReadableDatabase();
db.execSQL("delete from students where _id=?",new Object[]{id});
}
public void update(Student st){
SQLiteDatabase db = helper.getReadableDatabase();
db.execSQL("update students set name=?,sex=? where _id=?", new Object[]{st.getName(),st.getSex(),st.getId()});
}
public Student find(String id){
SQLiteDatabase db = helper.getReadableDatabase();
Cursor cursor = db.rawQuery("select * from students where _id=?", new String[]{id});
boolean result = cursor.moveToNext();
Student st = null;
if(result){
int _id = cursor.getInt(cursor.getColumnIndex("_id"));
String name = cursor.getString(cursor.getColumnIndex("name"));
String sex = cursor.getString(cursor.getColumnIndex("sex"));
st = new Student(String.valueOf(id),name,sex);
}
cursor.close();
return st;
}
//查询返回所有学生的信息
public List<Student> getAll(){
List<Student> list = new ArrayList<Student>();
SQLiteDatabase db = helper.getReadableDatabase();
Cursor cursor = db.rawQuery("select * from students", null);
while(cursor.moveToNext()){
int id = cursor.getInt(cursor.getColumnIndex("_id"));
String name = cursor.getString(cursor.getColumnIndex("name"));
String sex = cursor.getString(cursor.getColumnIndex("sex"));
Student st = new Student(String.valueOf(id),name,sex);
list.add(st);
}
list = null;
return list;
}
}
StudentDbOpenHelper.java
package com.example.studentsystemdemo1;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
public class StudentDbOpenHelper extends SQLiteOpenHelper {
public StudentDbOpenHelper(Context context){
super(context, "student.db", null, 1);
}
@Override
public void onCreate(SQLiteDatabase db) {
//创建数据库
db.execSQL("create table students (_id integer primary key autoincrement, name varchar(30),sex varchar(10))");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
}
  • 学生查询系统带item的应用:
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
Activity_main.xml
<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="match_parent"
android:orientation="vertical"
tools:context=".MainActivity" >
<EditText
android:id="@+id/ed_name"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="请输入学生的姓名" />
<RadioGroup
android:id="@+id/rgb"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<RadioButton
android:id="@+id/male"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="1"
android:checked="true"
android:text="男" />
<RadioButton
android:id="@+id/female"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="女" />
</RadioGroup>
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:onClick="save"
android:text="保存" />
<ScrollView
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<LinearLayout
android:id="@+id/ll"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
</LinearLayout>
</ScrollView>
</LinearLayout>
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
MainActivity.java
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//初始化
ed_name = (EditText) findViewById(R.id.ed_name);
rgb= (RadioGroup) findViewById(R.id.rgb);
lv = (ListView) findViewById(R.id.lv);
sdao = new StudentDao(this);
refreshView();
}
public void save(View v){
String name = ed_name.getText().toString().trim();
if(TextUtils.isEmpty(name)){
Toast.makeText(this, "学生信息不能为空", 0).show();
return;
}
String sex ="male";
int id = rgb.getCheckedRadioButtonId();
if(id==R.id.male){
sex ="male";
}else{
sex ="female";
}
//将数据 保存到数据库 中去
// 拿到 dao
Student st = new Student("xjlkj",name,sex);
sdao.add(st);
Toast.makeText(this, "保存 "+ name+"成功", 0).show();
//将所有的数据同步的显示 到 屏幕上去
//查询现有的数据
refreshView();
}
List<Student> students;
private MyAdapter myAdapter;
private void refreshView() {
// 将现有的全部给清空一下
students = sdao.getAll();
if(myAdapter ==null){
myAdapter = new MyAdapter();
//为 lv 设置数据适配器 --- 设置控制器controller
lv.setAdapter(myAdapter);
}else{
//则说明myAdapter已经存在了,需要做的是,通知控制器 adapter去更新一下数据.
myAdapter.notifyDataSetChanged();
}
}
private class MyAdapter extends BaseAdapter{
//最开始被调用的 , 用于告诉控制器 adapter到底要显示 多少个item
@Override
public int getCount() {
//告诉控制器 adapter显示多少个item
return students.size();
}
//每个item要显示在屏幕上时,又会调用到这个方法
//convertView:就是用来进行优化的
@Override
public View getView(int position, View convertView, ViewGroup parent) {
//获得当前的学生
final Student st = students.get(position);
//现在 item 已经定义好了,需要将硬盘上item.xml布局文件 转化为一个布局的对象返回去.
View v;
if (convertView == null) {
v = View.inflate(MainActivity.this, R.layout.item, null);
} else {
v = convertView;
}
//打气筒的对象 ,去填充xml,使得xml布局文件变为一个view对象.
//拿到iv和tv
ImageView iv = (ImageView) v.findViewById(R.id.item_iv);
String sex = st.getSex();
if("male".equals(sex)){
iv.setImageResource(R.drawable.nan);
}else{
iv.setImageResource(R.drawable.nv);
}
TextView tv = (TextView) v.findViewById(R.id.item_tv);
tv.setText(st.getName());
v.findViewById(R.id.item_delete).setOnClickListener( new View.OnClickListener() {
@Override
public void onClick(View v) {
/*System.out.println("======= onClick 点击了 ");
//需要拿到当前的点击的item ,将 学生的信息给删掉.
//获得点击点击的item
String id = st.getId();
sdao.delete(id);
Toast.makeText(MainActivity.this, "删除成功 : " + st.getName(), 0).show();
//重新加载下数据
refreshView();*/
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setTitle("删除...");
builder.setMessage("亲,你确定要删除吗? 不能反悔 的哦...");
//设置点击确定的按钮
builder.setPositiveButton("删除",new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
String id = st.getId();
sdao.delete(id);
Toast.makeText(MainActivity.this, "删除 成功 : " + st.getName(), 0).show();
// 重新 加载 下 数据
refreshView();
}
});
//设置点击取消的的按钮
builder.setNegativeButton("取消", null);
//你需要 调用 相应的方法 才能够显示出来
/* AlertDialog dialog = builder.create();
dialog.show();*/
builder.show();
}
});
return v;
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return null;
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
}
}
StudentDao.java
package com.example.studentsystemdemo2;
import java.util.ArrayList;
import java.util.List;
import domain.Student;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
public class StudentDao {
StudentDbOpenHelper helper;
public StudentDao(Context context){
helper = new StudentDbOpenHelper(context);
}
// "create table students (_id integer primary key autoincrement, name varchar(30),sex varchar(10))"
public void add(Student st){
//拿到工具类的实例, 然后去操作 数据库
SQLiteDatabase db = helper.getWritableDatabase();
//执行sql语句
db.execSQL("insert into students values(null,?,?)",new Object[]{st.getName(),st.getSex()});
}
public void delete(String id){
SQLiteDatabase db = helper.getWritableDatabase();
db.execSQL("delete from students where _id=?",new Object[]{id});
}
public void update(Student st){
SQLiteDatabase db = helper.getWritableDatabase();
db.execSQL("update students set name=?,sex=? where _id=?", new Object[]{st.getName(),st.getSex(),st.getId()});
}
public Student find(String id){
SQLiteDatabase db = helper.getReadableDatabase();
//select * from users where id=?
//叫做游标,与 javaweb中所学的resultSet结构是一样的
Cursor cursor = db.rawQuery("select * from students where _id=?", new String[]{id});
boolean result = cursor.moveToNext();
Student st = null;
if(result){
// st = new Student();
/*int _id = cursor.getInt(0);
String name = cursor.getString(1);
String sex = cursor.getString(2);
st.setId(id);
st.setName(name);
st.setSex(sex);*/
int _id = cursor.getInt(cursor.getColumnIndex("_id"));
String name = cursor.getString(cursor.getColumnIndex("name"));
String sex = cursor.getString(cursor.getColumnIndex("sex"));
st = new Student(String.valueOf(_id),name,sex);
}
// 最后会释放 资源
cursor.close();
return st;
}
//查询返回所有的学生的信息
public List<Student> getAll() {
List<Student> list =new ArrayList<Student>();
SQLiteDatabase db = helper.getReadableDatabase();
Cursor cursor = db.rawQuery("select * from students", null);
while(cursor.moveToNext()){
int id = cursor.getInt(cursor.getColumnIndex("_id"));
String name = cursor.getString(cursor.getColumnIndex("name"));
String sex = cursor.getString(cursor.getColumnIndex("sex"));
Student st = new Student(String.valueOf(id), name, sex);
list.add(st);
}
return list;
}
}
StudentDbOpenHelper.java
package com.example.studentsystemdemo2;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
public class StudentDbOpenHelper extends SQLiteOpenHelper {
public StudentDbOpenHelper(Context context) {
super(context, "student.db", null, 1);
}
@Override
public void onCreate(SQLiteDatabase db) {
//创建数据库
db.execSQL("create table students (_id integer primary key autoincrement, name varchar(30),sex varchar(10))");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
}
Student.java
package domain;
public class Student {
private String id;
private String name;
private String sex;
public Student() {
super();
}
public Student(String id, String name, String sex) {
super();
this.id = id;
this.name = name;
this.sex = sex;
}
@Override
public String toString() {
return "Student [id=" + id + ", name=" + name + ", sex=" + sex + "]";
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
}
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
activity_main.xml
<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="match_parent"
android:orientation="vertical"
tools:context=".MainActivity" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />
<EditText
android:id="@+id/ed_name"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="请输入学生的姓名" />
<RadioGroup
android:id="@+id/rgb"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<RadioButton
android:id="@+id/male"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="1"
android:checked="true"
android:text="男" />
<RadioButton
android:id="@+id/female"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="女" />
</RadioGroup>
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:onClick="save"
android:text="保存" />
<ListView
android:id="@+id/lv"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
</ListView>
</LinearLayout>
item.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="horizontal" >
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<ImageView
android:layout_marginLeft="15dip"
android:layout_alignParentLeft="true"
android:id="@+id/item_iv"
android:layout_width="20dip"
android:layout_height="30dip"
android:layout_centerVertical="true"
/>
<TextView
android:layout_toRightOf="@id/item_iv"
android:id="@+id/item_tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
/>
<ImageView
android:layout_marginRight="8dip"
android:id="@+id/item_delete"
android:layout_centerVertical="true"
android:layout_alignParentRight="true"
android:src="@drawable/delete"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</RelativeLayout>
</LinearLayout>
  • 常见的对话框
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
activity_main.xml
<Button
android:onClick="dialog01"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="创建确认取消对话框"/>
<Button
android:onClick="dialog02"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="创建单选对话框"/>
<Button
android:onClick="dialog03"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="创建多选对话框"/>
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
MainActivity.java
//创建确认取消对话框
public void dialog01(View v){
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("约会吗?");
builder.setMessage("告别单身,你愿意吗?");
//愿意情况下开启监听器
builder.setPositiveButton("愿意", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this, "我也单身,说不定可以来找我", 0).show();
}
});
builder.setNegativeButton("不愿意", null);
builder.show();
}
//创建单选进度条
public void dialog02(View v){
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("单选对话框");
final String[] items = {"小樱","小明","诸葛亮"};
builder.setSingleChoiceItems(items, -1, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this, "被点击了:"+items[which]+",位置:"+which, 0).show();
}
});
builder.show();
}
//创建多选对话框
public void dialog03(View v){
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("多选框");
final String[] items ={"android","python","go","c/c++"};
boolean[] checkItems = {true,false,false,true};
builder.setMultiChoiceItems(items, checkItems, new DialogInterface.OnMultiChoiceClickListener() {
@Override
public void onClick(DialogInterface dialog, int which, boolean isChecked) {
Toast.makeText(MainActivity.this, " 被点击了 : " + items[which] +",位置: " +which+", 值是: "+ isChecked, 0 ).show();
}
});
builder.show();
}
  • 用ContentValues来实现增删改查
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
Student.java
package domain;
public class Student {
private String id;
private String name;
private String sex;
public Student() {
super();
// TODO Auto-generated constructor stub
}
public Student(String id, String name, String sex) {
super();
this.id = id;
this.name = name;
this.sex = sex;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
@Override
public String toString() {
return "Student [id=" + id + ", name=" + name + ", sex=" + sex + "]";
}
}
StudentDao.java
package com.example.studentsystemdemo3;
import domain.Student;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
/*
*
* 增删改查有两套api了
*
* 第一套(你经常使用的):
* 增删改:db.execSQL();
* 查询:db.rawQuery();
*
* 第二套(推荐使用):
*
* 增:db.insert();
* 删:db.delete();
* 改:db.update();
* 查询:db.query();
*
* 实际开发过程中:数据库到底用的多不多?不多 ...
*
* 提供的保存数据的方式很多 :
*
* 保存数据的方式 :
* 1. 应用内部的私有的文件夹下 : /data/data/net.itceo.hello/files/cache
* 2. sharedPreference : /data/data/net.itceo.hello/shar_prefs
* 3. 在 sd 卡公共的部分 : /mnt/sdcard
* 4. 存到数据库 中 : sqlite 数据库
*
*
*
*
*/
public class StudentDao {
StudentDbOpenHelper helper;
public StudentDao(Context context){
helper = new StudentDbOpenHelper(context);
}
public void add(Student st){
//拿到 工具类的实例 , 然后去操作 数据库
SQLiteDatabase db = helper.getWritableDatabase();
ContentValues values = new ContentValues();
values.put("name", st.getName());
values.put("sex", st.getSex());
db.insert("students", null, values);
}
public void delete(String id){
SQLiteDatabase db = helper.getWritableDatabase();
db.delete("students", "_id=?", new String[]{id});
}
public void update(Student st){
SQLiteDatabase db = helper.getWritableDatabase();
ContentValues values = new ContentValues();
values.put("name", st.getName());
values.put("sex", st.getSex());
db.update("students", values, "_id=?", new String[]{st.getId()});
}
public Student find(String id){
SQLiteDatabase db = helper.getReadableDatabase();
db.query("students", new String[]{"_id","name","sex"}, "_id=?", new String[]{id}, null, null, null);
Cursor cursor = db.rawQuery("select * from students where _id=?", new String[]{id});
boolean result = cursor.moveToNext();
Student st = null;
if(result){
int _id = cursor.getInt(cursor.getColumnIndex("_id"));
String name = cursor.getString(cursor.getColumnIndex("name"));
String sex = cursor.getString(cursor.getColumnIndex("sex"));
st = new Student(String.valueOf(_id),name,sex);
}
// 最后会释放 资源
cursor.close();
return st;
}
}
StudentDbOpenHelper.java
package com.example.studentsystemdemo3;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
public class StudentDbOpenHelper extends SQLiteOpenHelper {
public StudentDbOpenHelper(Context context){
super(context, "student.db", null, 1);
}
@Override
public void onCreate(SQLiteDatabase db) {
//创建数据库
db.execSQL("create table students (_id integer primary key autoincrement, name varchar(30),sex varchar(10))");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
}
TestStudentDao.java
package com.example.studentsystemdemo3;
import domain.Student;
import android.test.AndroidTestCase;
public class TestStudentDao extends AndroidTestCase {
public void testAdd(){
Student st = new Student("8798", "二球", "女");
StudentDao sdao = new StudentDao(getContext());
sdao.add(st);
}
}
//注意:写测试类需要在AndroidMainfest.xml 下声明instrumentation和uses-libray