安卓通讯录开发实验报告

要求:

1.实验报告要完整,即要有实验要求与目的、实验内容与步骤(必要流程图或活动图、代码)、运行结果与分析、实验体会与收获;

2.代码要有必要的注释;

3.标题为四号黑体,其他为宋体小四号,行间距为单倍行间距,纸张大小为A4;

4.考核项目

考核项目

格式与完成性

实验内容

实验结果与分析

实验体会与收获

总成绩

权重

20%

60%

10%

10%

100%

得分

  • 实验目的

1.熟悉数据存储方式,能够归纳五种数据存储方式的特点;

2.掌握文件存储方式的使用,能够实现使用文件存储数据的功能;

3.掌握SharedPreferences的使用,能够实现数据存储的功能;

4.掌握SQLite数据库的使用,能够实现数据的增删改查功能。

  • 实验工具

Android studio。

  • 实验内容

运行效果图如下:

  1. 完成上图的布局;
  2. 利用SQLite数据库的创建与增删改查操作,完成上图添加、删除、修改、查询通讯录内容:姓名、电话。

  • 实验过程(代码)

activity_main.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   
android:layout_width="match_parent"
   
android:layout_height="match_parent"
   
android:background="@drawable/bg"
   
android:orientation="vertical"
   
android:padding="16dp" >
    <
LinearLayout
       
android:layout_width="match_parent"
       
android:layout_height="wrap_content"
       
android:layout_marginTop="130dp">
        <
TextView
           
android:layout_width="wrap_content"
           
android:layout_height="wrap_content"
           
android:text="姓名:"
           
android:textSize="18sp"/>
        <
EditText
           
android:id="@+id/et_name"
           
android:layout_width="match_parent"
           
android:layout_height="wrap_content"
           
android:hint="请输入姓名"
           
android:textSize="16sp"/>
    </
LinearLayout>
    <
LinearLayout
       
android:layout_width="match_parent"
       
android:layout_height="wrap_content"
       
android:layout_marginBottom="10dp">
        <
TextView
           
android:layout_width="wrap_content"
           
android:layout_height="wrap_content"
           
android:text="电话:"
           
android:textSize="18sp"/>
        <
EditText
           
android:id="@+id/et_phone"
           
android:layout_width="match_parent"
           
android:layout_height="wrap_content"
           
android:hint="请输入手机号码"
           
android:textSize="16sp"/>
    </
LinearLayout>
    <
LinearLayout
        android:layout_width="match_parent"
       
android:layout_height="wrap_content">
        <
Button
           
android:id="@+id/btn_add"
           
android:layout_width="0dp"
           
android:layout_height="wrap_content"
           
android:layout_marginRight="2dp"
           
android:layout_weight="1"
           
android:background="#B9B9FF"
           
android:text="添加"
           
android:textSize="18sp"/>
        <
Button
           
android:id="@+id/btn_query"
           
android:layout_width="0dp"
           
android:layout_height="wrap_content"
           
android:layout_marginRight="2dp"
           
android:layout_weight="1"
           
android:background="#DCB5FF"
           
android:text="查询"
           
android:textSize="18sp"/>
        <
Button
           
android:id="@+id/btn_update"
           
android:layout_width="0dp"
           
android:layout_height="wrap_content"
           
android:layout_marginRight="2dp"
           
android:layout_weight="1"
           
android:background="#E6CAFF"
            android:text="修改"
           
android:textSize="18sp"/>
        <
Button
           
android:id="@+id/btn_delete"
           
android:layout_width="0dp"
           
android:layout_height="wrap_content"
           
android:layout_marginRight="2dp"
           
android:layout_weight="1"
           
android:background="#ACD6FF"
           
android:text="删除"
            
android:textSize="18sp"/>
    </
LinearLayout>
    <
TextView
       
android:id="@+id/tv_show"
       
android:layout_width="match_parent"
       
android:layout_height="wrap_content"
       
android:layout_marginTop="25dp"
       
android:textSize="20sp"
   
/>

</
LinearLayout>

MainActivity.java:

package com.example.directory;

import androidx.appcompat.app.AppCompatActivity;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;


public class MainActivity extends AppCompatActivity implements
       
