效果圖示例android
一、在清單裏添加相應的權限sql
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>數據庫
-----------------------------------------函數
二、在res文件夾下建立一個菜單文件夾menu工具
該菜單文件夾有2個菜單佈局文件佈局
add_menu.xml菜單佈局文件.net
代碼sqlite
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:id="@+id/adddata_menu"
android:title="添加學生信息"/>xml
</menu>blog
-------------------------
list_menu.xml菜單佈局文件
代碼
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="@+id/list_delete"
android:title="刪除"/>
<item
android:id="@+id/list_change"
android:title="修改"/>
<item
android:id="@+id/list_no"
android:title="不操做"/>
</menu>
======================
三、佈局界面 -- 3個佈局文件
1) activity_main.xml -- 只有一個ListView控件
2)item_activity.xml -- 有3個TextView 控件 用來顯示 性別 年齡 分數
3)editdata_activity.xml -- 有3個EditText控件 用來添加 修改數據的編輯框
activity_main.xml佈局文件
代碼
<RelativeLayout 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"
tools:context="${relativePackage}.${activityClass}" >
<ListView
android:id="@+id/listview"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</RelativeLayout>
================================
item_activity.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="horizontal" >
<TextView
android:id="@+id/item_name"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:textSize="30sp"
android:text="dddd"/>
<TextView
android:id="@+id/item_sex"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:textSize="30sp"
android:text="dddd"/>
<TextView
android:id="@+id/item_age"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:textSize="30sp"
android:text="dddd"/>
</LinearLayout>
=================================
editdata_activity.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" >
<EditText
android:id="@+id/edit_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="姓名"/>
<EditText
android:id="@+id/edit_sex"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="性別"/>
<EditText
android:id="@+id/edit_age"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="年齡"/>
</LinearLayout>
================================
四、有2個類 一個工具類 用來對數據庫的增刪改查的工做 一個MainActivity類
工具類
代碼
public class Sqlite_operate_utils {
//File.separator -- 至關於 /
private static String DB_PATH = Environment.getExternalStorageDirectory() + File.separator + "stuentinfo.db";
private SQLiteDatabase db;
//構造函數 new 該類的時候 就去找 須要找的 數據庫
public Sqlite_operate_utils() {
db = SQLiteDatabase.openDatabase(DB_PATH, null, SQLiteDatabase.OPEN_READWRITE);
}
//查詢 數據的 方法1
public Cursor sqlite_select(String content, String[] condition){
return db.rawQuery(content, condition);
}
//查詢 數據 的 方法2
public List<Map<String, String>> sqlite_selectlist(String content, String[] condition){
// Log.i("data", "cursor:");
Cursor cursor = db.rawQuery(content, condition);
return cursorToList(cursor);
}
//返回List
public List<Map<String, String>> cursorToList(Cursor cursor) {
List<Map<String, String>> list = new ArrayList<Map<String,String>>();
while(cursor.moveToNext()){//數據庫表的 行
Map<String, String> map = new HashMap<String, String>();
for(int i = 0;i<cursor.getColumnCount();i++){//數據庫表的列
map.put(cursor.getColumnName(i), cursor.getString(i));
}
list.add(map);
}
cursor.close();
// Log.i("data", "list:" + list.size());
return list;
}
//增刪改 的方法
//返回布爾型 方便 查看 數據 操做 是否成功
public boolean executeData(String execute_content, Object[] bindArgs){
try {
if(bindArgs == null){//要綁定佔位符 的參數值
db.execSQL(execute_content);
return true;
}else{
db.execSQL(execute_content, bindArgs);
return true;
}
} catch (SQLException e) {
e.printStackTrace();
return false;
}
}
//關閉db
public void destroy(){
if(db != null){
db.close();
}
}
}
============================
MainActivity 類
代碼在 -- sqlite數據庫的基本增刪改查操做MainActivity類代碼
連接:http://my.oschina.net/u/2542711/blog/608649