XUtils简单入门使用

XUtils开源代码下载后我整成一个项目模块,学习中整理出一个具有两个模块的小项目,下载地址:http://pan.baidu.com/s/1nt3jqlV

public class DBActivity extends Activity {

    Button btnInsert;
    Button btnSel;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_db);
        btnInsert=(Button)findViewById(R.id.btnInsert);
        btnSel=(Button)findViewById(R.id.btnSel);

        /**
         * 插入一条数据库记录
         */
        btnInsert.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Employee e = new Employee();
                e.setEmpNo(10);
                e.setName("张三");
                e.setAge(18);
                DbUtils db = DbUtils.create(DBActivity.this);
                try {
                    db.save(e);
                } catch (DbException e1) {
                    e1.printStackTrace();
                }
            }
        });

        /**
         * 查询一条数据库记录
         */
        btnSel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                DbUtils db = DbUtils.create(DBActivity.this);
                try {
                    Employee e = db.findFirst(Selector.from(Employee.class));
                    if(e != null){
                        Toast.makeText(DBActivity.this,e.getName(),Toast.LENGTH_LONG).show();
                    }else {
                        Toast.makeText(DBActivity.this,"还没有记录哦!",Toast.LENGTH_LONG).show();
                    }
                } catch (DbException e1) {
                    e1.printStackTrace();
                }
            }
        });

    }



你可能感兴趣的:(XUtils简单入门使用)