实验报告
课程名称 |
基于Android平台移动互联网开发 |
实验日期 |
4月29日 |
||
实验项目名称 |
开发一个Android应用程序 |
实验地点 |
S3010 |
||
实验类型 |
□验证型 √设计型 □综合型 |
学 时 |
|||
一、实验目的及要求(本实验所涉及并要求掌握的知识点) |
|||||
设计一个个人通讯录,掌握Android平台下的数据库开发,该个人通讯录主要包括联系人列表和联系人详细信息等界面。 程序主界面是通讯录的目录显示手机上联系人的名称。点击联系人的姓名可以显示联系人的详细信息。在按了MEMU键之后会弹出菜单栏。单击菜单栏上的按钮可以添加联系人和删除联系人 |
|||||
二、实验环境(本实验所使用的硬件设备和相关软件) |
|||||
(1)PC机 (2)操作系统:Windows XP (3)软件: Eclipse, JDK1.6,Android SDK,ADT |
|||||
三、实验内容及步骤 |
|||||
一、 1)新建工程 2)修改布局文件activity_main.xml 3)开发布局文件link.xml用于显示联系人列表。 4)layout目录下新建一个detail.xml,用于显示联系人详细信息 5)开发数据库辅助类MyOpenHelper类,新建一个MyOpenHelper.java。
6)接下来便进入MainActivity端的开发,实现数据库增加、删除、修改记录等操作 |
|||||
四、实验结果(本实验源程序清单及运行结果或实验结论、实验设计图) |
|||||
代码: 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); } } detail_main关键代码 package com.example.contactsch; import android.annotation.SuppressLint; import android.app.Activity; import android.content.ContentValues; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; import android.os.Bundle; import android.util.Log; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.EditText; import android.widget.ImageButton; import android.widget.Toast; public class detail_main extends Activity{ private MyOpenHelper dbHelper; private EditText ename,ephone,emobile,eemail,epost,eaddr,ecomp; private ImageButton bc; @SuppressLint("NewApi") @Override
protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.detail); ename=(EditText)findViewById(R.id.etname); ephone=(EditText)findViewById(R.id.etphone); emobile=(EditText)findViewById(R.id.etmobile); eemail=(EditText)findViewById(R.id.etemail); epost=(EditText)findViewById(R.id.etpost); eaddr=(EditText)findViewById(R.id.etaddr); ecomp=(EditText)findViewById(R.id.etcomp); bc=(ImageButton)findViewById(R.id.bcButton1); Bundle bundle=new Bundle(); bundle=getIntent().getExtras(); String nString=bundle.getString("name"); ename.setText(nString); dbHelper=new MyOpenHelper(detail_main.this, "personal_contacts.db", null, 1); final SQLiteDatabase db=dbHelper.getReadableDatabase(); Cursor cursorg=db.rawQuery("select * from contacts where name=?",new String[] {nString}, null); if (cursorg.getCount()>0) { cursorg.moveToFirst(); ephone.setText(cursorg.getString(cursorg.getColumnIndex("phone"))); emobile.setText(cursorg.getString(cursorg.getColumnIndex("mobile"))); eemail.setText(cursorg.getString(cursorg.getColumnIndex("email"))); epost.setText(cursorg.getString(cursorg.getColumnIndex("post"))); eaddr.setText(cursorg.getString(cursorg.getColumnIndex("addr"))); ecomp.setText(cursorg.getString(cursorg.getColumnIndex("comp"))); } bc.setOnClickListener(new OnClickListener() {
@SuppressLint("NewApi") @Override public void onClick(View arg0) { // TODO Auto-generated method stub String sname=ename.getText().toString(); Cursor cursor=db.rawQuery("select * from contacts where name=?",new String[] {sname}, null); if (cursor.getCount() > 0) { Toast.makeText(detail_main.this, "名片已存在", Toast.LENGTH_LONG).show(); }else { ContentValues values = new ContentValues(); values.put("name", ename.getText().toString()); values.put("phone", ephone.getText().toString()); values.put("mobile", emobile.getText().toString()); values.put("email", eemail.getText().toString()); values.put("post", epost.getText().toString()); values.put("addr", eaddr.getText().toString()); values.put("comp", ecomp.getText().toString()); db.insert("contacts", null, values); values.clear(); Toast.makeText(detail_main.this,"名片存入成功" ,Toast.LENGTH_LONG).show(); } } }); } } MyOpenHelper关键代码 package com.example.contactsch; 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 CREAT_USER="create table contacts(id integer primary key autoincrement," + "name," + "phone," + "mobile," + "email," + "post," + "addr," + "comp)"; public static final String DB_NAME="personal_contacts"; public static final String TABLE_NAME="contacts"; public static final String ID="id"; public static final String NAME="name"; public static final String PHONE="phone"; public static final String MOBILE="mobile"; public static final String EMAIL="email"; public static final String POST="post"; public static final String ADDR="addr"; public static final String COMP="comp"; public MyOpenHelper(Context context, String name, CursorFactory factory, int version) { super(context, name, factory, version); // TODO Auto-generated constructor stub } @Override public void onCreate(SQLiteDatabase db) { // TODO Auto-generated method stub db.execSQL(CREAT_USER); Log.i("test", "777777777777777777777777"); } @Override public void onUpgrade(SQLiteDatabase arg0, int arg1, int arg2) { // TODO Auto-generated method stub
} } 运行结果:(截图) 主界面
点击添加增加添加信息
点击listview中的222
|
|||||
五、实验总结(对本实验结果进行分析,实验心得体会及改进意见) |
|||||
作业量有点大,实现的功能很多,还有删除不知如何能够实现,逻辑思路也没有,对于listview的语句还不是很熟悉,特别是点击事件返回的4个数值不知是什么意思,加大了难度.对于那种返回参数太多的语句会很乱,也不知什么意思.希望老师以后再文档中注释. |
|||||
实验评语 |
|
||||
实验成绩 |
|
指导教师签名: 年 月 日 |
|||