SQLite数据库储存图片信息,GridView显示,基本实现添加、删除图
发布时间:2020-12-12 20:20:11 所属栏目:百科 来源:网络整理
导读: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 3
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
133
134
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;
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);
dbcon =
new
DBContror(
this
);
mylist = dbcon.queryAllItem();
intent =
new
Intent();
gridView = (GridView) findViewById(R.id.gridview);
adapter =
new
PictureAdapter(mylist,
this
);
gridView.setAdapter(adapter);
gridView.setOnItemClickListener(
new
OnItemClickListener() {
public void onItemClick(AdapterView<?> parent,View v,
int position,long id) {
OnClickGridViewItem(position);
}
});
}
protected void OnClickGridViewItem(int position) {
String result = mylist.get(position).getTitle();
if
(result.endsWith(
"商城"
)) {
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) {
getMenuInflater().inflate(R.menu.main,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:
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
;
}
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
xmlns:android=
"http://schemas.android.com/apk/res/android"
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
xmlns:android=
"http://schemas.android.com/apk/res/android"
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
<menu xmlns:android=
"http://schemas.android.com/apk/res/android"
>
<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;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
public class DBHelper extends SQLiteOpenHelper {
public DBHelper(Context context) {
super
(context,Gloable.DB_NAME,
null
,Gloable.DB_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
String sql =
"CREATE TABLE IF NOT EXISTS "
+ Gloable.TABLE_NAME
+
"(id INTEGER PRIMARY KEY NOT NULL,pictureId INTEGER NOT NULL,pictureName varchar not null)"
;
db.execSQL(sql);
}
@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
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
package com.database.sqlite;
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;
public class DBContror {
private DBHelper picdb;
private List<Picture> all =
new
ArrayList<Picture>();
public DBContror(Context context) {
picdb =
new
DBHelper(context);
all = queryAllItem();
if
(all.size() > 0) {
}
else
{
initDB(getData());
}
}
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;
}
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;
}
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;
}
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
;
}
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
;
}
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代码:
?
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;
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;
}
图片Bean类:
Picture.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
package com.database.sqlite;
public class Picture {
private String title;
private int imageId;
public Picture() {
super
();
}
public Picture(String title,int imageId) {
super
();
this
.title = title;
this
.imageId = imageId;
}
public String getTitle() {
return
title;
}
public void setTitle(String title) {
this
.title = title;
}
public int getImageId() {
return
imageId;
}
public void setImageId(int imageId) {
this
.imageId = imageId;
}
}
管理界面类:
ManageActivity.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
package com.database.sqlite;
import java.util.List;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.GridView;
import android.widget.TextView;
import android.widget.Toast;
public class ManageActivity extends Activity {
private GridView gridView;
private PictureAdapter adapter;
private Intent intent;
private DBContror dbcon;
private List<Picture> mylist;
@Override
protected void onCreate(Bundle savedInstanceState) {
super
.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
setContentView(R.layout.manage);
getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE,R.layout.title);
TextView tv = (TextView) findViewById(R.id.textTile);
tv.setText(R.string.
delete
);
dbcon =
new
DBContror(
this
);
mylist = dbcon.queryAllItem();
intent =
new
Intent();
gridView = (GridView) findViewById(R.id.gridview);
adapter =
new
PictureAdapter(mylist,
this
);
gridView.setAdapter(adapter);
gridView.setOnItemClickListener(
new
OnItemClickListener() {
public void onItemClick(AdapterView<?> parent,
int position,long id) {
if
(position < 2) {
Toast.makeText(ManageActivity.
this
,
"系统应用,无法删除!正在返回请稍后……"
,
Toast.LENGTH_SHORT).show();
}
else
{
dbcon.deleteById(mylist.get(position).getTitle());
adapter.notifyDataSetChanged();
Toast.makeText(ManageActivity.
this
,
"删除成功!正在返回请稍后……"
,
Toast.LENGTH_SHORT).show();
}
onBack(
null
);
}
});
}
public void onBack(View view) {
intent.setClass(ManageActivity.
this
,MainActivity.class);
ManageActivity.
this
.startActivity(intent);
}
public void onMain(View view) {
intent.setClass(ManageActivity.
this
,MainActivity.class);
ManageActivity.
this
.startActivity(intent);
}
public void onBackPressed() {
}
}
管理布局类:
Manage.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:orientation=
"vertical"
android:weightSum=
"2"
>
<LinearLayout
xmlns:android=
"http://schemas.android.com/apk/res/android"
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:gravity=
"center"
android:text=
"@string/back"
android:background=
"@drawable/btn_home_bg"
android:onClick=
"onBack"
android:textColor=
"#FF0000"
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:gravity=
"center"
android:text=
"@string/main"
android:onClick=
"onMain"
android:background=
"@drawable/btn_home_bg"
android:textColor=
"#FF0000"
android:textSize=
"15dip"
/>
</LinearLayout>
<GridView
xmlns:android=
"http://schemas.android.com/apk/res/android"
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>
应用商城类:
AppStoreActivity.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
package com.database.sqlite;
import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
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;
public class AppStoreActivity extends Activity {
private GridView gridView;
private PictureAdapter adapter;
private Intent intent;
private List<Picture> mylist;
private DBContror dbcon;
public String[] addtitles =
new
String[] {
"博客"
,
"历史"
,
"计算器"
,
"分享"
,
"微博"
,
"时钟"
};
public Integer[] addimages =
new
Integer[] { R.drawable.blog,
R.drawable.history,R.drawable.calculator,R.drawable.share,
R.drawable.weibo,R.drawable.time };
@Override
protected void onCreate(Bundle savedInstanceState) {
super
.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
setContentView(R.layout.manage);
getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE,R.layout.title);
TextView tv = (TextView) findViewById(R.id.textTile);
tv.setText(R.string.add);
dbcon =
new
DBContror(
this
);
intent =
new
Intent();
gridView = (GridView) findViewById(R.id.gridview);
mylist =
new
ArrayList<Picture>();
for
(int i = 0; i < addtitles.length; i++) {
Picture newPic =
new
Picture();
newPic.setImageId(addimages[i]);
newPic.setTitle(addtitles[i]);
mylist.add(newPic);
}
adapter =
new
PictureAdapter(mylist,
this
);
gridView.setAdapter(adapter);
gridView.setOnItemClickListener(
new
OnItemClickListener() {
public void onItemClick(AdapterView<?> parent,
int position,long id) {
if
(dbcon.checkByName(mylist.get(position).getTitle())) {
Toast.makeText(AppStoreActivity.
this
,
"该应用已经存在无法再次添加!正在返回请稍后……"
,Toast.LENGTH_SHORT)
.show();
}
else
{
dbcon.insertItem(mylist.get(position));
adapter.notifyDataSetChanged();
Toast.makeText(AppStoreActivity.
this
,
"添加成功!正在返回请稍后……"
,
Toast.LENGTH_SHORT).show();
}
intent.setClass(AppStoreActivity.
this
,MainActivity.class);
AppStoreActivity.
this
.startActivity(intent);
}
});
}
public void onBack(View view) {
intent.setClass(AppStoreActivity.
this
,MainActivity.class);
AppStoreActivity.
this
.startActivity(intent);
}
public void onMain(View view) {
intent.setClass(AppStoreActivity.
this
,MainActivity.class);
AppStoreActivity.
this
.startActivity(intent);
}
public void onBackPressed() {
}
}
商城布局和管理布局一样,可以共用一个,呵呵……
全局变量类:
Gloable.java
[代码]java代码:
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
package com.database.sqlite;
public class Gloable {
public static String[] TITLES =
new
String[] {
"商城"
,
"测试1"
,
"测试2"
,
"测试3"
};
public static Integer[] IMAGES =
new
Integer[] { R.drawable.appstore,
R.drawable.test1,R.drawable.test2,R.drawable.test3 };
public static String DB_NAME =
"pictureDB"
;
public static String TABLE_NAME =
"picture"
;
public static int DB_VERSION = 1;
}
图片item类:
picture_item.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
<?xml version=
"1.0"
encoding=
"utf-8"
?>
<LinearLayout xmlns:android=
"http://schemas.android.com/apk/res/android"
android:id=
"@+id/root"
android:layout_width=
"wrap_content"
android:layout_height=
"wrap_content"
android:orientation=
"vertical"
>
<TextView
android:layout_width=
"wrap_content"
android:layout_height=
"wrap_content"
/>
<ImageView
android:id=
"@+id/image"
android:layout_width=
"wrap_content"
android:layout_height=
"wrap_content"
android:layout_gravity=
"center"
android:contentDescription=
"@string/app_name"
android:padding=
"4dp"
android:scaleType=
"fitXY"
/>
<TextView
android:id=
"@+id/title"
android:layout_width=
"wrap_content"
android:layout_height=
"wrap_content"
android:layout_gravity=
"center"
android:gravity=
"center_horizontal"
/>
</LinearLayout>
String类:
string.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
<resources>
<string name=
"app_name"
>SQLiteDemo</string>
<string name=
"hello_world"
>Hello world!</string>
<string name=
"menu_settings"
>设置</string>
<!-- 界面标题 -->
<string name=
"main_menu"
>主菜单</string>
<string name=
"shop_list"
>商城应用</string>
<string name=
"local_list"
>本地应用</string>
<!-- 标题上的选择按钮 -->
<string name=
"help"
>帮助</string>
<string name=
"main"
>主页</string>
<string name=
"back"
>返回</string>
<string name=
"manage"
>管理</string>
<string name=
"delete"
>点击图标删除</string>
<string name=
"add"
>点击图标添加应用</string>
<!-- 菜单功能键下的控件 -->
<string name=
"menu_about"
>关于</string>
<string name=
"menu_cancel"
>退出</string>
</resources>
最后需要添加如下权限:
[代码]xml代码:
?
1
<uses-permission android:name=
"android.permission.RESTART_PACKAGES"
/>
资源图片呢,没有找到在哪里添加附件,以后再说……只是数据库操作的小Demo,希望各位大虾多多指教,相互学习!!!
最后附上工程结构图:
(编辑:李大同)
【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!