标签:
课程名称 |
基于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,实现联系人详细信息显示功能。
|
|||||
四、实验结果(本实验源程序清单及运行结果或实验结论、实验设计图) |
|||||
代码: ackage com.example.tongxunlu; import android.os.Bundle; import android.app.Activity; import android.content.Intent; import android.database.Cursor; import android.support.v4.widget.CursorAdapter; import android.support.v4.widget.SimpleCursorAdapter; 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; public class MainActivity extends Activity { Button btn1,btn2; ListView listview; public static MyOpenHelper dbhelper; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); btn1=(Button)findViewById(R.id.button1); btn2=(Button)findViewById(R.id.button2); listview=(ListView)findViewById(R.id.lv); dbhelper=new MyOpenHelper(MainActivity.this, "myDict.db10", 1); Cursor cursor=dbhelper.getReadableDatabase().rawQuery("select * from contacts", null); inflateList(cursor); btn1.setOnClickListener(new OnClickListener() { @Override public void onClick(View arg0) { Intent intent=new Intent(); intent.setClass(MainActivity.this, BaocunActivity.class); startActivity(intent); MainActivity.this.finish(); } }); btn2.setOnClickListener(new OnClickListener() { @Override public void onClick(View arg0) { Intent intent=new Intent(); intent.setClass(MainActivity.this, shanchuActivity.class); startActivity(intent); MainActivity.this.finish(); } }); } private void inflateList(Cursor cursor) { SimpleCursorAdapter simpleCursorAdapter=new SimpleCursorAdapter(MainActivity.this, R.layout.lian, cursor, new String[]{"name","phone"}, new int[]{R.id.text1,R.id.text2}, CursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER); listview.setAdapter(simpleCursorAdapter); listview.setOnItemClickListener(new ItemClickListener()); } private final class ItemClickListener implements OnItemClickListener{ public void onItemClick(AdapterView<?> parent, View view, int position, long id) { ListView listview = (ListView) parent; Cursor cursor1 = (Cursor) listview.getItemAtPosition(position); String personid = cursor1.getString(cursor1.getColumnIndex("name")); String personid1 = cursor1.getString(cursor1.getColumnIndex("phone")); String personid2 = cursor1.getString(cursor1.getColumnIndex("mobile")); String personid3 = cursor1.getString(cursor1.getColumnIndex("email")); String personid4 = cursor1.getString(cursor1.getColumnIndex("post")); String personid5 = cursor1.getString(cursor1.getColumnIndex("addr")); String personid6 = cursor1.getString(cursor1.getColumnIndex("comp")); Intent intent=new Intent(); intent.putExtra("username1",personid); intent.putExtra("userphone1",personid1); intent.putExtra("usermobile1",personid2); intent.putExtra("useremail1",personid3); intent.putExtra("userpost1",personid4); intent.putExtra("useraddr1",personid5); intent.putExtra("usercomp1",personid6); intent.setClass(MainActivity.this, DetailActivity.class); startActivity(intent); MainActivity.this.finish(); } } @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; } } package com.example.tongxunlu; import android.app.Activity; import android.content.Intent; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; import android.os.Bundle; import android.support.v4.widget.CursorAdapter; import android.support.v4.widget.SimpleCursorAdapter; import android.view.Menu; import android.view.View; import android.view.View.OnClickListener; import android.widget.AdapterView; import android.widget.Button; import android.widget.ListView; import android.widget.Toast; import android.widget.AdapterView.OnItemClickListener; public class shanchuActivity extends Activity { Button btn; ListView listview1; Cursor cursor; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.shanchu); listview1=(ListView)findViewById(R.id.lv1); btn=(Button)findViewById(R.id.btn2); cursor=MainActivity.dbhelper.getReadableDatabase().rawQuery("select * from contacts", null); inflateList(cursor); btn.setOnClickListener(new OnClickListener() { @Override public void onClick(View arg0) { Intent intent=new Intent(); intent.setClass(shanchuActivity.this,MainActivity.class); startActivity(intent); shanchuActivity.this.finish(); } }); } private void inflateList(Cursor cursor) { SimpleCursorAdapter simpleCursorAdapter=new SimpleCursorAdapter(shanchuActivity.this, R.layout.lian, cursor, new String[]{"name","phone"}, new int[]{R.id.text1,R.id.text2}, CursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER); listview1.setAdapter(simpleCursorAdapter); listview1.setOnItemClickListener(new ItemClickListener()); } private final class ItemClickListener implements OnItemClickListener{ public void onItemClick(AdapterView<?> parent, View view, int position, long id) { ListView listview = (ListView) parent; Cursor cursor1 = (Cursor) listview.getItemAtPosition(position); String personid = cursor1.getString(cursor1.getColumnIndex("name")); SQLiteDatabase db = MainActivity.dbhelper.getWritableDatabase(); db.delete("contacts", "name="+"‘"+personid+"‘", null); db.close(); Toast.makeText(shanchuActivity.this, "删除成功!", Toast.LENGTH_LONG).show(); inflateList(cursor); } } 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; } } package com.example.tongxunlu; import android.app.Activity; import android.content.Intent; import android.database.Cursor; import android.os.Bundle; import android.support.v4.widget.CursorAdapter; import android.support.v4.widget.SimpleCursorAdapter; import android.view.Menu; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.EditText; import android.widget.ListView; import android.widget.Toast; public class BaocunActivity extends Activity{ EditText editTextNAME,editTextPHONE,editTextMOBILE,editTextEMAIL,editTextPOST,editTextADDR,editTextCOMP; ListView listview; Button btn,btn1; protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.baocun); btn=(Button)findViewById(R.id.button1); btn1=(Button)findViewById(R.id.button2); editTextNAME=(EditText)findViewById(R.id.editText1); editTextPHONE=(EditText)findViewById(R.id.editText2); editTextMOBILE=(EditText)findViewById(R.id.editText3); editTextEMAIL=(EditText)findViewById(R.id.editText4); editTextPOST=(EditText)findViewById(R.id.editText5); editTextADDR=(EditText)findViewById(R.id.editText6); editTextCOMP=(EditText)findViewById(R.id.editText7); listview=(ListView)findViewById(R.id.lv); btn.setOnClickListener(new OnClickListener() { @Override public void onClick(View arg0) { String name,phone,mobile,email,post,addr,comp; name=editTextNAME.getText().toString(); phone=editTextPHONE.getText().toString(); mobile=editTextMOBILE.getText().toString(); email=editTextEMAIL.getText().toString(); post=editTextPOST.getText().toString(); addr=editTextADDR.getText().toString(); comp=editTextCOMP.getText().toString(); MainActivity.dbhelper.getReadableDatabase().execSQL("insert into contacts values(null,?,?,?,?,?,?,?)",new String[]{name,phone,mobile,email,post,addr,comp}); Toast.makeText(BaocunActivity.this, "保存成功!", Toast.LENGTH_LONG).show(); } }); btn1.setOnClickListener(new OnClickListener() { @Override public void onClick(View arg0) { Intent intent=new Intent(); intent.setClass(BaocunActivity.this,MainActivity.class); startActivity(intent); BaocunActivity.this.finish(); } }); } @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; } }
运行结果:(截图) |
|||||
五、实验总结(对本实验结果进行分析,实验心得体会及改进意见) |
|||||
这次实验老师在上课的时候教过sql,但是这个实验太难,所以实现起来感觉超级难,而且前面的知识又忘了,在设计通讯录的过程中,出现了很多错误,还是要百度解决问题。 |
|||||
实验评语 |
|
||||
实验成绩 |
|
指导教师签名: 年 月 日 |
|||
|
标签:
原文地址:http://www.cnblogs.com/woaiQ1314/p/5487568.html