android安卓数据库的增删改查和发送通知

登录后安卓中的数据库的增删改查怎么实现还有发送通知... 登录后安卓中的数据库的增删改查怎么实现还有发送通知 展开
 我来答
会从地上爬上来
2017-11-12
知道答主
回答量:1
采纳率:0%
帮助的人:1.6万
展开全部
public class MyDbOpenHeler extends SQLiteOpenHelper {
    private static final String name="zhouke.db";
    private static final int version=1;
    public MyDbOpenHeler(Context context) {
        super(context, name, null, version);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL("create table if not exists word(_id integer primary key autoincrement,name varchar(20),age int)");
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

    }
}


------------------------------------------------


public class Main2Activity extends AppCompatActivity {
    ListView lv;
    MyDbOpenHeler heler;
    SQLiteDatabase db;
    SimpleCursorAdapter adapter;

    EditText et1;

    //通知
    NotificationManager manage;
    //---
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);
        lv= (ListView) findViewById(R.id.lv);
        //实例化数据库帮助类
        heler=new MyDbOpenHeler(this);
        //获取一个可读写的数据库操作对象
        db=heler.getWritableDatabase();
        Cursor word = db.query("word", null, null, null, null, null, "_id desc");
        adapter=new SimpleCursorAdapter(this,R.layout.list_ltem,word,
                new String[]{"_id","name","age"},new int[]{R.id.tv1,R.id.tv2,R.id.tv3},
                CursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER);
        lv.setAdapter(adapter);
//        if(word.isClosed()){
//            word.close();
//        }
        registerForContextMenu(lv);
        //通知的实例化
        manage= (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        //---
        et1= (EditText) findViewById(R.id.et1);


    }
    //创建选项菜单
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        menu.add(1,1,0,"增");
        return super.onCreateOptionsMenu(menu);
    }
    //选项菜单监听事件
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        View inflate = View.inflate(this, R.layout.dialog, null);
        final EditText et1 = (EditText) inflate.findViewById(R.id.et1);
        final EditText et2 = (EditText) inflate.findViewById(R.id.et2);
        new AlertDialog.Builder(this).setTitle("增加新学生信息").setView(inflate)
                .setPositiveButton("确认", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        ContentValues cv=new ContentValues();
                        cv.put("name",et1.getText().toString());
                        cv.put("age", et2.getText().toString());

                        long word = db.insert("word", null, cv);
                        if (word>0){
                            Toast.makeText(Main2Activity.this,"增加数据成功",Toast.LENGTH_SHORT).show();
                            Cursor word2 = db.query("word", null, null, null, null, null, "_id desc");
                            adapter.changeCursor(word2);

                            //通知---开始
                            NotificationCompat.Builder builder=new NotificationCompat.Builder(Main2Activity.this);
                            builder.setSmallIcon(R.drawable.aa)
                                    .setContentText("年龄:"+et2.getText().toString())
                                    .setContentTitle(et1.getText().toString())
                                    .setAutoCancel(true);
                            manage.notify(1,builder.build());
                            //通知 --结束


                        }
                    }
                }).setNegativeButton("取消", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {

            }
        }).show();
        return super.onOptionsItemSelected(item);
    }
    //创建上下文菜单
    @Override
    public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
        super.onCreateContextMenu(menu, v, menuInfo);
        menu.add(1,2,0,"修改");
        menu.add(1,3,0,"删除");
    }
    //上下文菜单监听事件
    @Override
    public boolean onContextItemSelected(MenuItem item) {
        //获取当前点击项的索引
        AdapterView.AdapterContextMenuInfo info= (AdapterView.AdapterContextMenuInfo) item.getMenuInfo();
        int position = info.position;
        //根据索引获取这一行数据,把它转化为Cursor
        Cursor  cursor= (Cursor) adapter.getItem(position);
        //从结果集中取出id
        final int _id = cursor.getInt(0);
        switch (item.getItemId()){
            case 2:
                View inflate = View.inflate(this, R.layout.dialog, null);
                final EditText et1 = (EditText) inflate.findViewById(R.id.et1);
                final EditText et2 = (EditText) inflate.findViewById(R.id.et2);
                //获取单词名称
                String name=cursor.getString(1);
                String age=cursor.getString(2);
                et1.setText(name);
                et2.setText(age);
                new AlertDialog.Builder(this).setTitle("修改学生信息").setView(inflate)
                        .setPositiveButton("确认", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                ContentValues cv=new ContentValues();
                                cv.put("name",et1.getText().toString());
                                cv.put("age", et2.getText().toString());

                                int word=db.update("word",cv,"_id=?",new String[]{String.valueOf(_id)});


                                if (word>0){
                                    Toast.makeText(Main2Activity.this,"修改数据成功",Toast.LENGTH_SHORT).show();
                                    Cursor word2 = db.query("word", null, null, null, null, null, "_id desc");
                                    adapter.changeCursor(word2);
                                }
                            }
                        }).setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {

                    }
                }).show();

                break;
            case 3:
                int word = db.delete("word", "_id=?", new String[]{String.valueOf(_id)});
                if (word>0){
                    Toast.makeText(Main2Activity.this,"删除数据成功",Toast.LENGTH_SHORT).show();
                    Cursor word2 = db.query("word", null, null, null, null, null, "_id desc");
                    adapter.changeCursor(word2);
                }
                break;
        }
        return super.onContextItemSelected(item);
    }

    public void click1(View view) {
        //模糊查询
        String aa=et1.getText().toString();
        Cursor word2 = db.query("word", null, "name like ?", new String[]{"%"+aa+"%"}, null, null, "_id desc");
        adapter.changeCursor(word2);
        //正常查询
 //       Cursor word2 = db.query("word", null, "name=?", new String[]{aa}, null, null, "_id desc");
    }
}
百度网友fc3cb19
2017-11-12
知道答主
回答量:1
采纳率:0%
帮助的人:939
展开全部
public class Main2Activity extends AppCompatActivity {
    ListView lv;
    MyDbOpenHeler heler;
    SQLiteDatabase db;
    SimpleCursorAdapter adapter;