View.OnClickListener {
            MyHelper
myHelper;
            
private EditText mEtName;
           
private EditText mEtPhone;
           
private TextView mTvShow;
           
private Button mBtnAdd;
           
private Button mBtnQuery;
           
private Button mBtnUpdate;
           
private Button mBtnDelete;

           
@Override
           
protected void onCreate(Bundle savedInstanceState) {
               
super.onCreate(savedInstanceState);
                setContentView(R.layout.
activity_main);
               
myHelper = new MyHelper(this);
                init();
            }
//
创建一个init()方法用于初始化界面控件并设置“添加”、“查询”、“修改”、“删除”按钮的点击监听事件
            private void init() {
               
mEtName = (EditText) findViewById(R.id.et_name);
               
mEtPhone = (EditText) findViewById(R.id.et_phone);
               
mTvShow = (TextView) findViewById(R.id.tv_show);
               
mBtnAdd = (Button) findViewById(R.id.btn_add);
               
mBtnQuery = (Button) findViewById(R.id.btn_query);
               
mBtnUpdate = (Button) findViewById(R.id.btn_update);
               
mBtnDelete = (Button) findViewById(R.id.btn_delete);

                mBtnAdd.setOnClickListener(this);
               
mBtnQuery.setOnClickListener(this);
               
mBtnUpdate.setOnClickListener(this);
               
mBtnDelete.setOnClickListener(this);
            }
           
@Override
           
public void onClick(View v){
                String name,phone;
                SQLiteDatabase db;
                ContentValues values;
               
switch(v.getId()){

//通过SQLiteDatabase类的insert()方法将姓名和电话信息添加到数据库中
                    case R.id.btn_add:
                        name=
mEtName.getText().toString();
                        phone=
mEtPhone.getText().toString();
                        db=
myHelper.getWritableDatabase();//获取可读写的SQLiteDatabase对象
                        values=new ContentValues();//创建ContentValues对象
                        values.put("name",name);
                        values.put(
"phone",phone);
                        db.insert(
"information",null,values);
                        Toast.makeText(
this,"信息已添加",Toast.LENGTH_SHORT).show();
                        db.close();
                       
break;

//通过SQLiteDatabase类的query()方法将姓名和电话信息查询出来,并显示在界面上
                    case R.id.btn_query:
                        db=
myHelper.getWritableDatabase();
                        Cursor cursor=db.query(
"information",null,null,null,null,null,null);
                       
if(cursor.getCount()==0){
                           
mTvShow.setText("");
                            Toast.makeText(
this,"没有数据",Toast.LENGTH_SHORT).show();
                        }
else{
                            cursor.moveToFirst();
                           
mTvShow.setText("Name: "+cursor.getString(1)+ " ;Tel: "+cursor.getString(2));
                        }
                        
while (cursor.moveToNext()){
                           
mTvShow.append("\n"+"Name : "+cursor.getString(1)+" ;Tel: "+cursor.getString(2));
                        }
                        cursor.close();
                        db.close();
                       
break;

//通过SQLiteDatabase类的update()方法修改数据库中的姓名和电话信息
                    case R.id.btn_update:
                        db=
myHelper.getWritableDatabase();
                        values=
new ContentValues();
                        values.put(
"phone",phone=mEtPhone.getText().toString());
                        db.update(
"information",values,"name=?",
                               
new String[]{mEtName.getText().toString()});
                        Toast.makeText(
this,"信息已修改",Toast.LENGTH_SHORT).show();
                        db.close();
                       
break;

//通过SQLiteDatabase类的delete()方法删除数据库中的姓名和电话信息
                    case R.id.btn_delete:
                        db=
myHelper.getWritableDatabase();
                        db.delete(
"information",null,null);
                        Toast.makeText(
this,"信息已删除",Toast.LENGTH_SHORT).show();
                       
mTvShow.setText("");
                        db.close();
                       
break;
                }
            }
}

MyHelper.java:数据库的创建

package com.example.directory;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
class MyHelper extends SQLiteOpenHelper {
   
public MyHelper(Context context) {
       
super(context, "itcast.db", null, 1);
    }

   
@Override
   
public void onCreate(SQLiteDatabase db) {

//初始化数据库的表结构,id整形字段,为关键字,新纪录插入表中时使用autoincrement关键字字段会自动递增,为每条新纪录生成一个唯一标识符,确保主键唯一性
        db.execSQL("CREATE TABLE information(_id INTEGER PRIMARY KEY AUTOINCREMENT, name VARCHAR(20),  phone VARCHAR(20))");
    }

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

  • 运行结果与分析

添加数据、查询数据:                                              

修改数据:

删除数据:

  • 实验体会与收获

  一开始MyHelper.java部分的代码与MainActivity写在一起,但MyHelper代码会报错,在CSDN查询后把MyHelper.java单独创建java class成功解决报错问题。代码问题解决后开始在虚拟机上运行,一开始可以正常添加name和phone但不可以查询数据,关闭app再打开会出现keeps stopping错误,如图所示

解决办法是将如下代码

   mEtName = EditText findViewById(R.id.et_name);
               
mEtPhone = EditText findViewById(R.id.et_phone);
               
mTvShow = TextView findViewById(R.id.tv_show);
               
mBtnAdd = Button findViewById(R.id.btn_add);
               
mBtnQuery = Button findViewById(R.id.btn_query);
               
mBtnUpdate = Button findViewById(R.id.btn_update);
               
mBtnDelete = Button findViewById(R.id.btn_delete);

修改为

   mEtName = (EditText) findViewById(R.id.et_name);
               
mEtPhone = (EditText) findViewById(R.id.et_phone);
               
mTvShow = (TextView) findViewById(R.id.tv_show);
               
mBtnAdd = (Button) findViewById(R.id.btn_add);
               
mBtnQuery = (Button) findViewById(R.id.btn_query);
               
mBtnUpdate = (Button) findViewById(R.id.btn_update);
               
mBtnDelete = (Button) findViewById(R.id.btn_delete);

具体原因还不太清楚,后续会继续查询资料、问老师弄懂。

个人疑点:delete功能是把数据库中所有联系人的信息都删除,怎么用代码实现一个Delete删除既可以删除所有联系人信息也可以根据需求删除部分联系人信息。

本次实验使用到的SQLite是Android自带的一个轻量级数据库,运算速度快,占用资源少,支持基本SQL语法,作为复杂数据的存储引擎,存储用户信息。

  • 20
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值