因项目有需求,需建一个数据库来存储数据,数据库选型就选了当前比较流行,性能也比较好的greenDao,目前使用的版本是3.2.0,简单易用,下面具体说下使用的步骤:
1.在build.gradle文件导入配置文件,导入包
//greenDao----start// apply plugin: 'org.greenrobot.greendao' buildscript { repositories { mavenCentral() } dependencies { classpath 'org.greenrobot:greendao-gradle-plugin:3.2.0' } } greendao { schemaVersion 1 //<--数据库的版本,用于升级时候进行更改 daoPackage 'com.example.pc.teste6.dao' //存放自动生成代码的包 targetGenDir 'src/main/java' //... } //greenDao----end//
//greeDao所需要的支持 compile 'org.greenrobot:greendao:3.2.0' compile 'org.greenrobot:greendao-generator:3.2.0'
2.greenDao非常智能化,接着只需写你的数据库实体类,就能生成对应的表了,例如
public class UserObject { //这个注解就表明下面那个id是个主键,必须用Long,否则会报错,autoincrement=true自增 @Id(autoincrement = true) Long id; @NotNull String name; //不为空 @NotNull Integer age; }接着按Ctrl+F9,就会生成get和set方法,并且在你的dao包下生成数据库文件和dao类
@Entity public class UserObject { //这个注解就表明下面那个id是个主键,必须用Long,否则会报错,autoincrement=true自增 @Id(autoincrement = true) Long id; @NotNull String name; //不为空 @NotNull Integer age; @Generated(hash = 824938356) public UserObject(Long id, @NotNull String name, @NotNull Integer age) { this.id = id; this.name = name; this.age = age; } @Generated(hash = 1300257024) public UserObject() { } public Long getId() { return this.id; } public void setId(Long id) { this.id = id; } public String getName() { return this.name; } public void setName(String name) { this.name = name; } public Integer getAge() { return this.age; } public void setAge(Integer age) { this.age = age; }3.写一个DBDaoUtil类,操作数据库
public class DBDaoUtil { private static DaoSession daoSession; private static DaoSession getDaoSession(Context context) { if (daoSession==null) { DaoMaster.OpenHelper openHelper = new DaoMaster.OpenHelper(context, "student"){ @Override public void onUpgrade(Database db, int oldVersion, int newVersion) { switch (oldVersion) { case 1: db.beginTransaction(); db.execSQL("..."); db.setTransactionSuccessful(); db.endTransaction(); break; } } }; SQLiteDatabase db = openHelper.getWritableDatabase(); DaoMaster daoMaster = new DaoMaster(db); daoSession = daoMaster.newSession(); } return daoSession; } public static UserObjectDao getUserDao(Context context) { return getDaoSession(context).getUserObjectDao(); } }
activity_main.xml文件
xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_main" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="com.example.pc.teste6.MainActivity"> <LinearLayout android:id="@+id/dataLL" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <Button android:id="@+id/add" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/network" android:text="增"/> <Button android:id="@+id/delete" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/add" android:text="删"/> <Button android:id="@+id/update" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/delete" android:text="改"/> <Button android:id="@+id/select" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/update" android:text="查"/> LinearLayout> <ListView android:id="@+id/dataLV" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_below="@+id/dataLL"> ListView> RelativeLayout>Mainactivity
import android.content.Context; import android.content.Intent; import android.content.IntentFilter; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.Button; import android.widget.ListView; import android.widget.TextView; import android.widget.Toast; import com.example.pc.teste6.bean.UserObject; import com.example.pc.teste6.dao.UserObjectDao; import com.example.pc.teste6.receiver.BatteryReceiver; import com.example.pc.teste6.util.DBDaoUtil; import com.example.pc.teste6.util.NetUtil; import java.util.ArrayList; import java.util.List; public class MainActivity extends AppCompatActivity implements View.OnClickListener{ private Context context; private Button addBtn,deleteBtn,updateBtn,selectBtn;//增删改查 private ListView dataLV; private DataAdapter adapter; private UserObjectDao userDao; private int i=1; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); context=this; initView(); initData(); } /** * 初始化控件 */ private void initView(){ addBtn= (Button) findViewById(R.id.add); deleteBtn= (Button) findViewById(R.id.delete); updateBtn= (Button) findViewById(R.id.update); selectBtn= (Button) findViewById(R.id.select); dataLV= (ListView) findViewById(R.id.dataLV); addBtn.setOnClickListener(this); deleteBtn.setOnClickListener(this); updateBtn.setOnClickListener(this); selectBtn.setOnClickListener(this); } /** * 初始化数据 */ private void initData(){ userDao = DBDaoUtil.getUserDao(context);//初始化dao adapter=new DataAdapter(context); dataLV.setAdapter(adapter); } @Override public void onClick(View view) { switch (view.getId()){ case R.id.add: UserObject ub1=new UserObject(); ub1.setName("小小英"+i); ub1.setAge(100); userDao.insert(ub1); Listlist1=userDao.loadAll(); adapter.addData(list1); i++; break; case R.id.delete: userDao.deleteByKey(Long.parseLong(i+"")); List list2=userDao.loadAll(); adapter.addData(list2); break; case R.id.update: UserObject ub3=new UserObject(); ub3.setId(Long.parseLong(i+"")); userDao.update(ub3); List list3=userDao.loadAll(); adapter.addData(list3); break; case R.id.select://查询数据 List list4=userDao.loadAll(); adapter.addData(list4); Toast.makeText(context,""+list4.size(),Toast.LENGTH_SHORT).show(); break; default: break; } } class DataAdapter extends BaseAdapter{ private Context context; private List mList; public DataAdapter(Context context) { this.context = context; } public void addData(List mList){ this.mList=mList; notifyDataSetChanged(); } @Override public int getCount() { return (mList!=null)?mList.size():1; } @Override public Object getItem(int i) { return mList.get(i); } @Override public long getItemId(int i) { return i; } @Override public View getView(int i, View view, ViewGroup viewGroup) { view = LayoutInflater.from(context).inflate(R.layout.lv_item,null); if(mList!=null&&mList.size()!=0){ TextView idTV= (TextView) view.findViewById(R.id.idTV); idTV.setText(mList.get(i).getId()+""); TextView nameTV= (TextView) view.findViewById(R.id.nameTv); nameTV.setText(mList.get(i).getName()); TextView ageTV= (TextView) view.findViewById(R.id.ageTv); ageTV.setText(mList.get(i).getAge()+""); } return view; } }
}
我的心愿是世界和平。。。