今天找到了一个数据库操作框架,看了一上午别人的文档,自己动手也写了个demo,便于以后查阅。
项目下载地址
public class ExampleDaoGenerator {
public static void main(String[] args) throws Exception {
//该方法第一个参数用来更新数据库版本号,第二个参数为要生成的DAO类所在包路径。
Schema schema = new Schema(1000, "com.example.greendaodemo");
//创建一个实体类
Entity user=schema.addEntity("User");
//默认表名就是类名,也可以自定义表名
//user.setTableName("UserList");
//设置一个自增长ID列为主键
user.addIdProperty().autoincrement();
user.addStringProperty("name").notNull();//非null字段
new DaoGenerator().generateAll(schema, "../GreenDaoDemo/src-gen");//GreenDaoDemo是新建的android项目名
}
}
这里我新建了User表,有两个字段id和name。 接下来,运行这个java工程
public class MainActivity extends ActionBarActivity {
private SQLiteDatabase db;
private DaoMaster daoMaster;
private DaoSession daoSession;
private UserDao userDao;
private Cursor cursor;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
DevOpenHelper helper = new DaoMaster.DevOpenHelper(this, "users-db",
null);
db = helper.getWritableDatabase();
daoMaster = new DaoMaster(db);
daoSession = daoMaster.newSession();
userDao = daoSession.getUserDao();
//插入数据
User user=new User(null,"mm");
userDao.insert(user);
//查询数据
String textColumn = UserDao.Properties.Name.columnName;
String orderBy = textColumn + " COLLATE LOCALIZED ASC";
cursor = db.query(userDao.getTablename(), userDao.getAllColumns(),
null, null, null, null, orderBy);
//适配器初始化
Adapter adapter=new Adapter(getApplicationContext(), cursor);
//绑定数据
((ListView)findViewById(R.id.listView1)).setAdapter(adapter);
((ListView)findViewById(R.id.listView1)).setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// TODO Auto-generated method stub
userDao.deleteByKey(id);//根据ID删除
cursor.requery();
}
});
}
class Adapter extends CursorAdapter{
Cursor c;
Context context;
public Adapter(Context context, Cursor c) {
super(context, c);
this.c=c;
this.context=context;
}
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
View v=LayoutInflater.from(context).inflate(R.layout.list_item,null);
ViewHolder viewHolder=new ViewHolder();
viewHolder.title=(TextView)v.findViewById(R.id.textView1);
viewHolder.text=(TextView)v.findViewById(R.id.textView2);
v.setTag(viewHolder);
return v;
}
@Override
public void bindView(View view, Context context, Cursor cursor) {
ViewHolder viewHolder=(ViewHolder)view.getTag();
viewHolder.title.setText(String.valueOf(cursor.getLong(cursor.getColumnIndex(UserDao.Properties.Id.columnName))));
viewHolder.text.setText(cursor.getString(cursor.getColumnIndex(UserDao.Properties.Name.columnName)));
}
public class ViewHolder{
TextView title;
TextView text;
}
}
}
activity_main.xml
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.greendaodemo.MainActivity"
tools:ignore="MergeRootFrame" >
<ListView
android:id="@+id/listView1"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</ListView>
</FrameLayout>
list_item.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center_vertical|center_horizontal"
android:orientation="vertical" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView"
android:textSize="20sp" />
<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_launcher" />
</LinearLayout>
运行如图: