MyApp:
package com.bewei.greendao.app;
import android.app.Application;
import android.database.sqlite.SQLiteDatabase;
import com.bewei.greendao.gen.DaoMaster;
import com.bewei.greendao.gen.DaoSession;
/**
* 1. 类的用途 单例设计模式
* 2. @author forever
* 3. @date 2017/11/6 15:10
*/
public class MyApp extends Application {
private static MyApp mInstance;
private DaoSession daoSession;
@Override
public void onCreate() {
super.onCreate();
mInstance = this;
setDb();
}
public static MyApp getmInstance(){
return mInstance;
}
private void setDb() {
DaoMaster.DevOpenHelper helper = new DaoMaster.DevOpenHelper(this, "bw.db", null);
SQLiteDatabase database = helper.getWritableDatabase();
DaoMaster daoMaster = new DaoMaster(database);
daoSession = daoMaster.newSession();
}
public DaoSession getDaoSessio(){
return daoSession;
}
}
User:
package com.bewei.greendao.bean;
import org.greenrobot.greendao.annotation.Entity;
import org.greenrobot.greendao.annotation.Id;
import org.greenrobot.greendao.annotation.Property;
import org.greenrobot.greendao.annotation.Generated;
/**
* 1. 类的用途
* 2. @author forever
* 3. @date 2017/11/6 14:55
*/
@Entity
public class User {
@Id
private Long id;
@Property(nameInDb = "name")
private String name;
@Generated(hash = 873297011)
public User(Long id, String name) {
this.id = id;
this.name = name;
}
@Generated(hash = 586692638)
public User() {
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
Main:
package com.bewei.greendao;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import com.bewei.greendao.app.MyApp;
import com.bewei.greendao.bean.User;
import com.bewei.greendao.gen.DaoSession;
import com.bewei.greendao.gen.UserDao;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private UserDao userDao;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
DaoSession daoSessio = MyApp.getmInstance().getDaoSessio();
userDao = daoSessio.getUserDao();
}
private void initView() {
Button bt_add = (Button) findViewById(R.id.bt_add);
Button bt_delete = (Button) findViewById(R.id.bt_delete);
Button bt_updata = (Button) findViewById(R.id.bt_updata);
Button bt_find = (Button) findViewById(R.id.bt_find);
bt_add.setOnClickListener(this);
bt_delete.setOnClickListener(this);
bt_updata.setOnClickListener(this);
bt_find.setOnClickListener(this);
}
@Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.bt_add:
List<User> users = new ArrayList<>();
User user1 = new User(Long.valueOf(5), "张三");
User user2 = new User(Long.valueOf(2), "李四");
User user3 = new User(Long.valueOf(3), "王五");
User user4 = new User(Long.valueOf(4), "赵六");
users.add(user1);
users.add(user2);
users.add(user3);
users.add(user4);
for (int i=0;i<users.size();i++){
userDao.insert(users.get(i));
}
Toast.makeText(this, "添加数据成功", Toast.LENGTH_SHORT).show();
break;
case R.id.bt_delete:
User useDelete = userDao.load(Long.valueOf(1));
userDao.delete(useDelete);//删除对象
Toast.makeText(this, "删除数据成功", Toast.LENGTH_SHORT).show();
break;
case R.id.bt_updata:
User useUpdata = userDao.load(Long.valueOf(1));
useUpdata.setName("马七");
userDao.update(useUpdata);//更新对象
break;
case R.id.bt_find:
User useFind = userDao.load(Long.valueOf(1));//查询对象
String name = useFind.getName();
Toast.makeText(this, "查询数据成功:name" + name, Toast.LENGTH_SHORT).show();
List<User> userList = userDao.loadAll();//查询集合
for (User userinfo : userList ) {
String userinfoName = userinfo.getName();
Toast.makeText(this, "查询数据成功:userinfoName:" + userinfoName, Toast.LENGTH_SHORT).show();
}
break;
}
}
}
Other:
package com.bewei.greendao;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
/**
* 1. 类的用途
* 2. @author forever
* 3. @date 2017/11/6 15:56
*/
public class OtherActivity extends AppCompatActivity {
@Bind(R.id.bt_add)
Button btAdd;
@Bind(R.id.bt_delete)
Button btDelete;
@Bind(R.id.bt_updata)
Button btUpdata;
@Bind(R.id.bt_find)
Button btFind;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//绑定控件
ButterKnife.bind(this);
btAdd.setText("add");
}
//设置监听
@OnClick({R.id.bt_add,R.id.bt_delete,R.id.bt_updata,R.id.bt_find})
public void onClick(View view){
switch (view.getId()){
case R.id.bt_add:
Toast.makeText(this, "添加", Toast.LENGTH_SHORT).show();
break;
case R.id.bt_delete:
Toast.makeText(this, "删除", Toast.LENGTH_SHORT).show();
break;
}
}
}
anim:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:orientation="vertical"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<Button
android:id="@+id/bt_add"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="添加数据"
/>
<Button
android:id="@+id/bt_delete"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="删除数据"
/>
<Button
android:id="@+id/bt_updata"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="更新数据"
/>
<Button
android:id="@+id/bt_find"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="查询数据"
/>
</LinearLayout>