sqlite数据库储存图片信息,GridView显示,基本实现添加、删除图标操作@H_502_3@
@H_502_3@
@H_502_3@
@H_502_3@
@H_502_3@
@H_502_3@
@H_502_3@
MainActivity.java@H_502_3@
[代码]java代码:
1
2
3
4
5
6
7
8
@H_404_53@
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_976_301@
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;
/**
* 主菜单类
*
*
*
* @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() {
}
}
|
@H_502_3@
main.xml@H_502_3@
[代码]xml代码:
1
2
3
4
5
6
7
8
@H_404_53@
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>
|
@H_502_3@
title.xml@H_502_3@
[代码]xml代码:
1
2
3
4
5
6
7
8
@H_404_53@
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>
|
@H_502_3@
main.xml@H_502_3@
[代码]xml代码:
创建数据类:@H_502_3@
DBHelper.java@H_502_3@
[代码]java代码:
1
2
3
4
5
6
7
8
@H_404_53@
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
|
* @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) {
}
}
|