当前位置:文档之家› Android实验报告_基于SQLite的通信录

Android实验报告_基于SQLite的通信录

第一次实验Android界面设计一. 实验目的及实验环境1. 实验目的1)掌握SQLiteOpenHelper类结构2)掌握基于SQLite数据库的应用开发过程3)掌握Content Provider发布数据的方法4)掌握Content Resolver获取数据的方法2.实验环境系统开发平Android Studio 3.0系统开发平台:Android 7.1运行平台:Windows10 x64运行环境: Framework SDK 2.0二. 实验教材、组织方式、实验容1.实验教材:Andorid开发与应用2.组织方式:个人独立完成2.实验容:实现基于SQLite数据库的通信录应用,通过单击增加图标打开添加通信录界面,通过单击通信录中的各条信息可删除选中项。

三.方案设计Android系统中集成了SQLite数据库,并且为数据库的操作提供了相关的类和方法,便于没有数据库开发经验的开发者编写程序。

另外,Android平台中利用Content Provider机制来实现跨应用程序数据共享。

一个应用程序可以通过Content Provider来发布自己的数据,其他的应用程序可以通过Content Resolver来获取共享数据。

四.运行结果五.总结通过这次实验掌握了SQLite OpenHelper类结构,掌握了基于SQLite数据库的应用开发过程以及Content Provider发布数据的方法和掌握Content Resolver 获取数据的方法。

六.附录:源代码主布局文件activity_main.xml:<LinearLayout xmlns:android="schemas.android./apk/res/android"<TextViewandroid:id="+id/title"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="通信录"/><ListViewandroid:id="+id/listView"android:layout_width="wrap_content"android:layout_height="wrap_content"></ListView> <Buttonandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:onClick="add"/></LinearLayout>添加联系人布局文件addrelation.xml:<?xml version="1.0"encoding="utf-8"?><LinearLayout xmlns:android="schemas.android./apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical"><TextViewandroid:layout_height="wrap_content"android:text=""/><EditTextandroid:id="+id/addName"android:layout_width="wrap_content"android:layout_height="wrap_content"/> <TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text=""/><EditTextandroid:id="+id/addTel"android:layout_width="wrap_content"android:layout_height="wrap_content"/> <TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="所属组"/><Spinnerandroid:id="+id/addGroup"android:layout_width="wrap_content"android:entries="array/test"></Spinner><Buttonandroid:id="+id/save"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_gravity="center"android:text=" 保存"/></LinearLayout>主Activity文件MainActivity.java:import android.support.v7.app.ActionBarActivity;import android.app.AlertDialog;import android.app.Application;import android.content.DialogInterface;import android.content.Intent;import android.database.Cursor;import android.os.Bundle;import android.view.Menu;import android.view.MenuItem;import android.view.View;import android.widget.AdapterView;import android.widget.ListView;import android.widget.SimpleCursorAdapter;public class MainActivity extends ActionBarActivity {private ListView listView;protected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(yout.activity_main);listView = (ListView) findViewById(R.id.listView);// getRelationFromDB();}private void getRelationFromDB() {final DatabaseHelper dbHelper = new DatabaseHelper(this);Cursor cursor = dbHelper.query();String[] from = { "_id", "name", "tel", "groupName" };int[] to = { R.id._id, , R.id.tel, R.id.group };SimpleCursorAdapter scadapter = new SimpleCursorAdapter(this,yout.relationlist, cursor, from, to);listView.setAdapter(scadapter);listView.setOnItemClickListener(new AdapterView.OnItemClickListener() { public void onItemClick(AdapterView<?> adapter, View view,int position, long id) {final long temp = id;AlertDialog.Builder adBuilder = new AlertDialog.Builder(MainActivity.this);adBuilder.setMessage("确认要删除记录吗?").setPositiveButton("确认",new DialogInterface.OnClickListener() {Overridepublic void onClick(DialogInterface dialog,int which) {// TODO Auto-generated method stubdbHelper.del((int) temp);Cursor cursor = dbHelper.query();String[] from = { "_id", "name", "tel","groupName" };int[] to = { R.id._id, ,R.id.tel, R.id.group };SimpleCursorAdapter scadapter = new SimpleCursorAdapter (getApplicationContext(),yout.relationlist, cursor,from, to);MainActivity.this.listView.setAdapter(scadapter);}}).setNegativeButton("取消",new DialogInterface.OnClickListener() {Overridepublic void onClick(DialogInterface dialog,int which) {}});AlertDialog aleraDialog = adBuilder.create();aleraDialog.show();}});dbHelper.close();}public void add(View view) {Intent intent = new Intent(MainActivity.this, AddrelationActivity.class);startActivityForResult(intent, 0x111);}protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data);if (requestCode == 0x111 && resultCode == 0x111) {getRelationFromDB();}}Overridepublic 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;}Overridepublic boolean onOptionsItemSelected(MenuItem item) {// Handle action bar item clicks here. The action bar will// automatically handle clicks on the Home/Up button, so long// as you specify a parent activity in AndroidManifest.xml.int id = item.getItemId();if (id == R.id.action_settings) {return true;}return super.onOptionsItemSelected(item);}}添加联系人AddrelationActivity.javaimport android.app.Activity;import android.app.AlertDialog;import android.content.ContentValues;import android.content.DialogInterface;import android.content.Intent;import android.os.Bundle;import android.view.View;import android.widget.EditText;import android.widget.Spinner;public class AddrelationActivity extends Activity {private EditText addName, addTel;private Spinner addGroup;protected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(yout.addrelation);addName = (EditText) findViewById(R.id.addName);addTel = (EditText) findViewById(R.id.addTel);addGroup = (Spinner) findViewById(R.id.addGroup);}public void save(View view) {final ContentValues values = new ContentValues();values.put("name", addName.getText().toString());values.put("tel", addTel.getText().toString());values.put("groupName", addGroup.getSelectedItem().toString());final DatabaseHelper dbHelper = new DatabaseHelper(getApplicationContext());final AlertDialog.Builder adBuilder = new AlertDialog.Builder(this);adBuilder.setMessage("确认保存记录吗?").setPositiveButton("确认", new DialogInterface.OnClickListener() {Overridepublic void onClick(DialogInterface dialog, int which) {// TODO Auto-generated method stubdbHelper.insert(values);Intent intent = getIntent();setResult(0x111, intent);AddrelationActivity.this.finish();}}).setNegativeButton("取消", new DialogInterface.OnClickListener() {public void onClick(DialogInterface dialog, int which) {}});AlertDialog aleraDialog = adBuilder.create();aleraDialog.show();}}。

相关主题