SQLite数据库储存图片信息,GridView显示,基本实现添加、删除图标操作 ... ...

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;
/**
* 主菜单类
*
*
*
* @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);
       
        dbcon = new DBContror( this ); // 初始化数据库
        mylist = dbcon.queryAllItem(); // 查询数据库中的全部数据,并存放到List<Picture>中
       
        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); // 点击事件
            }
        });
    }
    /**
     * 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) {
        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:
            // 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
        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
    <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;
/**
* 创建数据库
*
* @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) {
        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;
/**
* 数据库操作类
*
* @author 小赵
*
*/
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());
        }
    }
    /**
     * 数据库初始化
     *
     * @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, null , 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, null , contentValues);
            db.close();
        } catch (Exception e) {
        }
        return row;
    }
    /**
     * 按名称删除指定名称(name)的数据
     *
     * @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;
    }
    /**
     * 查询数据库中是否存在名称为name的数据
     *
     * @param name
     * @return
     */
    public boolean checkByName(String name) {
        Cursor cursor;
        try {
            SQLiteDatabase db = picdb.getWritableDatabase();
            picdb.onCreate(db);
            cursor = db.query(Gloable.TABLE_NAME, null , "pictureName=?" ,
                    new String[] { name }, null , null , 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 , null ,
                    null , 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;
    }
    /**
     * 查看picture数据库是否已经存在该图片
     */
    public boolean checkPic(Picture picture) {
        try {
            SQLiteDatabase db = picdb.getWritableDatabase();
            picdb.onCreate(db);
            String clientCode = picture.getTitle();
            Cursor cursor = db.query(Gloable.TABLE_NAME, null , "pictureName=?" ,
                    new String[] { clientCode }, null , null , 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代码:

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


图片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; //图片ID(位置)
    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;
/**
* 管理界面(用于删除操作)
*
* @author 小赵
*
*/
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, View v,
                    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;
/**
* 商城
*
* @author 小赵
*
*/
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, View v,
                    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);
            }
        });
    }
    /**
     * 返回
     *
     * @param view
     */
    public void onBack(View view) {
        intent.setClass(AppStoreActivity. this , MainActivity.class);
        AppStoreActivity. this .startActivity(intent);
    }
    /**
     * 返回主页
     *
     * @param view
     */
    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;
/**
* 全局资源类
*
* @author 小赵
*
*/
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,希望各位大虾多多指教,相互学习!!!

最后附上工程结构图:

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值