实验8

实验报告

课程名称

基于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代码:

package com.example.contactsch;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

 

import android.app.Activity;
import android.app.LauncherActivity.ListItem;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.net.LocalSocketAddress.Namespace;
import android.os.Bundle;
import android.support.v4.widget.CursorAdapter;
import android.support.v4.widget.SimpleCursorAdapter;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;

public class MainActivity extends Activity {
 private ListView lView;
 private MyOpenHelper dbHelper;
 private Button delb,addb;
 SQLiteDatabase db;
 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  delb=(Button)findViewById(R.id.del);
  addb=(Button)findViewById(R.id.add);
  lView=(ListView)findViewById(R.id.listV);
  Log.i("test", "111111111111111111111");
  dbHelper=new MyOpenHelper(MainActivity.this, "personal_contacts.db", null, 1);
  db=dbHelper.getReadableDatabase();
  Cursor cursor=db.rawQuery("select * from contacts", null);
  Log.i("test", "2222222222222222222222");
  inflateList(cursor);
  Log.i("test", "3333333333333333333333");
  addb.setOnClickListener(new OnClickListener() {
   
   @Override
   public void onClick(View arg0) {
    // TODO Auto-generated method stub
    Bundle bundle=new Bundle();
    bundle.putString("name", "");
    Intent intent=new Intent();
    intent.setClass(MainActivity.this, detail_main.class);
    intent.putExtras(bundle);
    startActivity(intent);
   }
  });
  lView.setOnItemClickListener(new OnItemClickListener() {

   @Override
   public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
     long arg3) {
    // TODO Auto-generated method stub
    TextView ser=(TextView)arg1.findViewById(R.id.tView1);
    String se=ser.getText().toString();
    Intent intent=new Intent(MainActivity.this,detail_main.class);
    Bundle bundle=new Bundle();
    bundle.putString("name", se);
    intent.putExtras(bundle);
    startActivity(intent);
    
   }
  });

 }

 private void inflateList(Cursor cursor) {
  // TODO Auto-generated method stub
  int c=cursor.getCount();
  String[] nameS=new String[c];
  String[] phoneS=new String[c];
  int co=0;
  while(cursor.moveToNext()){
   nameS[co]=cursor.getString(cursor.getColumnIndex("name"));
   phoneS[co]=cursor.getString(cursor.getColumnIndex("phone"));
   co++;
  }
  final ArrayList<HashMap<String, Object>> listItem=new ArrayList<HashMap<String,Object>>();
  for (int i = 1; i <= c; i++) {
   HashMap<String, Object> map=new HashMap<String, Object>();
   map.put("ItemTitle", nameS[i-1]);
   map.put("ItemText", phoneS[i-1]);
   listItem.add(map);
    
  }
  
  Log.i("test", "66666666666666666666666666666");
  if (cursor.getCount()!=0) {
   Log.i("test", "1000000000000000000000000000");
   SimpleAdapter sAdapter=new SimpleAdapter(MainActivity.this, listItem, R.layout.link,
     new String[]{"ItemTitle","ItemText"} , new int[]{R.id.tView1,R.id.tView2});
   Log.i("test", "10101010101010101010101010101");
   lView.setAdapter(sAdapter);
  }
  Log.i("test", "4444444444444444");
  Log.i("test", "55555555555555555555");
 }

 @Override
 public boolean onCreateOptionsMenu(Menu menu) {
  // Inflate the menu; this adds items to the action bar if it is present.
  MenuInflater inflater=new MenuInflater(this);
  inflater.inflate(R.menu.main, menu);
  return super.onCreateOptionsMenu(menu);
 }

}

DetailActivity代码:

<?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:orientation="vertical" >

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:layout_marginTop="10dp">

        <TextView
            android:id="@+id/name"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="姓名:"
            android:textSize="20sp" />

        <EditText
            android:id="@+id/etname"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:ems="10"
            android:textSize="25sp"
            android:background="#eeffffff" >
        </EditText>
    </LinearLayout>

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:layout_marginTop="10dp">
        <TextView
            android:id="@+id/phone"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="固定电话:"
            android:textSize="20sp" />

        <EditText
            android:id="@+id/etphone"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:ems="10"
            android:textSize="25sp"
            android:background="#eeffffff">
        </EditText>
    </LinearLayout>

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:layout_marginTop="10dp">
        <TextView
            android:id="@+id/mobile"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="移动电话:"
            android:textSize="20sp" />

        <EditText
            android:id="@+id/etmobile"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:ems="10"
            android:textSize="25sp"
            android:background="#eeffffff">
        </EditText>
    </LinearLayout>

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:layout_marginTop="10dp">

        <TextView
            android:id="@+id/email"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="电子邮件:"
            android:textSize="20sp" />

        <EditText
            android:id="@+id/etemail"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:ems="10"
            android:textSize="25sp"
            android:background="#eeffffff">
        </EditText>
    </LinearLayout>

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:layout_marginTop="10dp">

        <TextView
            android:id="@+id/post"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="邮政编码:"
            android:textSize="20sp" />

        <EditText
            android:id="@+id/etpost"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:ems="10"
            android:textSize="25sp"
            android:background="#eeffffff">
        </EditText>
    </LinearLayout>

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:layout_marginTop="10dp">

        <TextView
            android:id="@+id/addr"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="通信地址:"
            android:textSize="20sp" />

        <EditText
            android:id="@+id/etaddr"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:ems="10"
            android:textSize="25sp"
            android:background="#eeffffff">
        </EditText>
    </LinearLayout>

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:layout_marginTop="10dp">

        <TextView
            android:id="@+id/comp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="公司名称:"
            android:textSize="20sp" />

        <EditText
            android:id="@+id/etcomp"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:ems="10"
            android:textSize="25sp"
            android:background="#eeffffff">
        </EditText>
    </LinearLayout>

    <ImageButton
        android:id="@+id/bcButton1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:src="@drawable/www" />

</LinearLayout>

运行结果:(截图)

 

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

    经过本次实验,对于如何使用数据库数据来制作通讯录,对数据库有了一定的了解。通过查阅资料以及老师上课的讲解对于该怎么使用数据库还是有所了解的,只是过程并没有那么熟悉,要继续多加练习。

实验评语

 

实验成绩

 

指导教师签名:              年   月   日

           
原文地址:https://www.cnblogs.com/VernSean/p/5488226.html