安卓中数据库运用---做个便签app

前言:这个app其实很早之前就做完了,但当时没有积累,大二刚开了Java课对安卓有了一些新认识于是前几天就把这个app重做了一遍,总结下来(文章最后有GitHub地址,但建议还是先看一下博客的思路)

效果如图:

总体就是运用数据库里的修改,保存,还有recyclerview的应用

点击"+"添加新的便签(menu的用法)

public boolean onOptionsItemSelected(MenuItem item){

        switch (item.getItemId())
        {
            case R.id.add:
                //跳转
                Intent intent = new Intent(MainActivity.this,Note.class);
                startActivity(intent);
                break;
                default:
        }
        return true;
    }

点击完成或者直接返回都可以保存(在Note实现)

 accomplish.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //如果什么都不写就直接返回

                if(edit.getText().toString().equals("")){
                    finish();
                    return;
                }
                //新建表
                NoteData note = new NoteData();
                //设置内容
                note.setDatabody(edit.getText().toString());
                //这里用Calendar方法来获取系统日期
                Calendar calendar = Calendar.getInstance();
                int year = calendar.get(Calendar.YEAR);
                int month = calendar.get(Calendar.MONTH)+1;
                int day = calendar.get(Calendar.DAY_OF_MONTH);
                int hour = calendar.get(Calendar.HOUR_OF_DAY);
                int minute = calendar.get(Calendar.MINUTE);
                int second  = calendar.get(Calendar.SECOND);
                //设置一下日期显示的格式
                SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.CHINA);
                //按照格式来显示日期
                note.setDatatime(format.format(calendar.getTime()));
                //记得保存
                note.save();
                //添加到notedatalist中
                noteDataList.add(note);
                Toast.makeText(com.example.dell.database.Note.this,"已保存",Toast.LENGTH_SHORT).show();
                finish();
                //recyclerview中更新
                dataAdapter.notifyDataSetChanged();
            }
        });

再次点击进入,可以修改之前的便签(在edit中实现,原理还是Note中的写法),但在修改之前先获取noteDataList中的信息

//从list中获取信息
        position = getIntent().getIntExtra("position", 0);
        note = noteDataList.get(position);
        Log.e("", "onCreate: "+position+note.getDatabody() );
        edit.setText(note.getDatabody());
        note.save();

向左滑动可以删除便签这里因为水平有限用了开源(不得不说开源真得是方便)

	api 'com.github.mcxtzhang:SwipeDelMenuLayout:V1.3.0'


总体思路就是这样,然后源代码都在GitHub上
(好像大家都喜欢直接看源码,上回有个看我博客的朋友就问我有没有源码,然后我说没有GitHub的习惯之后就很尴尬,于是这回就上传一波)https://github.com/jh360twb/Database

猜你喜欢

转载自blog.csdn.net/qq873044564/article/details/82817549