实验报告
课程名称 |
基于Android平台移动互联网开发 |
实验日期 |
2016年5月6日 |
||
实验项目名称 |
SQLite数据库操作 |
实验地点 |
S30010 |
||
实验类型 |
□验证型 √设计型 □综合型 |
学 时 |
|||
一、实验目的及要求(本实验所涉及并要求掌握的知识点) |
|||||
|
|||||
二、实验环境(本实验所使用的硬件设备和相关软件) |
|||||
(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,实现联系人详细信息显示功能。 |
|||||
四、实验结果(本实验源程序清单及运行结果或实验结论、实验设计图) |
|||||
代码: detail.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:orientation="vertical" > <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="0.00" android:orientation="horizontal" > <TextView android:id="@+id/tname" android:layout_width="77dp" android:layout_height="wrap_content" android:text=" 姓名: " android:textSize="18dp" /> <EditText android:id="@+id/ename" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:ems="10" android:inputType="textPersonName" > <requestFocus /> </EditText> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" > <TextView android:id="@+id/tguhua" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="固定电话:" android:textSize="18dp" /> <EditText android:id="@+id/eguhua" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:ems="10" android:inputType="phone" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="0.00" android:orientation="horizontal" > <TextView android:id="@+id/tshouji" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="0.00" android:text="移动电话:" android:textSize="18dp"/> <EditText android:id="@+id/eshouji" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="0.00" android:ems="10" android:inputType="phone" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="0.00" android:orientation="horizontal" > <TextView android:id="@+id/temail" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="电子邮件:" android:textSize="18dp"/> <EditText android:id="@+id/eemail" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:ems="10" android:inputType="textEmailAddress" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="0.00" android:orientation="horizontal" > <TextView android:id="@+id/tpost" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="邮政编码:" android:textSize="18dp" /> <EditText android:id="@+id/epost" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:ems="10" android:inputType="number" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="0.00" android:orientation="horizontal" > <TextView android:id="@+id/tadress" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="通讯地址:" android:textSize="18dp"/> <EditText android:id="@+id/eadress" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:ems="10" android:inputType="textPostalAddress" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="0.00" android:orientation="horizontal" > <TextView android:id="@+id/tgongsi" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="公司名称:" android:textSize="18dp"/> <EditText android:id="@+id/egongsi" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:ems="10" /> </LinearLayout> <Button android:id="@+id/btnsave" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="保存" /> </LinearLayout> Myopenhelper.java package com.example.contacts; import android.R.string; import android.app.Activity; import android.content.Context; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteDatabase.CursorFactory; import android.database.sqlite.SQLiteOpenHelper; import android.util.Log; public class Myopenhelper extends SQLiteOpenHelper{ public static final String DB_NAME="personal_contacts"; //数据库文件名 public static final String TABLE_NAME="contacts"; //表名 public static final String ID="id"; //ID public static final String NAME="name"; //名称 public static final String PHONE="phone"; //固话 public static final String MOBTLE="mobile"; //手机 public static final String EMAIL="email"; //email public static final String POST="post"; //邮编 public static final String ADD="address"; //地址 public static final String GONGSI="gongsi"; //公司 public Myopenhelper(Context context, String name, CursorFactory factory, int version) { super(context, name, factory, version); } @Override public void onCreate(SQLiteDatabase db) { db.execSQL("creat table id no exists"+TABLE_NAME+"(" +ID+"integer primary key," +NAME+"varchar," +PHONE+"varchar," +MOBTLE+"varchar," +EMAIL+"varchar," +POST+"varchar," +ADD+"varchar," +GONGSI+"varchar)"); } @Override public void onUpgrade(SQLiteDatabase db, int arg1, int arg2) { db.execSQL("if exists"+TABLE_NAME); onCreate(db); Log.e("Datsbase", "onUpgrade"); } } DetailActivity.java package com.example.contacts; import android.app.Activity; import android.content.ContentValues; import android.content.Intent; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.EditText; import android.widget.Toast; public class DetailActivity extends Activity { EditText ename, eguhua, eshouji, eemail, epost, eadress, egongsi; String name, guhua, shouji, email, post, adress, gongsi; Button btnsave; SQLiteDatabase userdatabase; Bundle bd; String selectname; protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.detail); btnsave = (Button) findViewById(R.id.btnsave); ename = (EditText) findViewById(R.id.ename); eguhua = (EditText) findViewById(R.id.eguhua); eshouji = (EditText) findViewById(R.id.eshouji); eemail = (EditText) findViewById(R.id.eemail); epost = (EditText) findViewById(R.id.epost); eadress = (EditText) findViewById(R.id.eadress); egongsi = (EditText) findViewById(R.id.egongsi); 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(); ename.setText(cursor.getString(cursor.getColumnIndex("name"))); eguhua.setText(cursor.getString(cursor.getColumnIndex("guhua"))); eshouji.setText(cursor.getString(cursor.getColumnIndex("shouji"))); eemail.setText(cursor.getString(cursor.getColumnIndex("email"))); epost.setText(cursor.getString(cursor.getColumnIndex("post"))); eadress.setText(cursor.getString(cursor.getColumnIndex("adress"))); egongsi.setText(cursor.getString(cursor.getColumnIndex("gongsi"))); btnsave.setText("保存"); } btnsave.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub name = ename.getText().toString(); guhua = eguhua.getText().toString(); shouji = eshouji.getText().toString(); email = eemail.getText().toString(); post = epost.getText().toString(); adress = eadress.getText().toString(); gongsi = egongsi.getText().toString(); if (!name.equals("") && !guhua.equals("") && !shouji.equals("") && !email.equals("") && !post.equals("") && !adress.equals("") && !gongsi.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", guhua); cv.put("mobile", shouji); cv.put("email", email); cv.put("post", post); cv.put("addr", adress); cv.put("comp", gongsi); 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(); } } }); } } MainActivity.java package com.example.contacts; import java.util.ArrayList; import java.util.HashMap; import android.os.Bundle; import android.app.Activity; import android.content.Intent; import android.view.Menu; 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; public class MainActivity extends Activity { private ListView lv; private Button addbtn; private Button deletebtn; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); addbtn=(Button)findViewById(R.id.button1); addbtn.setOnClickListener(new OnClickListener() { public void onClick(View arg0) { Intent intent2=new Intent(); intent2.setClass(MainActivity.this, DetailActivity.class); startActivity(intent2); finish(); } }); deletebtn=(Button) findViewById(R.id.button2); deletebtn.setOnClickListener(new OnClickListener() { @Override public void onClick(View arg0) { // TODO Auto-generated method stub // db.delete("contacts","name=?",new string[]{s}); } }); } } @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; } } |
五、实验总结(对本实验结果进行分析,实验心得体会及改进意见) |
|||||
这次的实验没有成功。 |