sqlite数据库储存图片信息,GridView显示,基本实现添加、删除图标操作
效果图如下:
废话不多说,直接上代码:
MainActivity.java
[代码]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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
@H_210_301@
133
134
@H_403_305@
135
136
137
138
139
|
package com.database.sqlite;
import java.util.List;
import android.app.Activity;
import android.app.ActivityManager;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;
/**
* 主菜单类
*
*
*
* @author 小赵
*
*/
public class MainActivity extends Activity {
private GridView gridView;
private PictureAdapter adapter;
private DBContror dbcon;
private List<Picture> mylist;
private Intent intent;
@Override
public void onCreate(Bundle savedInstanceState) {
super
.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
setContentView(R.layout.main);
getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE,R.layout.title);
TextView tv = (TextView) findViewById(R.id.textTile);
tv.setText(R.string.main_menu);
intent =
new
Intent();
gridView = (GridView) findViewById(R.id.gridview);
gridView.setAdapter(adapter);
gridView.setOnItemClickListener(
new
OnItemClickListener() {
public void onItemClick(AdapterView<?> parent,View v,
int position,long id) {
OnClickGridViewItem(position);
// 点击事件
}
});
}
/**
* GridView中图标点击事件
*
* @param position
*/
protected void OnClickGridViewItem(int position) {
String result = mylist.get(position).getTitle();
if
(result.endsWith(
"商城"
)) {
// 进入AppStore
intent.setClass(MainActivity.
this
,AppStoreActivity.class);
MainActivity.
this
.startActivity(intent);
}
else
{
Toast.makeText(MainActivity.
this
,mylist.get(position).getTitle(),
Toast.LENGTH_SHORT).show();
}
}
@Override
protected void onResume() {
super
.onResume();
mylist = dbcon.queryAllItem();
adapter =
new
PictureAdapter(mylist,
this
);
adapter.notifyDataSetChanged();
gridView.setAdapter(adapter);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
return
true
;
}
/**
*/
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch
(item.getItemId()) {
case
R.id.About:
Toast.makeText(MainActivity.
this
,
"关于"
,Toast.LENGTH_SHORT).show();
break
;
case
R.id.Exit:
// Toast.makeText(MainActivity.this,"退出",Toast.LENGTH_SHORT).show();
ActivityManager am = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
am.restartPackage(getPackageName());
intent.setClass(
this
,MainActivity.class);
Intent i =
new
Intent(Intent.ACTION_MAIN);
i.addCategory(Intent.CATEGORY_HOME);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
startActivity(i);
break
;
default
:
break
;
}
return
false
;
}
/**
* 管理应用(删除操作)
*
* @param view
*/
public void OnManage(View view) {
intent.setClass(MainActivity.
this
,ManageActivity.class);
this
.startActivity(intent);
}
/**
* 禁用返回键
*/
public void onBackPressed() {
}
}
|
主菜单布局XML
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
|
<?xml version=
"1.0"
encoding=
"utf-8"
?>
<LinearLayout xmlns:android=
"http://schemas.android.com/apk/res/android"
android:layout_width=
"fill_parent"
android:layout_height=
"fill_parent"
android:layout_gravity=
"left"
android:background=
"@drawable/top_image"
android:orientation=
"vertical"
android:weightSum=
"2"
>
<LinearLayout
android:layout_width=
"fill_parent"
android:layout_height=
"wrap_content"
android:background=
"@drawable/top_image"
android:orientation=
"horizontal"
>
<!-- 帮助按钮 -->
<Button
android:layout_width=
"wrap_content"
android:layout_height=
"wrap_content"
android:layout_gravity=
"bottom|center"
android:layout_weight=
"1"
android:background=
"@drawable/btn_home_bg"
android:gravity=
"center"
android:text=
"@string/help"
android:textColor=
"#000000"
android:textSize=
"15dip"
/>
<TextView
android:layout_width=
"wrap_content"
android:layout_height=
"wrap_content"
android:layout_weight=
"2"
/>
<!-- 返回主页按钮 -->
<Button
android:layout_width=
"wrap_content"
android:layout_height=
"wrap_content"
android:layout_gravity=
"bottom|center"
android:layout_weight=
"1"
android:background=
"@drawable/btn_home_bg"
android:gravity=
"center"
android:onClick=
"OnManage"
android:text=
"@string/manage"
android:textColor=
"#000000"
android:textSize=
"15dip"
/>
</LinearLayout>
<GridView
android:id=
"@+id/gridview"
android:layout_width=
"fill_parent"
android:layout_height=
"fill_parent"
android:columnWidth=
"80dip"
android:gravity=
"center"
android:horizontalSpacing=
"5dip"
android:numColumns=
"auto_fit"
android:stretchMode=
"columnWidth"
android:verticalSpacing=
"5dip"
/>
</LinearLayout>
|
标题布局:
title.xml
[代码]xml代码:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
<?xml version=
"1.0"
encoding=
"utf-8"
?>
<LinearLayout xmlns:android=
"http://schemas.android.com/apk/res/android"
android:layout_width=
"wrap_content"
android:layout_height=
"wrap_content"
android:layout_gravity=
"center"
>
<TextView
android:id=
"@+id/textTile"
android:layout_width=
"wrap_content"
android:layout_height=
"wrap_content"
android:textColor=
"#FFFFFF"
android:textStyle=
"bold"
android:textSize=
"18dip"
/>
</LinearLayout>
|
菜单选项布局:
main.xml
[代码]xml代码:
1
2
3
4
5
6
7
8
9
10
11
12
13
|
<item
android:id=
"@+id/About"
android:orderInCategory=
"100"
android:title=
"@string/menu_about"
/>
<item
android:id=
"@+id/Exit"
android:orderInCategory=
"100"
android:title=
"@string/menu_cancel"
/>
</menu>
|
创建数据类:
DBHelper.java
[代码]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
|
package com.database.sqlite;
import android.content.Context;
/**
* 创建数据库
*
* @author 小赵
*
*/
public class DBHelper extends sqliteOpenHelper {
public DBHelper(Context context) {
super
(context,Gloable.DB_NAME,
null
,Gloable.DB_VERSION);
}
/**
* 创建数据库
*/
@Override
public void onCreate(sqliteDatabase db) {
+ Gloable.TABLE_NAME
+
"(id INTEGER PRIMARY KEY NOT NULL,pictureId INTEGER NOT NULL,pictureName varchar not null)"
;
}
@Override
public void onUpgrade(sqliteDatabase arg0,int arg1,int arg2) {
}
}
|
数据库操作类:
DBContror.java
[代码]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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
@H_210_301@
133
134
@H_403_305@
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
|
package com.database.sqlite;
import java.util.ArrayList;
import java.util.List;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
/**
* 数据库操作类
*
* @author 小赵
*
*/
public class DBContror {
private DBHelper picdb;
private List<Picture> all =
new
ArrayList<Picture>();
public DBContror(Context context) {
picdb =
new
DBHelper(context);
if
(all.size() > 0) {
}
else
{
initDB(getData());
}
}
/**
* 数据库初始化
*
* @param value
* @return
*/
private long initDB(List<Picture> value) {
long row = 0;
sqliteDatabase db = picdb.getWritableDatabase();
picdb.onCreate(db);
ContentValues values =
null
;
Picture map =
null
;
try
{
while
(value.size() > 0) {
map = value.remove(0);
values =
new
ContentValues();
values.put(
"pictureId"
,map.getImageId());
values.put(
"pictureName"
,map.getTitle());
row = db.insert(Gloable.TABLE_NAME,values);
}
}
catch
(Exception e) {
e.printStackTrace();
} finally {
if
(db.isOpen()) {
db.close();
}
}
return
row;
}
/**
* 添加数据
*
* @param picBean
* @return
*/
public long insertItem(Picture picBean) {
long row = 0;
try
{
sqliteDatabase db = picdb.getWritableDatabase();
picdb.onCreate(db);
ContentValues contentValues =
new
ContentValues();
contentValues.put(
"pictureId"
,picBean.getImageId());
contentValues.put(
"pictureName"
,picBean.getTitle());
row = db.insert(Gloable.TABLE_NAME,contentValues);
db.close();
}
catch
(Exception e) {
}
return
row;
}
/**
*
* @param name
* @return
*/
public int deleteById(String name) {
int i = 0;
try
{
String[] sb =
new
String[1];
sb[0] = name;
sqliteDatabase db = picdb.getWritableDatabase();
picdb.onCreate(db);
i = db.
delete
(Gloable.TABLE_NAME,
"pictureName=?"
,sb);
}
catch
(Exception e) {
}
return
i;
}
/**
*
* @param name
* @return
*/
public boolean checkByName(String name) {
Cursor cursor;
try
{
sqliteDatabase db = picdb.getWritableDatabase();
picdb.onCreate(db);
cursor = db.query(Gloable.TABLE_NAME,
new
String[] { name },
null
);
cursor.moveToFirst();
int count = cursor.getCount();
db.close();
if
(count > 0) {
cursor.close();
return
true
;
}
cursor.close();
}
catch
(Exception e) {
}
return
false
;
}
/**
*
* @return
*/
public List<Picture> queryAllItem() {
sqliteDatabase db = picdb.getReadableDatabase();
picdb.onCreate(db);
List<Picture> pictures =
new
ArrayList<Picture>();
try
{
Cursor cursor = db.query(Gloable.TABLE_NAME,
null
,
null
);
while
(cursor.moveToNext()) {
Picture picture =
new
Picture();
picture.setImageId(cursor.getInt(cursor
.getColumnIndex(
"pictureId"
)));
picture.setTitle(cursor.getString(cursor
.getColumnIndex(
"pictureName"
)));
pictures.add(picture);
}
cursor.close();
db.close();
}
catch
(Exception e) { }
return
pictures;
}
/**
*/
public boolean checkPic(Picture picture) {
try
{
sqliteDatabase db = picdb.getWritableDatabase();
picdb.onCreate(db);
String clientCode = picture.getTitle();
Cursor cursor = db.query(Gloable.TABLE_NAME,
new
String[] { clientCode },
null
);
cursor.moveToFirst();
int count = cursor.getCount();
db.close();
if
(count > 0) {
cursor.close();
return
true
;
}
cursor.close();
}
catch
(Exception e) { }
return
false
;
}
/**
* 初始化添加系统数据
*
* @return
*/
private List<Picture> getData() {
List<Picture> list =
new
ArrayList<Picture>();
for
(int i = 0; i < Gloable.IMAGES.length; i++) {
Picture picture =
new
Picture();
picture.setImageId(Gloable.IMAGES[i]);
picture.setTitle(Gloable.TITLES[i]);
list.add(picture);
}
return
list;
}
}
|
适配器类PictureAdapter.java
[代码]java代码:
@H_129_4044@
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
|
package com.database.sqlite;
import java.util.List;
import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
/**
* 适配器类PictureAdapter
*
* @author 小赵
*
*/
public class PictureAdapter extends BaseAdapter {
List<Picture> pictures;
Context context;
public PictureAdapter(List<Picture> pic_list,Context context) {
super
();
this
.pictures = pic_list;
this
.context = context;
}
@Override
public int getCount() {
if
(
null
!= pictures) {
return
pictures.size();
}
else
{
return
0;
}
}
@Override
public Object getItem(int position) {
return
pictures.get(position);
}
@Override
public long getItemId(int position) {
return
position;
}
@Override
public View getView(int position,View convertView,ViewGroup parent) {
LayoutInflater inflater = LayoutInflater.from(context);
ViewHolder viewHolder;
if
(convertView ==
null
) {
convertView = inflater.inflate(R.layout.picture_item,
null
);
viewHolder =
new
ViewHolder();
viewHolder.title = (TextView) convertView.findViewById(R.id.title);
viewHolder.image = (ImageView) convertView.findViewById(R.id.image);
convertView.setTag(viewHolder);
}
else
{
viewHolder = (ViewHolder) convertView.getTag();
}
viewHolder.title.setText((CharSequence) pictures.get(position)
.getTitle());
viewHolder.title.setTextColor(Color.BLACK);
viewHolder.image.setImageResource(pictures.get(position).getImageId());
return
convertView;
}
}
class ViewHolder {
public TextView title;
public ImageView image;
}
|