数据库的一些操作

第一个:

public class Main2Activity extends AppCompatActivity implements View.OnClickListener {

    private Button add;
    private ListView listview;
    private ProgressDialog progressDialog;
    private List<Bean> list;
    private MyAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);

        initPro();
        initView();
        initChaxun();
    }

    private void initChaxun() {

        list.clear();
        BeanDao dao = Daosingleton.getDaosingleton().getDao(Main2Activity.this);
        List<Bean> listt = dao.queryBuilder().list();
        list.addAll(listt);

    }

    private void initPro() {

        progressDialog = new ProgressDialog(Main2Activity.this);
        progressDialog.setMax(100);
        progressDialog.setCancelable(false);
        progressDialog.setMessage("正在加载...");
        progressDialog.show();

        Timer timer = new Timer();
        timer.schedule(new MyTask(),2000);
    }
    class MyTask extends TimerTask{

        @Override
        public void run() {
            progressDialog.dismiss();
        }
    }

    private void initView() {
        add = (Button) findViewById(R.id.add);
        listview = (ListView) findViewById(R.id.listview);

        list = new ArrayList<Bean>();
        adapter = new MyAdapter(Main2Activity.this, list);
        listview.setAdapter(adapter);

        listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, final int position, long id) {

                AlertDialog.Builder builder = new AlertDialog.Builder(Main2Activity.this);

                builder.setPositiveButton("修改", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {

                    }
                });
                builder.setNegativeButton("删除", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {

                        BeanDao getdao = Daosingleton.getDaosingleton().getDao(Main2Activity.this);
                        getdao.delete(list.get(position));
                        initChaxun();
                        adapter.notifyDataSetChanged();
                    }
                });

                builder.create();
                builder.show();

            }
        });

        add.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.add:

                Intent intent = new Intent(Main2Activity.this,Main3Activity.class);
                startActivityForResult(intent,1000);
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        switch (requestCode){
            case 1000:
                Toast.makeText(this, "666666666", Toast.LENGTH_SHORT).show();
                String name = data.getStringExtra("name");
                String chu = data.getStringExtra("chu");
                Log.i("TAG",name+chu+"");
                Bean bean=new Bean();
                bean.setName(name);
                bean.setShang(chu);

                charu(bean);
                initChaxun();
                adapter.notifyDataSetChanged();
                break;
            case 2000:
                Toast.makeText(this, "你已经取消了添加数据", Toast.LENGTH_SHORT).show();
                break;
        }
    }
    private void charu(Bean bean) {
        BeanDao  getdao = Daosingleton.getDaosingleton().getDao(Main2Activity.this);
        getdao.insert(bean);

    }

    private void xiugai(Bean bean){
        BeanDao dao = Daosingleton.getDaosingleton().getDao(Main2Activity.this);

    }
}


再写一个Bean类:

再写一个工具类:

public class Daosingleton {

    private static Daosingleton daosingleton;

    public Daosingleton() {
    }

    public BeanDao getDao(Context context){
        DaoMaster.DevOpenHelper devOpenHelper = new DaoMaster.DevOpenHelper(context, "casrhs.db", null);

        DaoMaster daoMaster=new DaoMaster(devOpenHelper.getReadableDb());

        DaoSession daoSession = daoMaster.newSession();

        BeanDao beanDao = daoSession.getBeanDao();

        return beanDao;
    }

    public static Daosingleton getDaosingleton(){

        if (daosingleton == null) {
            synchronized (Daosingleton.class){

                if (daosingleton == null) {
                    daosingleton=new Daosingleton();

                }

            }

        }
        return daosingleton;
    }
}
第二个Activity

public class Main3Activity extends AppCompatActivity implements View.OnClickListener {

    private EditText name;
    private EditText chu;
    private Button yes;
    private Button no;
    private LinearLayout activity_four;
    private String nameString;
    private String chuString;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main3);
        initView();
    }

    private void initView() {
        name = (EditText) findViewById(R.id.name);
        chu = (EditText) findViewById(R.id.chu);
        yes = (Button) findViewById(R.id.yes);
        no = (Button) findViewById(R.id.no);
        activity_four = (LinearLayout) findViewById(R.id.activity_four);

        yes.setOnClickListener(this);
        no.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.yes:
                submit();
                finish();
                break;
            case R.id.no:
                Intent intent = new Intent();
                setResult(2000, intent);
                finish();

                break;
        }
    }

    private void submit() {
        // validate
        // validate
        nameString = name.getText().toString().trim();


        chuString = chu.getText().toString().trim();


        if (!nameString.equals("") && !chuString.equals("")) {

            Intent intent = new Intent();

            intent.putExtra("name", nameString);
            intent.putExtra("chu", chuString);

            setResult(1000, intent);


        } else {

            Toast.makeText(this, "不能为空", Toast.LENGTH_SHORT).show();

        }
    }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值