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

    实验报告

    课程名称

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

    实验日期

    2016年5月6日

    实验项目名称

    SQLite数据库操作

    实验地点

    S3010

    实验类型

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

    学  时

    2

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

    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,实现联系人详细信息显示功能。

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

    代码:

     MainActivity代码:

     1 public class MainActivity extends Activity {
     2     Button btnadd, btndel;
     3     Intent it = new Intent();
     4     ListView listview;
     5     UserSQL usersql;
     6     SQLiteDatabase userdatabases;
     7     ArrayList<HashMap<String, Object>> userlist;
     8     String[] name=new String[100];
     9 
    10     @Override
    11     protected void onCreate(Bundle savedInstanceState) {
    12         super.onCreate(savedInstanceState);
    13         setContentView(R.layout.activity_main);
    14         btnadd = (Button) findViewById(R.id.add);
    15         btndel = (Button) findViewById(R.id.delete);
    16         listview = (ListView) findViewById(R.id.listView1);
    17         usersql = new UserSQL(MainActivity.this, "user.db", null, 1);
    18         userdatabases = usersql.getReadableDatabase();
    19         userlist = new ArrayList<HashMap<String, Object>>();
    20         Cursor cursor = userdatabases.rawQuery("select * from userTable", null);
    21         cursor.moveToFirst();
    22         if (cursor.getCount() > 0) {
    23             for (int i = 0; i < cursor.getCount(); i++) {
    24                 HashMap<String, Object> hashmap = new HashMap<String, Object>();
    25                 hashmap.put("name",
    26                         cursor.getString(cursor.getColumnIndex("name")));
    27                 name[i]=cursor.getString(cursor.getColumnIndex("name"));
    28                 hashmap.put("phone",
    29                         "["+cursor.getString(cursor.getColumnIndex("phone"))+"]");
    30                 userlist.add(hashmap);
    31                 if (i < cursor.getCount()) {
    32                     cursor.moveToNext();
    33                 }
    34             }
    35             SimpleAdapter sa = new SimpleAdapter(MainActivity.this, userlist,
    36                     R.layout.userlist, new String[] { "name", "phone" }, new int[] {
    37                             R.id.namelist, R.id.phonelist });
    38             listview.setAdapter(sa);
    39         }
    40         listview.setOnItemClickListener(new OnItemClickListener() {
    41 
    42             @Override
    43             public void onItemClick(AdapterView<?> parent, View view,
    44                     int position, long id) {
    45                 // TODO Auto-generated method stub
    46                 Intent intent=new Intent(MainActivity.this,DetailActivity.class);
    47                 Bundle bd=new Bundle();
    48                 bd.putString("name", name[position]);
    49             
    50                 intent.putExtras(bd);
    51                 startActivity(intent);
    52             }
    53         });
    54         btnadd.setOnClickListener(new OnClickListener() {
    55 
    56             @Override
    57             public void onClick(View v) {
    58                 // TODO Auto-generated method stub
    59                 it.setClass(MainActivity.this, DetailActivity.class);
    60                 Bundle bd=new Bundle();
    61                 bd.putString("name", "");
    62                 
    63                 it.putExtras(bd);
    64                 startActivity(it);
    65 
    66             }
    67         });
    68         btndel.setOnClickListener(new OnClickListener() {
    69             
    70             @Override
    71             public void onClick(View v) {
    72                 // TODO Auto-generated method stub
    73                 it.setClass(MainActivity.this, DeleteActivity.class);
    74                 startActivity(it);
    75             }
    76         });
    77     }
    78 
    79     @Override
    80     public boolean onCreateOptionsMenu(Menu menu) {
    81         // Inflate the menu; this adds items to the action bar if it is present.
    82         getMenuInflater().inflate(R.menu.main, menu);
    83         return true;
    84     }
    85 
    86 }

    DetailActivity代码:

      1 public class DetailActivity extends Activity {
      2     EditText edname, edphone, edmobile, edemail, edpost, edaddr, edcomp;
      3     String name, phone, mobile, email, post, addr, comp;
      4     Button btnadd;
      5     UserSQL usersql;
      6     SQLiteDatabase userdatabase;
      7     Bundle bd;
      8     String selectname;
      9 
     10     protected void onCreate(Bundle savedInstanceState) {
     11         super.onCreate(savedInstanceState);
     12         setContentView(R.layout.detail);
     13         btnadd = (Button) findViewById(R.id.deadd);
     14         edname = (EditText) findViewById(R.id.edname);
     15         edphone = (EditText) findViewById(R.id.edphone);
     16         edmobile = (EditText) findViewById(R.id.edmoblie);
     17         edemail = (EditText) findViewById(R.id.edemail);
     18         edpost = (EditText) findViewById(R.id.edpost);
     19         edaddr = (EditText) findViewById(R.id.edaddr);
     20         edcomp = (EditText) findViewById(R.id.edcomp);
     21         usersql = new UserSQL(DetailActivity.this, "user.db", null, 1);
     22         userdatabase = usersql.getReadableDatabase();
     23         bd = getIntent().getExtras();
     24         if (!bd.getString("name").equals("")) {
     25             selectname = bd.getString("name");
     26             Cursor cursor = userdatabase.rawQuery(
     27                     "select * from userTable where name=?",
     28                     new String[] { selectname });
     29             cursor.moveToFirst();
     30             edname.setText(cursor.getString(cursor.getColumnIndex("name")));
     31             edphone.setText(cursor.getString(cursor.getColumnIndex("phone")));
     32             edmobile.setText(cursor.getString(cursor.getColumnIndex("mobile")));
     33             edemail.setText(cursor.getString(cursor.getColumnIndex("email")));
     34             edpost.setText(cursor.getString(cursor.getColumnIndex("post")));
     35             edaddr.setText(cursor.getString(cursor.getColumnIndex("addr")));
     36             edcomp.setText(cursor.getString(cursor.getColumnIndex("comp")));
     37             btnadd.setText("刷新联系人");
     38         }
     39         btnadd.setOnClickListener(new OnClickListener() {
     40 
     41             @Override
     42             public void onClick(View v) {
     43                 // TODO Auto-generated method stub
     44                 name = edname.getText().toString();
     45                 phone = edphone.getText().toString();
     46                 mobile = edmobile.getText().toString();
     47                 email = edemail.getText().toString();
     48                 post = edpost.getText().toString();
     49                 addr = edaddr.getText().toString();
     50                 comp = edcomp.getText().toString();
     51 
     52                 if (!name.equals("") && !phone.equals("") && !mobile.equals("")
     53                         && !email.equals("") && !post.equals("")
     54                         && !addr.equals("") && !comp.equals("")) {
     55 
     56                     Cursor cursor = userdatabase.rawQuery(
     57                             "select * from userTable where name=?",
     58                             new String[] { name });
     59                     cursor.moveToFirst();
     60                     ContentValues cv = new ContentValues();
     61                     cv.put("name", name);
     62                     cv.put("phone", phone);
     63                     cv.put("mobile", mobile);
     64                     cv.put("email", email);
     65                     cv.put("post", post);
     66                     cv.put("addr", addr);
     67                     cv.put("comp", comp);
     68                     if (cursor.getCount() <= 0) {
     69                         userdatabase.insert("userTable", null, cv);
     70                         cv.clear();
     71                         Toast.makeText(DetailActivity.this, "保存" + name + "成功",
     72                                 Toast.LENGTH_LONG).show();
     73                         userdatabase.delete("userTable", "name=?",
     74                                 new String[] { selectname });
     75 
     76                         Intent it = new Intent();
     77                         it.setClass(DetailActivity.this, MainActivity.class);
     78                         startActivity(it);
     79                         finish();
     80                     } else if (cursor.getCount() == 1
     81                             && cursor.getString(cursor.getColumnIndex("name"))
     82                                     .equals(selectname)) {
     83                         userdatabase.update("userTable", cv, "name=?",
     84                                 new String[] { selectname });
     85                         cv.clear();
     86                         Toast.makeText(DetailActivity.this, "刷新联系人" + name + "成功",
     87                                 Toast.LENGTH_LONG).show();
     88                         Intent it = new Intent();
     89                         it.setClass(DetailActivity.this, MainActivity.class);
     90                         startActivity(it);
     91                         finish();
     92                     } else {
     93 
     94                         Toast.makeText(DetailActivity.this, name + "已被注册",
     95                                 Toast.LENGTH_LONG).show();
     96                     }
     97                 } else {
     98                     Toast.makeText(DetailActivity.this, "信息填写不完整",
     99                             Toast.LENGTH_LONG).show();
    100                 }
    101             }
    102 
    103         });
    104     }
    105 }

    运行结果:(截图)

     

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

        经过本次实验,对于数据库数据的新增、删除等有了一定的了解,经过同学讨论以及参考老师所给的材料,再加上结合老师上课讲的重点难点,大概能够完成,但依然存在一些小问题。

    实验评语

     

    实验成绩

     

    指导教师签名:              年   月   日

               
  • 相关阅读:
    浅析全球电信运营商排名
    《时空骇客》中的远距传物理论和虫洞理论
    优秀的商业计划书一定会“动”
    手机搜索的商业模式
    手机网游排行榜
    手机按键对应表
    "Avatar模式"透析
    百度数据暗示无线互联网将以个人为中心
    一种精神致加西亚的信
    手机定位技术将成社交网络催化剂
  • 原文地址:https://www.cnblogs.com/zhushengjie/p/5487852.html
Copyright © 2011-2022 走看看