|
AndroidDataFramework 是Android平台上一个数据库定义框架,通过该框架可以简单的通过XML文件来定义Sqlite数据库. AndroidDataFramework 刚发布1.0版本,功能还不是很完善,但也可以简单使用了.下面就来看看如何使用:
下载类库:
http://androiddataframework.googlecode.com/files/com.android.DataFramework.jar
如果你想查看示例代码可以下载这个文件:
http://androiddataframework.googlecode.com/files/Prueba%20DataFramework.tar.gz
本教程使用 的示例代码就是来自于上面的文件中.
下载好类库后,用Eclipse新建Android项目,并且把下载后的jar库加入类建构路径中.
这样环境就设置好了.
1.sqlite数据库的定义:
要定义Sqlite数据库,只需要创建如下文件:
res/xml/tables.xml
注意: 定义数据库表结构的文件必须位于res/xml/目录下且文件名字必需为 tables.xml.
很明显这是一个XML文件,里面定义了数据库表结构.
<!-- 使用该文件来定义数据库和数据表,这里定义了数据库名字为notes_db,两个数据表 notes和categories -->
<DATABASE version="1" name="notes_db">
<TABLE class=ke-zeroborder name="notes" to-string="%title%">
<FIELD name="title" size="128" type="text" obligatory="true">
<FIELD name="body" type="text" obligatory="true">
<FIELD name="category_id" type="foreign-key" foreign-table="categories"></FIELD>
</FIELD>
</FIELD>
</TABLE>
<TABLE class=ke-zeroborder name="categories" to-string="%name%description%"
backup="no">
<FIELD name="name" size="128" type="string-identifier"
obligatory="true">
<FIELD name="description" size="128" type="text" obligatory="true"></FIELD>
</FIELD>
</TABLE>
</DATABASE>
注意上面 table 中的 to-string 属性的定义:在 AndroidDataFramework 中,每条表记录用一个Entity来表示, to-string定义了用那些表中的字段来显示toString信息.如果有多个字段用%分割,例如第一个表格中的定义:
to-string="%name%description%"
field中的obligatory="true"属性指定该字段是否为必需的; 而type="string-identifier"指定该属性的类型,目前type的类型有 string-identifier 和 text(在定义数据表初始数据的时候使用):
string-identifier:说明该字段的取值来自于 res/values/strings.xml 中定义的文字内容
text:说明该字段的内容就是里面的文本
2 sqlite数据库表初始数据的定义:
初始化数据在如下文件中定义:
res/xml/initialvalues_v1.xml
文件名中的initialvalues_是不能修改的,而后面的v1代表数据库的版本号.
<VALUES> <!-- table 指定数据所属的表格, id定义该数据的id值 -->
<ROW id=1 table="categories">
<!-- 由于在tables.xml文件中定义的name field的类型为string-identifier,说明其初始化值取之于strings.xml中的定义
所以这里的name值对应为strings.xml中work的字符串值.
-->
<FIELD name="name" value="work">
<!-- description的类型为text,为这里定义的字符串 -->
<FIELD name="description" value=" for work"> </FIELD>
<ROW id=2 table="categories">
<FIELD name="name" value="friends">
<FIELD name="description" value=" for friends"> </FIELD>
</FIELD>
</ROW>
</FIELD>
</ROW>
</VALUES>
AndroidDataFramework 的使用请参考下面的Java代码:
package org.goodev;
import android.app.ListActivity;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import com.android.dataframework.DataFramework;
public class PruebaDataFramework extends ListActivity {
private static final int ACTIVITY_CREATE=0;
private static final int ACTIVITY_EDIT=1;
private static final int INSERT_ID = Menu.FIRST;
private static final int DELETE_ID = Menu.FIRST + 1;
private static final int BACKUP_ID = Menu.FIRST + 2;
private static final int RESTORE_ID = Menu.FIRST + 3;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.notes_list);
try {
//初始化数据库,org.goodev为项目的package名字
DataFramework.getInstance().open(this, "org.goodev");
} catch (Exception e) {
e.printStackTrace();
}
fillData();
}
private void fillData() {
//得到notes表格的Cursor
Cursor notesCursor = DataFramework.getInstance().getCursor("notes");
//manage该Cursor
startManagingCursor(notesCursor);
String[] from = new String[] {"title"};
int[] to = new int[]{R.id.text1};
SimpleCursorAdapter notes =
new SimpleCursorAdapter(this, R.layout.notes_row, notesCursor, from, to);
setListAdapter(notes);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
menu.add(0, INSERT_ID, 0, R.string.menu_insert);
menu.add(0, DELETE_ID, 0, R.string.menu_delete);
menu.add(0, BACKUP_ID, 0, "Backup");
menu.add(0, RESTORE_ID, 0, "Restore");
return true;
}
@Override
public boolean onMenuItemSelected(int featureId, MenuItem item) {
switch(item.getItemId()) {
case INSERT_ID:
createNote();
return true;
case BACKUP_ID:
try {
//备份数据到xml文件的功能
DataFramework.getInstance().backup("/sdcard/backup-notes.xml");
} catch (Exception e) {
}
return true;
case RESTORE_ID:
try {
DataFramework.getInstance().emptyTablesBackup();
//恢复数据的功能
DataFramework.getInstance().restore("/sdcard/backup-notes.xml");
fillData();
} catch (Exception e) {
}
return true;
case DELETE_ID:
//清空数据表
DataFramework.getInstance().emptyTable("notes");
fillData();
return true;
}
return super.onMenuItemSelected(featureId, item);
}
private void createNote() {
Intent i = new Intent(this, NoteEdit.class);
startActivityForResult(i, ACTIVITY_CREATE);
}
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
Intent i = new Intent(this, NoteEdit.class);
i.putExtra(DataFramework.KEY_ID, id);
startActivityForResult(i, ACTIVITY_EDIT);
}
@Override
protected void onActivityResult(int requestCode, int resultCode,
Intent intent) {
super.onActivityResult(requestCode, resultCode, intent);
fillData();
}
}
AndroidDataFramework 项目主页:http://code.google.com/p/androiddataframework/ |
|