    EditText et1;

    //通知
    NotificationManager manage;
    //---
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);
        lv= (ListView) findViewById(R.id.lv);
        //实例化数据库帮助类
        heler=new MyDbOpenHeler(this);
        //获取一个可读写的数据库操作对象
        db=heler.getWritableDatabase();
        Cursor word = db.query("word", null, null, null, null, null, "_id desc");
        adapter=new SimpleCursorAdapter(this,R.layout.list_ltem,word,
                new String[]{"_id","name","age"},new int[]{R.id.tv1,R.id.tv2,R.id.tv3},
                CursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER);
        lv.setAdapter(adapter);
//        if(word.isClosed()){
//            word.close();
//        }
        registerForContextMenu(lv);
        //通知的实例化
        manage= (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        //---
        et1= (EditText) findViewById(R.id.et1);


    }
    //创建选项菜单
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        menu.add(1,1,0,"增");
        return super.onCreateOptionsMenu(menu);
    }
    //选项菜单监听事件
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        View inflate = View.inflate(this, R.layout.dialog, null);
        final EditText et1 = (EditText) inflate.findViewById(R.id.et1);
        final EditText et2 = (EditText) inflate.findViewById(R.id.et2);
        new AlertDialog.Builder(this).setTitle("增加新学生信息").setView(inflate)
                .setPositiveButton("确认", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        ContentValues cv=new ContentValues();
                        cv.put("name",et1.getText().toString());
                        cv.put("age", et2.getText().toString());
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
匿名用户
2017-11-12
展开全部
表名: person 字段: id, name, age 1 张三 20 2 李四 22 3 王五 23 查询: select id,name,age from person; 删除: delete from person where id=1 (删除ID=1的那条数据,) delete from person (删除person表中的所有数据); 修改: update person set name="刘德华" where id=2; (就会李四的名字改成刘德华); 增加: insert into person values(4,'赵六',24);
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
夏慕雪c
2017-11-12
知道答主
回答量:2
采纳率:0%
帮助的人:1843
展开全部
public class MainActivity extends AppCompatActivity {
    EditText et1,et2;
    Button btn;
    SharedPreferences shared;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        et1= (EditText) findViewById(R.id.et1);
        et2= (EditText) findViewById(R.id.et2);
        btn= (Button) findViewById(R.id.btn);
        shared=getSharedPreferences("click",MODE_PRIVATE|MODE_APPEND);
        String name=shared.getString("name","");
        String pass=shared.getString("pass","");
        if (!name.isEmpty()){
            et1.setText(name);
            et2.setText(pass);
        }
    }
    public void click(View view) {
        String name=et1.getText().toString();
        String pass=et2.getText().toString();
        SharedPreferences.Editor editor=shared.edit();
        editor.putString("name",name);
        editor.putString("pass",pass);
        editor.commit();
        Intent intent=new Intent(this,Main2Activity.class);
        startActivity(intent);
    }
}
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
收起 更多回答(2)
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式