zoukankan      html  css  js  c++  java
  • 实验八:SQLite数据库操作

    实验报告

    课程名称

    基于Android平台移动互联网开发

    实验日期

    2016年5月6日

    实验项目名称

    SQLite数据库操作

    实验地点

    S3010

    实验类型

    □验证型    √设计型    □综合型

    学  时

    4

    一、实验目的及要求(本实验所涉及并要求掌握的知识点)

    1. 设计一个个人通讯录,掌握Android平台下的数据库开发,该个人通讯录主要包括联系人列表和联系人详细信息等界面。
    2. 程序主界面是通讯录的目录显示手机上联系人的名称。点击联系人的姓名可以显示联系人的详细信息。单击图标按钮可以添加联系人和删除联系人。

    二、实验环境(本实验所使用的硬件设备和相关软件)

    (1)PC机

    (2)操作系统:Windows XP

    (3)软件: Eclipse, JDK1.6,Android SDK,ADT

    三、实验内容及步骤

    (1)  确定数据库的数据结构。

    (2)  新建工程,修改布局文件,定义字符串资源。

    (3)  开发布局文件activity_main.xml用于显示联系人列表。

    (4)  layout目录下新建一个detail.xml,用于显示联系人详细信息。

    (5)  开发数据库辅助类MyOpenHelper类

    (6)  DetailActivity端开发实现数据库增加、删除、修改记录等操作

    (7)  新建Activity名为DetailActivity.java,实现联系人详细信息显示功能。

    四、实验结果(本实验源程序清单及运行结果或实验结论、实验设计图)

    代码:

    DeleteActivity

    public class DeleteActivity extends Activity {
        Button btnback;
        Intent it = new Intent();
        ListView listview;
        UserSQL usersql;
        SQLiteDatabase userdatabases;
        ArrayList<HashMap<String, Object>> userlist;
        String[] name = new String[100];
        Cursor cursor;
        SimpleAdapter sa;
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.delete);
            btnback = (Button) findViewById(R.id.back);
            listview = (ListView) findViewById(R.id.listView2);
            usersql = new UserSQL(DeleteActivity.this, "user.db", null, 1);
            userdatabases = usersql.getReadableDatabase();
            userlist = new ArrayList<HashMap<String, Object>>();
            cursor = userdatabases.rawQuery("select * from userTable", null);
            cursor.moveToFirst();
            
            if (cursor.getCount() > 0) {
                for (int i = 0; i < cursor.getCount(); i++) {
                    HashMap<String, Object> hashmap = new HashMap<String, Object>();
                    hashmap.put("name",
                            cursor.getString(cursor.getColumnIndex("name")));
                    name[i] = cursor.getString(cursor.getColumnIndex("name"));
                    hashmap.put("phone",
                            "[" + cursor.getString(cursor.getColumnIndex("phone"))
                                    + "]");
                    userlist.add(hashmap);
                    if (i < cursor.getCount()) {
                        cursor.moveToNext();
                    }
                }
                sa = new SimpleAdapter(DeleteActivity.this, userlist,
                        R.layout.list, new String[] { "name", "phone" }, new int[] {
                                R.id.namelist, R.id.phonelist });
                listview.setAdapter(sa);
            }
            listview.setOnItemClickListener(new OnItemClickListener() {
    
                @Override
                public void onItemClick(AdapterView<?> parent, View view,
                        int position, long id) {
                    // TODO Auto-generated method stub
                    
                    userdatabases.delete("userTable", "name=?",new String[] { name[position] });
                    
                    list();
                }
            });
            btnback.setOnClickListener(new OnClickListener() {
                
                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    it.setClass(DeleteActivity.this, MainActivity.class);
                    startActivity(it);
                    finish();
                }
            });
    
        }
        public void list(){
            userlist = new ArrayList<HashMap<String, Object>>();
            cursor = userdatabases.rawQuery("select * from userTable", null);
            cursor.moveToFirst();
            if (cursor.getCount() > 0) {
                for (int i = 0; i < cursor.getCount(); i++) {
                    HashMap<String, Object> hashmap = new HashMap<String, Object>();
                    hashmap.put("name",
                            cursor.getString(cursor.getColumnIndex("name")));
                    name[i] = cursor.getString(cursor.getColumnIndex("name"));
                    hashmap.put("phone",
                            "[" + cursor.getString(cursor.getColumnIndex("phone"))
                                    + "]");
                    userlist.add(hashmap);
                    if (i < cursor.getCount()) {
                        cursor.moveToNext();
                    }
                }
                sa = new SimpleAdapter(DeleteActivity.this, userlist,
                        R.layout.list, new String[] { "name", "phone" }, new int[] {
                                R.id.namelist, R.id.phonelist });
                listview.setAdapter(sa);
            }
        }
    }

    DetailActivity

    package com.example.tongxinlu;
    public class DetailActivity extends Activity {
        EditText edname, edphone, edmobile, edemail, edpost, edaddr, edcomp;
        String name, phone, mobile, email, post, addr, comp;
        Button btnadd;
        UserSQL usersql;
        SQLiteDatabase userdatabase;
        Bundle bd;
        String selectname;
    
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.detail);
            btnadd = (Button) findViewById(R.id.deadd);
            edname = (EditText) findViewById(R.id.edname);
            edphone = (EditText) findViewById(R.id.edphone);
            edmobile = (EditText) findViewById(R.id.edmoblie);
            edemail = (EditText) findViewById(R.id.edemail);
            edpost = (EditText) findViewById(R.id.edpost);
            edaddr = (EditText) findViewById(R.id.edaddr);
            edcomp = (EditText) findViewById(R.id.edcomp);
            usersql = new UserSQL(DetailActivity.this, "user.db", null, 1);
            userdatabase = usersql.getReadableDatabase();
            bd = getIntent().getExtras();
            if (!bd.getString("name").equals("")) {
                selectname = bd.getString("name");
                Cursor cursor = userdatabase.rawQuery(
                        "select * from userTable where name=?",
                        new String[] { selectname });
                cursor.moveToFirst();
                edname.setText(cursor.getString(cursor.getColumnIndex("name")));
                edphone.setText(cursor.getString(cursor.getColumnIndex("phone")));
                edmobile.setText(cursor.getString(cursor.getColumnIndex("mobile")));
                edemail.setText(cursor.getString(cursor.getColumnIndex("email")));
                edpost.setText(cursor.getString(cursor.getColumnIndex("post")));
                edaddr.setText(cursor.getString(cursor.getColumnIndex("addr")));
                edcomp.setText(cursor.getString(cursor.getColumnIndex("comp")));
                btnadd.setText("更新");
            }
            btnadd.setOnClickListener(new OnClickListener() {
    
                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    name = edname.getText().toString();
                    phone = edphone.getText().toString();
                    mobile = edmobile.getText().toString();
                    email = edemail.getText().toString();
                    post = edpost.getText().toString();
                    addr = edaddr.getText().toString();
                    comp = edcomp.getText().toString();
    
                    if (!name.equals("") && !phone.equals("") && !mobile.equals("")
                            && !email.equals("") && !post.equals("")
                            && !addr.equals("") && !comp.equals("")) {
    
                        Cursor cursor = userdatabase.rawQuery(
                                "select * from userTable where name=?",
                                new String[] { name });
                        cursor.moveToFirst();
                        ContentValues cv = new ContentValues();
                        cv.put("name", name);
                        cv.put("phone", phone);
                        cv.put("mobile", mobile);
                        cv.put("email", email);
                        cv.put("post", post);
                        cv.put("addr", addr);
                        cv.put("comp", comp);
                        if (cursor.getCount() <= 0) {
                            userdatabase.insert("userTable", null, cv);
                            cv.clear();
                            Toast.makeText(DetailActivity.this, "保存" + name + "成功",
                                    Toast.LENGTH_LONG).show();
                            userdatabase.delete("userTable", "name=?",
                                    new String[] { selectname });
    
                            Intent it = new Intent();
                            it.setClass(DetailActivity.this, MainActivity.class);
                            startActivity(it);
                            finish();
                        } else if (cursor.getCount() == 1
                                && cursor.getString(cursor.getColumnIndex("name"))
                                        .equals(selectname)) {
                            userdatabase.update("userTable", cv, "name=?",
                                    new String[] { selectname });
                            cv.clear();
                            Toast.makeText(DetailActivity.this, "更新" + name + "成功",
                                    Toast.LENGTH_LONG).show();
                            Intent it = new Intent();
                            it.setClass(DetailActivity.this, MainActivity.class);
                            startActivity(it);
                            finish();
                        } else {
    
                            Toast.makeText(DetailActivity.this, name + "已注册",
                                    Toast.LENGTH_LONG).show();
                        }
                    } else {
                        Toast.makeText(DetailActivity.this, "信息不完整",
                                Toast.LENGTH_LONG).show();
                    }
                }
    
            });
        }
    }

    main

    package com.example.tongxinlu;public class MainActivity extends Activity {
        Button btnadd, btndel;
        Intent it = new Intent();
        ListView listview;
        UserSQL usersql;
        SQLiteDatabase userdatabases;
        ArrayList<HashMap<String, Object>> userlist;
        String[] name=new String[100];
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            btnadd = (Button) findViewById(R.id.add);
            btndel = (Button) findViewById(R.id.delete);
            listview = (ListView) findViewById(R.id.listView1);
            usersql = new UserSQL(MainActivity.this, "user.db", null, 1);
            userdatabases = usersql.getReadableDatabase();
            userlist = new ArrayList<HashMap<String, Object>>();
            Cursor cursor = userdatabases.rawQuery("select * from userTable", null);
            cursor.moveToFirst();
            if (cursor.getCount() > 0) {
                for (int i = 0; i < cursor.getCount(); i++) {
                    HashMap<String, Object> hashmap = new HashMap<String, Object>();
                    hashmap.put("name",
                            cursor.getString(cursor.getColumnIndex("name")));
                    name[i]=cursor.getString(cursor.getColumnIndex("name"));
                    hashmap.put("phone",
                            "["+cursor.getString(cursor.getColumnIndex("phone"))+"]");
                    userlist.add(hashmap);
                    if (i < cursor.getCount()) {
                        cursor.moveToNext();
                    }
                }
                SimpleAdapter sa = new SimpleAdapter(MainActivity.this, userlist,
                        R.layout.list, new String[] { "name", "phone" }, new int[] {
                                R.id.namelist, R.id.phonelist });
                listview.setAdapter(sa);
            }
            listview.setOnItemClickListener(new OnItemClickListener() {
    
                @Override
                public void onItemClick(AdapterView<?> parent, View view,
                        int position, long id) {
                    // TODO Auto-generated method stub
                    Intent intent=new Intent(MainActivity.this,DetailActivity.class);
                    Bundle bd=new Bundle();
                    bd.putString("name", name[position]);
                
                    intent.putExtras(bd);
                    startActivity(intent);
                }
            });
            btnadd.setOnClickListener(new OnClickListener() {
    
                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    it.setClass(MainActivity.this, DetailActivity.class);
                    Bundle bd=new Bundle();
                    bd.putString("name", "");   
                    it.putExtras(bd);
                    startActivity(it);
    
                }
            });
            btndel.setOnClickListener(new OnClickListener() {
                
                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    it.setClass(MainActivity.this, DeleteActivity.class);
                    startActivity(it);
                }
            });
        }
    
        @Override
        public boolean onCreateOptionsMenu(Menu menu) {
            // Inflate the menu; this adds items to the action bar if it is present.
            getMenuInflater().inflate(R.menu.main, menu);
            return true;
        }
    
    }

    运行结果:(截图)

     

     

    五、实验总结(对本实验结果进行分析,实验心得体会及改进意见)

        经过本次的实验,对数据库进行增、删、查、改的操作有了一定的了解,通过参考资料和老师上课讲解梳理了部分知识难点,完成的挺顺利。日后需要多看,多练编写android程序。

    实验评语

     

    实验成绩

     

    指导教师签名:              年   月   日

  • 相关阅读:
    Redis事务,持久化,缓存同步
    关于虚拟机初次安装出现32位问题
    解决nginx无法访问问题
    如何解决maven 打war,在tomcat下直接访问localhost:8080
    如何解决eclipse里面tomcat 8080端口被占用
    连接mysql出现Access denied for user ‘root’@’localhost’ (using password:YES)问题解决办法
    idea下面修改项目的访问路径
    wordpress主题乱码
    如何在Windows系统本地环境下使用phpstudy快速搭建wordpress平台
    小积累
  • 原文地址:https://www.cnblogs.com/cckuang/p/5487346.html
Copyright © 2011-2022 走看看