Android实验报告基于SQLite的通信录.docx
《Android实验报告基于SQLite的通信录.docx》由会员分享,可在线阅读,更多相关《Android实验报告基于SQLite的通信录.docx(12页珍藏版)》请在冰豆网上搜索。
Android实验报告基于SQLite的通信录
第一次实验Android界面设计
一.实验目的及实验环境
1.实验目的
1〕掌握SQLiteOpenHelper类构造
2〕掌握基于SQLite数据库的应用开发过程
3〕掌握ContentProvider发布数据的方法
4〕掌握ContentResolver获取数据的方法
2.实验环境
系统开发平AndroidStudio3.0
系统开发平台:
Android7.1
运行平台:
Windows10x64
运行环境:
Microsoft.NETFrameworkSDK2.0
二.实验教材、组织方式、实验容
1.实验教材:
Andorid开发与应用
2.组织方式:
个人独立完成
2.实验容:
实现基于SQLite数据库的通信录应用,通过单击增加图标翻开添加通信录界面,通过单击通信录中的各条信息可删除选中项。
三.方案设计
Android系统中集成了SQLite数据库,并且为数据库的操作提供了相关的类和方法,便于没有数据库开发经历的开发者编写程序。
另外,Android平台中利用ContentProvider机制来实现跨应用程序数据共享。
一个应用程序可以通过ContentProvider来发布自己的数据,其他的应用程序可以通过ContentResolver来获取共享数据。
四.运行结果
五.总结
通过这次实验掌握了SQLiteOpenHelper类构造,掌握了基于SQLite数据库的应用开发过程以及ContentProvider发布数据的方法和掌握ContentResolver获取数据的方法。
六.附录:
源代码
主布局文件activity_main.xml:
android="schemas.android./apk/res/android"android:id="+id/title"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="通信录"/>android:id="+id/listView"android:layout_width="wrap_content"android:layout_height="wrap_content">android:layout_width="wrap_content"android:layout_height="wrap_content"android:onClick="add"/>添加联系人布局文件addrelation.xml:xmlversion="1.0"encoding="utf-8"?>android="schemas.android./apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical">android:layout_width="wrap_content"android:layout_height="wrap_content"android:text=""/>android:id="+id/addName"android:layout_width="wrap_content"android:layout_height="wrap_content"/>android:layout_width="wrap_content"android:layout_height="wrap_content"android:text=""/>android:id="+id/addTel"android:layout_width="wrap_content"android:layout_height="wrap_content"/>android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="所属组"/>android:id="+id/addGroup"android:layout_width="wrap_content"android:layout_height="wrap_content"android:entries="array/test">android:id="+id/save"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_gravity="center"android:text="保存"/>主Activity文件MainActivity.java:importandroid.support.v7.app.ActionBarActivity;importandroid.app.AlertDialog;importandroid.app.Application;importandroid.content.DialogInterface;importandroid.content.Intent;importandroid.database.Cursor;importandroid.os.Bundle;importandroid.view.Menu;importandroid.view.MenuItem;importandroid.view.View;importandroid.widget.AdapterView;importandroid.widget.ListView;importandroid.widget.SimpleCursorAdapter;publicclassMainActivityextendsActionBarActivity{privateListViewlistView;protectedvoidonCreate(BundlesavedInstanceState){super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);listView=(ListView)findViewById(R.id.listView);//getRelationFromDB();}privatevoidgetRelationFromDB(){finalDatabaseHelperdbHelper=newDatabaseHelper(this);Cursorcursor=dbHelper.query();String[]from={"_id","name","tel","groupName"};int[]to={R.id._id,R.id.name,R.id.tel,R.id.group};SimpleCursorAdapterscadapter=newSimpleCursorAdapter(this,R.layout.relationlist,cursor,from,to);listView.setAdapter(scadapter);listView.setOnItemClickListener(newAdapterView.OnItemClickListener(){publicvoidonItemClick(AdapterView>adapter,Viewview,intposition,longid){finallongtemp=id;AlertDialog.BuilderadBuilder=newAlertDialog.Builder(MainActivity.this);adBuilder.setMessage("确认要删除记录吗?").setPositiveButton("确认",newDialogInterface.OnClickListener(){OverridepublicvoidonClick(DialogInterfacedialog,intwhich){//TODOAuto-generatedmethodstubdbHelper.del((int)temp);Cursorcursor=dbHelper.query();String[]from={"_id","name","tel","groupName"};int[]to={R.id._id,R.id.name,R.id.tel,R.id.group};SimpleCursorAdapterscadapter=newSimpleCursorAdapter(getApplicationContext(),R.layout.relationlist,cursor,from,to);MainActivity.this.listView.setAdapter(scadapter);}}).setNegativeButton("取消",newDialogInterface.OnClickListener(){OverridepublicvoidonClick(DialogInterfacedialog,intwhich){}});AlertDialogaleraDialog=adBuilder.create();aleraDialog.show();}});dbHelper.close();}publicvoidadd(Viewview){Intentintent=newIntent(MainActivity.this,AddrelationActivity.class);startActivityForResult(intent,0x111);}protectedvoidonActivityResult(intrequestCode,intresultCode,Intentdata){super.onActivityResult(requestCode,resultCode,data);if(requestCode==0x111&&resultCode==0x111){getRelationFromDB();}}OverridepublicbooleanonCreateOptionsMenu(Menumenu){//Inflatethemenu;thisaddsitemstotheactionbarifitispresent.getMenuInflater().inflate(R.menu.main,menu);returntrue;}OverridepublicbooleanonOptionsItemSelected(MenuItemitem){//Handleactionbaritemclickshere.Theactionbarwill//automaticallyhandleclicksontheHome/Upbutton,solong//asyouspecifyaparentactivityinAndroidManifest.xml.intid=item.getItemId();if(id==R.id.action_settings){returntrue;}returnsuper.onOptionsItemSelected(item);}}添加联系人AddrelationActivity.javaimportandroid.app.Activity;importandroid.app.AlertDialog;importandroid.content.ContentValues;importandroid.content.DialogInterface;importandroid.content.Intent;importandroid.os.Bundle;importandroid.view.View;importandroid.widget.EditText;importandroid.widget.Spinner;publicclassAddrelationActivityextendsActivity{privateEditTextaddName,addTel;privateSpinneraddGroup;protectedvoidonCreate(BundlesavedInstanceState){super.onCreate(savedInstanceState);setContentView(R.layout.addrelation);addName=(EditText)findViewById(R.id.addName);addTel=(EditText)findViewById(R.id.addTel);addGroup=(Spinner)findViewById(R.id.addGroup);}publicvoidsave(Viewview){finalContentValuesvalues=newContentValues();values.put("name",addName.getText().toString());values.put("tel",addTel.getText().toString());values.put("groupName",addGroup.getSelectedItem().toString());finalDatabaseHelperdbHelper=newDatabaseHelper(getApplicationContext());finalAlertDialog.BuilderadBuilder=newAlertDialog.Builder(this);adBuilder.setMessage("确认保存记录吗?").setPositiveButton("确认",newDialogInterface.OnClickListener(){OverridepublicvoidonClick(DialogInterfacedialog,intwhich){//TODOAuto-generatedmethodstubdbHelper.insert(values);Intentintent=getIntent();setResult(0x111,intent);AddrelationActivity.this.finish();}}).setNegativeButton("取消",newDialogInterface.OnClickListener(){publicvoidonClick(DialogInterfacedialog,intwhich){}});AlertDialogaleraDialog=adBuilder.create();aleraDialog.show();}}
android="schemas.android./apk/res/android"
android:id="+id/title"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="通信录"/>android:id="+id/listView"android:layout_width="wrap_content"android:layout_height="wrap_content">android:layout_width="wrap_content"android:layout_height="wrap_content"android:onClick="add"/>添加联系人布局文件addrelation.xml:xmlversion="1.0"encoding="utf-8"?>android="schemas.android./apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical">android:layout_width="wrap_content"android:layout_height="wrap_content"android:text=""/>android:id="+id/addName"android:layout_width="wrap_content"android:layout_height="wrap_content"/>android:layout_width="wrap_content"android:layout_height="wrap_content"android:text=""/>android:id="+id/addTel"android:layout_width="wrap_content"android:layout_height="wrap_content"/>android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="所属组"/>android:id="+id/addGroup"android:layout_width="wrap_content"android:layout_height="wrap_content"android:entries="array/test">android:id="+id/save"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_gravity="center"android:text="保存"/>主Activity文件MainActivity.java:importandroid.support.v7.app.ActionBarActivity;importandroid.app.AlertDialog;importandroid.app.Application;importandroid.content.DialogInterface;importandroid.content.Intent;importandroid.database.Cursor;importandroid.os.Bundle;importandroid.view.Menu;importandroid.view.MenuItem;importandroid.view.View;importandroid.widget.AdapterView;importandroid.widget.ListView;importandroid.widget.SimpleCursorAdapter;publicclassMainActivityextendsActionBarActivity{privateListViewlistView;protectedvoidonCreate(BundlesavedInstanceState){super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);listView=(ListView)findViewById(R.id.listView);//getRelationFromDB();}privatevoidgetRelationFromDB(){finalDatabaseHelperdbHelper=newDatabaseHelper(this);Cursorcursor=dbHelper.query();String[]from={"_id","name","tel","groupName"};int[]to={R.id._id,R.id.name,R.id.tel,R.id.group};SimpleCursorAdapterscadapter=newSimpleCursorAdapter(this,R.layout.relationlist,cursor,from,to);listView.setAdapter(scadapter);listView.setOnItemClickListener(newAdapterView.OnItemClickListener(){publicvoidonItemClick(AdapterView>adapter,Viewview,intposition,longid){finallongtemp=id;AlertDialog.BuilderadBuilder=newAlertDialog.Builder(MainActivity.this);adBuilder.setMessage("确认要删除记录吗?").setPositiveButton("确认",newDialogInterface.OnClickListener(){OverridepublicvoidonClick(DialogInterfacedialog,intwhich){//TODOAuto-generatedmethodstubdbHelper.del((int)temp);Cursorcursor=dbHelper.query();String[]from={"_id","name","tel","groupName"};int[]to={R.id._id,R.id.name,R.id.tel,R.id.group};SimpleCursorAdapterscadapter=newSimpleCursorAdapter(getApplicationContext(),R.layout.relationlist,cursor,from,to);MainActivity.this.listView.setAdapter(scadapter);}}).setNegativeButton("取消",newDialogInterface.OnClickListener(){OverridepublicvoidonClick(DialogInterfacedialog,intwhich){}});AlertDialogaleraDialog=adBuilder.create();aleraDialog.show();}});dbHelper.close();}publicvoidadd(Viewview){Intentintent=newIntent(MainActivity.this,AddrelationActivity.class);startActivityForResult(intent,0x111);}protectedvoidonActivityResult(intrequestCode,intresultCode,Intentdata){super.onActivityResult(requestCode,resultCode,data);if(requestCode==0x111&&resultCode==0x111){getRelationFromDB();}}OverridepublicbooleanonCreateOptionsMenu(Menumenu){//Inflatethemenu;thisaddsitemstotheactionbarifitispresent.getMenuInflater().inflate(R.menu.main,menu);returntrue;}OverridepublicbooleanonOptionsItemSelected(MenuItemitem){//Handleactionbaritemclickshere.Theactionbarwill//automaticallyhandleclicksontheHome/Upbutton,solong//asyouspecifyaparentactivityinAndroidManifest.xml.intid=item.getItemId();if(id==R.id.action_settings){returntrue;}returnsuper.onOptionsItemSelected(item);}}添加联系人AddrelationActivity.javaimportandroid.app.Activity;importandroid.app.AlertDialog;importandroid.content.ContentValues;importandroid.content.DialogInterface;importandroid.content.Intent;importandroid.os.Bundle;importandroid.view.View;importandroid.widget.EditText;importandroid.widget.Spinner;publicclassAddrelationActivityextendsActivity{privateEditTextaddName,addTel;privateSpinneraddGroup;protectedvoidonCreate(BundlesavedInstanceState){super.onCreate(savedInstanceState);setContentView(R.layout.addrelation);addName=(EditText)findViewById(R.id.addName);addTel=(EditText)findViewById(R.id.addTel);addGroup=(Spinner)findViewById(R.id.addGroup);}publicvoidsave(Viewview){finalContentValuesvalues=newContentValues();values.put("name",addName.getText().toString());values.put("tel",addTel.getText().toString());values.put("groupName",addGroup.getSelectedItem().toString());finalDatabaseHelperdbHelper=newDatabaseHelper(getApplicationContext());finalAlertDialog.BuilderadBuilder=newAlertDialog.Builder(this);adBuilder.setMessage("确认保存记录吗?").setPositiveButton("确认",newDialogInterface.OnClickListener(){OverridepublicvoidonClick(DialogInterfacedialog,intwhich){//TODOAuto-generatedmethodstubdbHelper.insert(values);Intentintent=getIntent();setResult(0x111,intent);AddrelationActivity.this.finish();}}).setNegativeButton("取消",newDialogInterface.OnClickListener(){publicvoidonClick(DialogInterfacedialog,intwhich){}});AlertDialogaleraDialog=adBuilder.create();aleraDialog.show();}}
android:
id="+id/title"
layout_width="wrap_content"
layout_height="wrap_content"
text="通信录"/>
android:id="+id/listView"android:layout_width="wrap_content"android:layout_height="wrap_content">
id="+id/listView"
layout_height="wrap_content">
android:layout_width="wrap_content"android:layout_height="wrap_content"android:onClick="add"/>添加联系人布局文件addrelation.xml:xmlversion="1.0"encoding="utf-8"?>android="schemas.android./apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical">android:layout_width="wrap_content"android:layout_height="wrap_content"android:text=""/>android:id="+id/addName"android:layout_width="wrap_content"android:layout_height="wrap_content"/>android:layout_width="wrap_content"android:layout_height="wrap_content"android:text=""/>android:id="+id/addTel"android:layout_width="wrap_content"android:layout_height="wrap_content"/>android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="所属组"/>android:id="+id/addGroup"android:layout_width="wrap_content"android:layout_height="wrap_content"android:entries="array/test">android:id="+id/save"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_gravity="center"android:text="保存"/>主Activity文件MainActivity.java:importandroid.support.v7.app.ActionBarActivity;importandroid.app.AlertDialog;importandroid.app.Application;importandroid.content.DialogInterface;importandroid.content.Intent;importandroid.database.Cursor;importandroid.os.Bundle;importandroid.view.Menu;importandroid.view.MenuItem;importandroid.view.View;importandroid.widget.AdapterView;importandroid.widget.ListView;importandroid.widget.SimpleCursorAdapter;publicclassMainActivityextendsActionBarActivity{privateListViewlistView;protectedvoidonCreate(BundlesavedInstanceState){super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);listView=(ListView)findViewById(R.id.listView);//getRelationFromDB();}privatevoidgetRelationFromDB(){finalDatabaseHelperdbHelper=newDatabaseHelper(this);Cursorcursor=dbHelper.query();String[]from={"_id","name","tel","groupName"};int[]to={R.id._id,R.id.name,R.id.tel,R.id.group};SimpleCursorAdapterscadapter=newSimpleCursorAdapter(this,R.layout.relationlist,cursor,from,to);listView.setAdapter(scadapter);listView.setOnItemClickListener(newAdapterView.OnItemClickListener(){publicvoidonItemClick(AdapterView>adapter,Viewview,intposition,longid){finallongtemp=id;AlertDialog.BuilderadBuilder=newAlertDialog.Builder(MainActivity.this);adBuilder.setMessage("确认要删除记录吗?").setPositiveButton("确认",newDialogInterface.OnClickListener(){OverridepublicvoidonClick(DialogInterfacedialog,intwhich){//TODOAuto-generatedmethodstubdbHelper.del((int)temp);Cursorcursor=dbHelper.query();String[]from={"_id","name","tel","groupName"};int[]to={R.id._id,R.id.name,R.id.tel,R.id.group};SimpleCursorAdapterscadapter=newSimpleCursorAdapter(getApplicationContext(),R.layout.relationlist,cursor,from,to);MainActivity.this.listView.setAdapter(scadapter);}}).setNegativeButton("取消",newDialogInterface.OnClickListener(){OverridepublicvoidonClick(DialogInterfacedialog,intwhich){}});AlertDialogaleraDialog=adBuilder.create();aleraDialog.show();}});dbHelper.close();}publicvoidadd(Viewview){Intentintent=newIntent(MainActivity.this,AddrelationActivity.class);startActivityForResult(intent,0x111);}protectedvoidonActivityResult(intrequestCode,intresultCode,Intentdata){super.onActivityResult(requestCode,resultCode,data);if(requestCode==0x111&&resultCode==0x111){getRelationFromDB();}}OverridepublicbooleanonCreateOptionsMenu(Menumenu){//Inflatethemenu;thisaddsitemstotheactionbarifitispresent.getMenuInflater().inflate(R.menu.main,menu);returntrue;}OverridepublicbooleanonOptionsItemSelected(MenuItemitem){//Handleactionbaritemclickshere.Theactionbarwill//automaticallyhandleclicksontheHome/Upbutton,solong//asyouspecifyaparentactivityinAndroidManifest.xml.intid=item.getItemId();if(id==R.id.action_settings){returntrue;}returnsuper.onOptionsItemSelected(item);}}添加联系人AddrelationActivity.javaimportandroid.app.Activity;importandroid.app.AlertDialog;importandroid.content.ContentValues;importandroid.content.DialogInterface;importandroid.content.Intent;importandroid.os.Bundle;importandroid.view.View;importandroid.widget.EditText;importandroid.widget.Spinner;publicclassAddrelationActivityextendsActivity{privateEditTextaddName,addTel;privateSpinneraddGroup;protectedvoidonCreate(BundlesavedInstanceState){super.onCreate(savedInstanceState);setContentView(R.layout.addrelation);addName=(EditText)findViewById(R.id.addName);addTel=(EditText)findViewById(R.id.addTel);addGroup=(Spinner)findViewById(R.id.addGroup);}publicvoidsave(Viewview){finalContentValuesvalues=newContentValues();values.put("name",addName.getText().toString());values.put("tel",addTel.getText().toString());values.put("groupName",addGroup.getSelectedItem().toString());finalDatabaseHelperdbHelper=newDatabaseHelper(getApplicationContext());finalAlertDialog.BuilderadBuilder=newAlertDialog.Builder(this);adBuilder.setMessage("确认保存记录吗?").setPositiveButton("确认",newDialogInterface.OnClickListener(){OverridepublicvoidonClick(DialogInterfacedialog,intwhich){//TODOAuto-generatedmethodstubdbHelper.insert(values);Intentintent=getIntent();setResult(0x111,intent);AddrelationActivity.this.finish();}}).setNegativeButton("取消",newDialogInterface.OnClickListener(){publicvoidonClick(DialogInterfacedialog,intwhich){}});AlertDialogaleraDialog=adBuilder.create();aleraDialog.show();}}
onClick="add"/>
添加联系人布局文件addrelation.xml:
xmlversion="1.0"encoding="utf-8"?
>
android="schemas.android./apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical">android:layout_width="wrap_content"android:layout_height="wrap_content"android:text=""/>android:id="+id/addName"android:layout_width="wrap_content"android:layout_height="wrap_content"/>android:layout_width="wrap_content"android:layout_height="wrap_content"android:text=""/>android:id="+id/addTel"android:layout_width="wrap_content"android:layout_height="wrap_content"/>android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="所属组"/>android:id="+id/addGroup"android:layout_width="wrap_content"android:layout_height="wrap_content"android:entries="array/test">android:id="+id/save"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_gravity="center"android:text="保存"/>主Activity文件MainActivity.java:importandroid.support.v7.app.ActionBarActivity;importandroid.app.AlertDialog;importandroid.app.Application;importandroid.content.DialogInterface;importandroid.content.Intent;importandroid.database.Cursor;importandroid.os.Bundle;importandroid.view.Menu;importandroid.view.MenuItem;importandroid.view.View;importandroid.widget.AdapterView;importandroid.widget.ListView;importandroid.widget.SimpleCursorAdapter;publicclassMainActivityextendsActionBarActivity{privateListViewlistView;protectedvoidonCreate(BundlesavedInstanceState){super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);listView=(ListView)findViewById(R.id.listView);//getRelationFromDB();}privatevoidgetRelationFromDB(){finalDatabaseHelperdbHelper=newDatabaseHelper(this);Cursorcursor=dbHelper.query();String[]from={"_id","name","tel","groupName"};int[]to={R.id._id,R.id.name,R.id.tel,R.id.group};SimpleCursorAdapterscadapter=newSimpleCursorAdapter(this,R.layout.relationlist,cursor,from,to);listView.setAdapter(scadapter);listView.setOnItemClickListener(newAdapterView.OnItemClickListener(){publicvoidonItemClick(AdapterView>adapter,Viewview,intposition,longid){finallongtemp=id;AlertDialog.BuilderadBuilder=newAlertDialog.Builder(MainActivity.this);adBuilder.setMessage("确认要删除记录吗?").setPositiveButton("确认",newDialogInterface.OnClickListener(){OverridepublicvoidonClick(DialogInterfacedialog,intwhich){//TODOAuto-generatedmethodstubdbHelper.del((int)temp);Cursorcursor=dbHelper.query();String[]from={"_id","name","tel","groupName"};int[]to={R.id._id,R.id.name,R.id.tel,R.id.group};SimpleCursorAdapterscadapter=newSimpleCursorAdapter(getApplicationContext(),R.layout.relationlist,cursor,from,to);MainActivity.this.listView.setAdapter(scadapter);}}).setNegativeButton("取消",newDialogInterface.OnClickListener(){OverridepublicvoidonClick(DialogInterfacedialog,intwhich){}});AlertDialogaleraDialog=adBuilder.create();aleraDialog.show();}});dbHelper.close();}publicvoidadd(Viewview){Intentintent=newIntent(MainActivity.this,AddrelationActivity.class);startActivityForResult(intent,0x111);}protectedvoidonActivityResult(intrequestCode,intresultCode,Intentdata){super.onActivityResult(requestCode,resultCode,data);if(requestCode==0x111&&resultCode==0x111){getRelationFromDB();}}OverridepublicbooleanonCreateOptionsMenu(Menumenu){//Inflatethemenu;thisaddsitemstotheactionbarifitispresent.getMenuInflater().inflate(R.menu.main,menu);returntrue;}OverridepublicbooleanonOptionsItemSelected(MenuItemitem){//Handleactionbaritemclickshere.Theactionbarwill//automaticallyhandleclicksontheHome/Upbutton,solong//asyouspecifyaparentactivityinAndroidManifest.xml.intid=item.getItemId();if(id==R.id.action_settings){returntrue;}returnsuper.onOptionsItemSelected(item);}}添加联系人AddrelationActivity.javaimportandroid.app.Activity;importandroid.app.AlertDialog;importandroid.content.ContentValues;importandroid.content.DialogInterface;importandroid.content.Intent;importandroid.os.Bundle;importandroid.view.View;importandroid.widget.EditText;importandroid.widget.Spinner;publicclassAddrelationActivityextendsActivity{privateEditTextaddName,addTel;privateSpinneraddGroup;protectedvoidonCreate(BundlesavedInstanceState){super.onCreate(savedInstanceState);setContentView(R.layout.addrelation);addName=(EditText)findViewById(R.id.addName);addTel=(EditText)findViewById(R.id.addTel);addGroup=(Spinner)findViewById(R.id.addGroup);}publicvoidsave(Viewview){finalContentValuesvalues=newContentValues();values.put("name",addName.getText().toString());values.put("tel",addTel.getText().toString());values.put("groupName",addGroup.getSelectedItem().toString());finalDatabaseHelperdbHelper=newDatabaseHelper(getApplicationContext());finalAlertDialog.BuilderadBuilder=newAlertDialog.Builder(this);adBuilder.setMessage("确认保存记录吗?").setPositiveButton("确认",newDialogInterface.OnClickListener(){OverridepublicvoidonClick(DialogInterfacedialog,intwhich){//TODOAuto-generatedmethodstubdbHelper.insert(values);Intentintent=getIntent();setResult(0x111,intent);AddrelationActivity.this.finish();}}).setNegativeButton("取消",newDialogInterface.OnClickListener(){publicvoidonClick(DialogInterfacedialog,intwhich){}});AlertDialogaleraDialog=adBuilder.create();aleraDialog.show();}}
layout_width="match_parent"
layout_height="match_parent"
orientation="vertical">
android:layout_width="wrap_content"android:layout_height="wrap_content"android:text=""/>android:id="+id/addName"android:layout_width="wrap_content"android:layout_height="wrap_content"/>android:layout_width="wrap_content"android:layout_height="wrap_content"android:text=""/>android:id="+id/addTel"android:layout_width="wrap_content"android:layout_height="wrap_content"/>android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="所属组"/>android:id="+id/addGroup"android:layout_width="wrap_content"android:layout_height="wrap_content"android:entries="array/test">android:id="+id/save"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_gravity="center"android:text="保存"/>主Activity文件MainActivity.java:importandroid.support.v7.app.ActionBarActivity;importandroid.app.AlertDialog;importandroid.app.Application;importandroid.content.DialogInterface;importandroid.content.Intent;importandroid.database.Cursor;importandroid.os.Bundle;importandroid.view.Menu;importandroid.view.MenuItem;importandroid.view.View;importandroid.widget.AdapterView;importandroid.widget.ListView;importandroid.widget.SimpleCursorAdapter;publicclassMainActivityextendsActionBarActivity{privateListViewlistView;protectedvoidonCreate(BundlesavedInstanceState){super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);listView=(ListView)findViewById(R.id.listView);//getRelationFromDB();}privatevoidgetRelationFromDB(){finalDatabaseHelperdbHelper=newDatabaseHelper(this);Cursorcursor=dbHelper.query();String[]from={"_id","name","tel","groupName"};int[]to={R.id._id,R.id.name,R.id.tel,R.id.group};SimpleCursorAdapterscadapter=newSimpleCursorAdapter(this,R.layout.relationlist,cursor,from,to);listView.setAdapter(scadapter);listView.setOnItemClickListener(newAdapterView.OnItemClickListener(){publicvoidonItemClick(AdapterView>adapter,Viewview,intposition,longid){finallongtemp=id;AlertDialog.BuilderadBuilder=newAlertDialog.Builder(MainActivity.this);adBuilder.setMessage("确认要删除记录吗?").setPositiveButton("确认",newDialogInterface.OnClickListener(){OverridepublicvoidonClick(DialogInterfacedialog,intwhich){//TODOAuto-generatedmethodstubdbHelper.del((int)temp);Cursorcursor=dbHelper.query();String[]from={"_id","name","tel","groupName"};int[]to={R.id._id,R.id.name,R.id.tel,R.id.group};SimpleCursorAdapterscadapter=newSimpleCursorAdapter(getApplicationContext(),R.layout.relationlist,cursor,from,to);MainActivity.this.listView.setAdapter(scadapter);}}).setNegativeButton("取消",newDialogInterface.OnClickListener(){OverridepublicvoidonClick(DialogInterfacedialog,intwhich){}});AlertDialogaleraDialog=adBuilder.create();aleraDialog.show();}});dbHelper.close();}publicvoidadd(Viewview){Intentintent=newIntent(MainActivity.this,AddrelationActivity.class);startActivityForResult(intent,0x111);}protectedvoidonActivityResult(intrequestCode,intresultCode,Intentdata){super.onActivityResult(requestCode,resultCode,data);if(requestCode==0x111&&resultCode==0x111){getRelationFromDB();}}OverridepublicbooleanonCreateOptionsMenu(Menumenu){//Inflatethemenu;thisaddsitemstotheactionbarifitispresent.getMenuInflater().inflate(R.menu.main,menu);returntrue;}OverridepublicbooleanonOptionsItemSelected(MenuItemitem){//Handleactionbaritemclickshere.Theactionbarwill//automaticallyhandleclicksontheHome/Upbutton,solong//asyouspecifyaparentactivityinAndroidManifest.xml.intid=item.getItemId();if(id==R.id.action_settings){returntrue;}returnsuper.onOptionsItemSelected(item);}}添加联系人AddrelationActivity.javaimportandroid.app.Activity;importandroid.app.AlertDialog;importandroid.content.ContentValues;importandroid.content.DialogInterface;importandroid.content.Intent;importandroid.os.Bundle;importandroid.view.View;importandroid.widget.EditText;importandroid.widget.Spinner;publicclassAddrelationActivityextendsActivity{privateEditTextaddName,addTel;privateSpinneraddGroup;protectedvoidonCreate(BundlesavedInstanceState){super.onCreate(savedInstanceState);setContentView(R.layout.addrelation);addName=(EditText)findViewById(R.id.addName);addTel=(EditText)findViewById(R.id.addTel);addGroup=(Spinner)findViewById(R.id.addGroup);}publicvoidsave(Viewview){finalContentValuesvalues=newContentValues();values.put("name",addName.getText().toString());values.put("tel",addTel.getText().toString());values.put("groupName",addGroup.getSelectedItem().toString());finalDatabaseHelperdbHelper=newDatabaseHelper(getApplicationContext());finalAlertDialog.BuilderadBuilder=newAlertDialog.Builder(this);adBuilder.setMessage("确认保存记录吗?").setPositiveButton("确认",newDialogInterface.OnClickListener(){OverridepublicvoidonClick(DialogInterfacedialog,intwhich){//TODOAuto-generatedmethodstubdbHelper.insert(values);Intentintent=getIntent();setResult(0x111,intent);AddrelationActivity.this.finish();}}).setNegativeButton("取消",newDialogInterface.OnClickListener(){publicvoidonClick(DialogInterfacedialog,intwhich){}});AlertDialogaleraDialog=adBuilder.create();aleraDialog.show();}}
text=""/>
android:id="+id/addName"android:layout_width="wrap_content"android:layout_height="wrap_content"/>android:layout_width="wrap_content"android:layout_height="wrap_content"android:text=""/>android:id="+id/addTel"android:layout_width="wrap_content"android:layout_height="wrap_content"/>android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="所属组"/>android:id="+id/addGroup"android:layout_width="wrap_content"android:layout_height="wrap_content"android:entries="array/test">android:id="+id/save"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_gravity="center"android:text="保存"/>主Activity文件MainActivity.java:importandroid.support.v7.app.ActionBarActivity;importandroid.app.AlertDialog;importandroid.app.Application;importandroid.content.DialogInterface;importandroid.content.Intent;importandroid.database.Cursor;importandroid.os.Bundle;importandroid.view.Menu;importandroid.view.MenuItem;importandroid.view.View;importandroid.widget.AdapterView;importandroid.widget.ListView;importandroid.widget.SimpleCursorAdapter;publicclassMainActivityextendsActionBarActivity{privateListViewlistView;protectedvoidonCreate(BundlesavedInstanceState){super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);listView=(ListView)findViewById(R.id.listView);//getRelationFromDB();}privatevoidgetRelationFromDB(){finalDatabaseHelperdbHelper=newDatabaseHelper(this);Cursorcursor=dbHelper.query();String[]from={"_id","name","tel","groupName"};int[]to={R.id._id,R.id.name,R.id.tel,R.id.group};SimpleCursorAdapterscadapter=newSimpleCursorAdapter(this,R.layout.relationlist,cursor,from,to);listView.setAdapter(scadapter);listView.setOnItemClickListener(newAdapterView.OnItemClickListener(){publicvoidonItemClick(AdapterView>adapter,Viewview,intposition,longid){finallongtemp=id;AlertDialog.BuilderadBuilder=newAlertDialog.Builder(MainActivity.this);adBuilder.setMessage("确认要删除记录吗?").setPositiveButton("确认",newDialogInterface.OnClickListener(){OverridepublicvoidonClick(DialogInterfacedialog,intwhich){//TODOAuto-generatedmethodstubdbHelper.del((int)temp);Cursorcursor=dbHelper.query();String[]from={"_id","name","tel","groupName"};int[]to={R.id._id,R.id.name,R.id.tel,R.id.group};SimpleCursorAdapterscadapter=newSimpleCursorAdapter(getApplicationContext(),R.layout.relationlist,cursor,from,to);MainActivity.this.listView.setAdapter(scadapter);}}).setNegativeButton("取消",newDialogInterface.OnClickListener(){OverridepublicvoidonClick(DialogInterfacedialog,intwhich){}});AlertDialogaleraDialog=adBuilder.create();aleraDialog.show();}});dbHelper.close();}publicvoidadd(Viewview){Intentintent=newIntent(MainActivity.this,AddrelationActivity.class);startActivityForResult(intent,0x111);}protectedvoidonActivityResult(intrequestCode,intresultCode,Intentdata){super.onActivityResult(requestCode,resultCode,data);if(requestCode==0x111&&resultCode==0x111){getRelationFromDB();}}OverridepublicbooleanonCreateOptionsMenu(Menumenu){//Inflatethemenu;thisaddsitemstotheactionbarifitispresent.getMenuInflater().inflate(R.menu.main,menu);returntrue;}OverridepublicbooleanonOptionsItemSelected(MenuItemitem){//Handleactionbaritemclickshere.Theactionbarwill//automaticallyhandleclicksontheHome/Upbutton,solong//asyouspecifyaparentactivityinAndroidManifest.xml.intid=item.getItemId();if(id==R.id.action_settings){returntrue;}returnsuper.onOptionsItemSelected(item);}}添加联系人AddrelationActivity.javaimportandroid.app.Activity;importandroid.app.AlertDialog;importandroid.content.ContentValues;importandroid.content.DialogInterface;importandroid.content.Intent;importandroid.os.Bundle;importandroid.view.View;importandroid.widget.EditText;importandroid.widget.Spinner;publicclassAddrelationActivityextendsActivity{privateEditTextaddName,addTel;privateSpinneraddGroup;protectedvoidonCreate(BundlesavedInstanceState){super.onCreate(savedInstanceState);setContentView(R.layout.addrelation);addName=(EditText)findViewById(R.id.addName);addTel=(EditText)findViewById(R.id.addTel);addGroup=(Spinner)findViewById(R.id.addGroup);}publicvoidsave(Viewview){finalContentValuesvalues=newContentValues();values.put("name",addName.getText().toString());values.put("tel",addTel.getText().toString());values.put("groupName",addGroup.getSelectedItem().toString());finalDatabaseHelperdbHelper=newDatabaseHelper(getApplicationContext());finalAlertDialog.BuilderadBuilder=newAlertDialog.Builder(this);adBuilder.setMessage("确认保存记录吗?").setPositiveButton("确认",newDialogInterface.OnClickListener(){OverridepublicvoidonClick(DialogInterfacedialog,intwhich){//TODOAuto-generatedmethodstubdbHelper.insert(values);Intentintent=getIntent();setResult(0x111,intent);AddrelationActivity.this.finish();}}).setNegativeButton("取消",newDialogInterface.OnClickListener(){publicvoidonClick(DialogInterfacedialog,intwhich){}});AlertDialogaleraDialog=adBuilder.create();aleraDialog.show();}}
id="+id/addName"
layout_height="wrap_content"/>
android:layout_width="wrap_content"android:layout_height="wrap_content"android:text=""/>android:id="+id/addTel"android:layout_width="wrap_content"android:layout_height="wrap_content"/>android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="所属组"/>android:id="+id/addGroup"android:layout_width="wrap_content"android:layout_height="wrap_content"android:entries="array/test">android:id="+id/save"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_gravity="center"android:text="保存"/>主Activity文件MainActivity.java:importandroid.support.v7.app.ActionBarActivity;importandroid.app.AlertDialog;importandroid.app.Application;importandroid.content.DialogInterface;importandroid.content.Intent;importandroid.database.Cursor;importandroid.os.Bundle;importandroid.view.Menu;importandroid.view.MenuItem;importandroid.view.View;importandroid.widget.AdapterView;importandroid.widget.ListView;importandroid.widget.SimpleCursorAdapter;publicclassMainActivityextendsActionBarActivity{privateListViewlistView;protectedvoidonCreate(BundlesavedInstanceState){super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);listView=(ListView)findViewById(R.id.listView);//getRelationFromDB();}privatevoidgetRelationFromDB(){finalDatabaseHelperdbHelper=newDatabaseHelper(this);Cursorcursor=dbHelper.query();String[]from={"_id","name","tel","groupName"};int[]to={R.id._id,R.id.name,R.id.tel,R.id.group};SimpleCursorAdapterscadapter=newSimpleCursorAdapter(this,R.layout.relationlist,cursor,from,to);listView.setAdapter(scadapter);listView.setOnItemClickListener(newAdapterView.OnItemClickListener(){publicvoidonItemClick(AdapterView>adapter,Viewview,intposition,longid){finallongtemp=id;AlertDialog.BuilderadBuilder=newAlertDialog.Builder(MainActivity.this);adBuilder.setMessage("确认要删除记录吗?").setPositiveButton("确认",newDialogInterface.OnClickListener(){OverridepublicvoidonClick(DialogInterfacedialog,intwhich){//TODOAuto-generatedmethodstubdbHelper.del((int)temp);Cursorcursor=dbHelper.query();String[]from={"_id","name","tel","groupName"};int[]to={R.id._id,R.id.name,R.id.tel,R.id.group};SimpleCursorAdapterscadapter=newSimpleCursorAdapter(getApplicationContext(),R.layout.relationlist,cursor,from,to);MainActivity.this.listView.setAdapter(scadapter);}}).setNegativeButton("取消",newDialogInterface.OnClickListener(){OverridepublicvoidonClick(DialogInterfacedialog,intwhich){}});AlertDialogaleraDialog=adBuilder.create();aleraDialog.show();}});dbHelper.close();}publicvoidadd(Viewview){Intentintent=newIntent(MainActivity.this,AddrelationActivity.class);startActivityForResult(intent,0x111);}protectedvoidonActivityResult(intrequestCode,intresultCode,Intentdata){super.onActivityResult(requestCode,resultCode,data);if(requestCode==0x111&&resultCode==0x111){getRelationFromDB();}}OverridepublicbooleanonCreateOptionsMenu(Menumenu){//Inflatethemenu;thisaddsitemstotheactionbarifitispresent.getMenuInflater().inflate(R.menu.main,menu);returntrue;}OverridepublicbooleanonOptionsItemSelected(MenuItemitem){//Handleactionbaritemclickshere.Theactionbarwill//automaticallyhandleclicksontheHome/Upbutton,solong//asyouspecifyaparentactivityinAndroidManifest.xml.intid=item.getItemId();if(id==R.id.action_settings){returntrue;}returnsuper.onOptionsItemSelected(item);}}添加联系人AddrelationActivity.javaimportandroid.app.Activity;importandroid.app.AlertDialog;importandroid.content.ContentValues;importandroid.content.DialogInterface;importandroid.content.Intent;importandroid.os.Bundle;importandroid.view.View;importandroid.widget.EditText;importandroid.widget.Spinner;publicclassAddrelationActivityextendsActivity{privateEditTextaddName,addTel;privateSpinneraddGroup;protectedvoidonCreate(BundlesavedInstanceState){super.onCreate(savedInstanceState);setContentView(R.layout.addrelation);addName=(EditText)findViewById(R.id.addName);addTel=(EditText)findViewById(R.id.addTel);addGroup=(Spinner)findViewById(R.id.addGroup);}publicvoidsave(Viewview){finalContentValuesvalues=newContentValues();values.put("name",addName.getText().toString());values.put("tel",addTel.getText().toString());values.put("groupName",addGroup.getSelectedItem().toString());finalDatabaseHelperdbHelper=newDatabaseHelper(getApplicationContext());finalAlertDialog.BuilderadBuilder=newAlertDialog.Builder(this);adBuilder.setMessage("确认保存记录吗?").setPositiveButton("确认",newDialogInterface.OnClickListener(){OverridepublicvoidonClick(DialogInterfacedialog,intwhich){//TODOAuto-generatedmethodstubdbHelper.insert(values);Intentintent=getIntent();setResult(0x111,intent);AddrelationActivity.this.finish();}}).setNegativeButton("取消",newDialogInterface.OnClickListener(){publicvoidonClick(DialogInterfacedialog,intwhich){}});AlertDialogaleraDialog=adBuilder.create();aleraDialog.show();}}
android:id="+id/addTel"android:layout_width="wrap_content"android:layout_height="wrap_content"/>android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="所属组"/>android:id="+id/addGroup"android:layout_width="wrap_content"android:layout_height="wrap_content"android:entries="array/test">android:id="+id/save"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_gravity="center"android:text="保存"/>主Activity文件MainActivity.java:importandroid.support.v7.app.ActionBarActivity;importandroid.app.AlertDialog;importandroid.app.Application;importandroid.content.DialogInterface;importandroid.content.Intent;importandroid.database.Cursor;importandroid.os.Bundle;importandroid.view.Menu;importandroid.view.MenuItem;importandroid.view.View;importandroid.widget.AdapterView;importandroid.widget.ListView;importandroid.widget.SimpleCursorAdapter;publicclassMainActivityextendsActionBarActivity{privateListViewlistView;protectedvoidonCreate(BundlesavedInstanceState){super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);listView=(ListView)findViewById(R.id.listView);//getRelationFromDB();}privatevoidgetRelationFromDB(){finalDatabaseHelperdbHelper=newDatabaseHelper(this);Cursorcursor=dbHelper.query();String[]from={"_id","name","tel","groupName"};int[]to={R.id._id,R.id.name,R.id.tel,R.id.group};SimpleCursorAdapterscadapter=newSimpleCursorAdapter(this,R.layout.relationlist,cursor,from,to);listView.setAdapter(scadapter);listView.setOnItemClickListener(newAdapterView.OnItemClickListener(){publicvoidonItemClick(AdapterView>adapter,Viewview,intposition,longid){finallongtemp=id;AlertDialog.BuilderadBuilder=newAlertDialog.Builder(MainActivity.this);adBuilder.setMessage("确认要删除记录吗?").setPositiveButton("确认",newDialogInterface.OnClickListener(){OverridepublicvoidonClick(DialogInterfacedialog,intwhich){//TODOAuto-generatedmethodstubdbHelper.del((int)temp);Cursorcursor=dbHelper.query();String[]from={"_id","name","tel","groupName"};int[]to={R.id._id,R.id.name,R.id.tel,R.id.group};SimpleCursorAdapterscadapter=newSimpleCursorAdapter(getApplicationContext(),R.layout.relationlist,cursor,from,to);MainActivity.this.listView.setAdapter(scadapter);}}).setNegativeButton("取消",newDialogInterface.OnClickListener(){OverridepublicvoidonClick(DialogInterfacedialog,intwhich){}});AlertDialogaleraDialog=adBuilder.create();aleraDialog.show();}});dbHelper.close();}publicvoidadd(Viewview){Intentintent=newIntent(MainActivity.this,AddrelationActivity.class);startActivityForResult(intent,0x111);}protectedvoidonActivityResult(intrequestCode,intresultCode,Intentdata){super.onActivityResult(requestCode,resultCode,data);if(requestCode==0x111&&resultCode==0x111){getRelationFromDB();}}OverridepublicbooleanonCreateOptionsMenu(Menumenu){//Inflatethemenu;thisaddsitemstotheactionbarifitispresent.getMenuInflater().inflate(R.menu.main,menu);returntrue;}OverridepublicbooleanonOptionsItemSelected(MenuItemitem){//Handleactionbaritemclickshere.Theactionbarwill//automaticallyhandleclicksontheHome/Upbutton,solong//asyouspecifyaparentactivityinAndroidManifest.xml.intid=item.getItemId();if(id==R.id.action_settings){returntrue;}returnsuper.onOptionsItemSelected(item);}}添加联系人AddrelationActivity.javaimportandroid.app.Activity;importandroid.app.AlertDialog;importandroid.content.ContentValues;importandroid.content.DialogInterface;importandroid.content.Intent;importandroid.os.Bundle;importandroid.view.View;importandroid.widget.EditText;importandroid.widget.Spinner;publicclassAddrelationActivityextendsActivity{privateEditTextaddName,addTel;privateSpinneraddGroup;protectedvoidonCreate(BundlesavedInstanceState){super.onCreate(savedInstanceState);setContentView(R.layout.addrelation);addName=(EditText)findViewById(R.id.addName);addTel=(EditText)findViewById(R.id.addTel);addGroup=(Spinner)findViewById(R.id.addGroup);}publicvoidsave(Viewview){finalContentValuesvalues=newContentValues();values.put("name",addName.getText().toString());values.put("tel",addTel.getText().toString());values.put("groupName",addGroup.getSelectedItem().toString());finalDatabaseHelperdbHelper=newDatabaseHelper(getApplicationContext());finalAlertDialog.BuilderadBuilder=newAlertDialog.Builder(this);adBuilder.setMessage("确认保存记录吗?").setPositiveButton("确认",newDialogInterface.OnClickListener(){OverridepublicvoidonClick(DialogInterfacedialog,intwhich){//TODOAuto-generatedmethodstubdbHelper.insert(values);Intentintent=getIntent();setResult(0x111,intent);AddrelationActivity.this.finish();}}).setNegativeButton("取消",newDialogInterface.OnClickListener(){publicvoidonClick(DialogInterfacedialog,intwhich){}});AlertDialogaleraDialog=adBuilder.create();aleraDialog.show();}}
id="+id/addTel"
android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="所属组"/>android:id="+id/addGroup"android:layout_width="wrap_content"android:layout_height="wrap_content"android:entries="array/test">android:id="+id/save"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_gravity="center"android:text="保存"/>主Activity文件MainActivity.java:importandroid.support.v7.app.ActionBarActivity;importandroid.app.AlertDialog;importandroid.app.Application;importandroid.content.DialogInterface;importandroid.content.Intent;importandroid.database.Cursor;importandroid.os.Bundle;importandroid.view.Menu;importandroid.view.MenuItem;importandroid.view.View;importandroid.widget.AdapterView;importandroid.widget.ListView;importandroid.widget.SimpleCursorAdapter;publicclassMainActivityextendsActionBarActivity{privateListViewlistView;protectedvoidonCreate(BundlesavedInstanceState){super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);listView=(ListView)findViewById(R.id.listView);//getRelationFromDB();}privatevoidgetRelationFromDB(){finalDatabaseHelperdbHelper=newDatabaseHelper(this);Cursorcursor=dbHelper.query();String[]from={"_id","name","tel","groupName"};int[]to={R.id._id,R.id.name,R.id.tel,R.id.group};SimpleCursorAdapterscadapter=newSimpleCursorAdapter(this,R.layout.relationlist,cursor,from,to);listView.setAdapter(scadapter);listView.setOnItemClickListener(newAdapterView.OnItemClickListener(){publicvoidonItemClick(AdapterView>adapter,Viewview,intposition,longid){finallongtemp=id;AlertDialog.BuilderadBuilder=newAlertDialog.Builder(MainActivity.this);adBuilder.setMessage("确认要删除记录吗?").setPositiveButton("确认",newDialogInterface.OnClickListener(){OverridepublicvoidonClick(DialogInterfacedialog,intwhich){//TODOAuto-generatedmethodstubdbHelper.del((int)temp);Cursorcursor=dbHelper.query();String[]from={"_id","name","tel","groupName"};int[]to={R.id._id,R.id.name,R.id.tel,R.id.group};SimpleCursorAdapterscadapter=newSimpleCursorAdapter(getApplicationContext(),R.layout.relationlist,cursor,from,to);MainActivity.this.listView.setAdapter(scadapter);}}).setNegativeButton("取消",newDialogInterface.OnClickListener(){OverridepublicvoidonClick(DialogInterfacedialog,intwhich){}});AlertDialogaleraDialog=adBuilder.create();aleraDialog.show();}});dbHelper.close();}publicvoidadd(Viewview){Intentintent=newIntent(MainActivity.this,AddrelationActivity.class);startActivityForResult(intent,0x111);}protectedvoidonActivityResult(intrequestCode,intresultCode,Intentdata){super.onActivityResult(requestCode,resultCode,data);if(requestCode==0x111&&resultCode==0x111){getRelationFromDB();}}OverridepublicbooleanonCreateOptionsMenu(Menumenu){//Inflatethemenu;thisaddsitemstotheactionbarifitispresent.getMenuInflater().inflate(R.menu.main,menu);returntrue;}OverridepublicbooleanonOptionsItemSelected(MenuItemitem){//Handleactionbaritemclickshere.Theactionbarwill//automaticallyhandleclicksontheHome/Upbutton,solong//asyouspecifyaparentactivityinAndroidManifest.xml.intid=item.getItemId();if(id==R.id.action_settings){returntrue;}returnsuper.onOptionsItemSelected(item);}}添加联系人AddrelationActivity.javaimportandroid.app.Activity;importandroid.app.AlertDialog;importandroid.content.ContentValues;importandroid.content.DialogInterface;importandroid.content.Intent;importandroid.os.Bundle;importandroid.view.View;importandroid.widget.EditText;importandroid.widget.Spinner;publicclassAddrelationActivityextendsActivity{privateEditTextaddName,addTel;privateSpinneraddGroup;protectedvoidonCreate(BundlesavedInstanceState){super.onCreate(savedInstanceState);setContentView(R.layout.addrelation);addName=(EditText)findViewById(R.id.addName);addTel=(EditText)findViewById(R.id.addTel);addGroup=(Spinner)findViewById(R.id.addGroup);}publicvoidsave(Viewview){finalContentValuesvalues=newContentValues();values.put("name",addName.getText().toString());values.put("tel",addTel.getText().toString());values.put("groupName",addGroup.getSelectedItem().toString());finalDatabaseHelperdbHelper=newDatabaseHelper(getApplicationContext());finalAlertDialog.BuilderadBuilder=newAlertDialog.Builder(this);adBuilder.setMessage("确认保存记录吗?").setPositiveButton("确认",newDialogInterface.OnClickListener(){OverridepublicvoidonClick(DialogInterfacedialog,intwhich){//TODOAuto-generatedmethodstubdbHelper.insert(values);Intentintent=getIntent();setResult(0x111,intent);AddrelationActivity.this.finish();}}).setNegativeButton("取消",newDialogInterface.OnClickListener(){publicvoidonClick(DialogInterfacedialog,intwhich){}});AlertDialogaleraDialog=adBuilder.create();aleraDialog.show();}}
text="所属组"/>
android:id="+id/addGroup"android:layout_width="wrap_content"android:layout_height="wrap_content"android:entries="array/test">
id="+id/addGroup"
entries="array/test">
android:id="+id/save"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_gravity="center"android:text="保存"/>主Activity文件MainActivity.java:importandroid.support.v7.app.ActionBarActivity;importandroid.app.AlertDialog;importandroid.app.Application;importandroid.content.DialogInterface;importandroid.content.Intent;importandroid.database.Cursor;importandroid.os.Bundle;importandroid.view.Menu;importandroid.view.MenuItem;importandroid.view.View;importandroid.widget.AdapterView;importandroid.widget.ListView;importandroid.widget.SimpleCursorAdapter;publicclassMainActivityextendsActionBarActivity{privateListViewlistView;protectedvoidonCreate(BundlesavedInstanceState){super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);listView=(ListView)findViewById(R.id.listView);//getRelationFromDB();}privatevoidgetRelationFromDB(){finalDatabaseHelperdbHelper=newDatabaseHelper(this);Cursorcursor=dbHelper.query();String[]from={"_id","name","tel","groupName"};int[]to={R.id._id,R.id.name,R.id.tel,R.id.group};SimpleCursorAdapterscadapter=newSimpleCursorAdapter(this,R.layout.relationlist,cursor,from,to);listView.setAdapter(scadapter);listView.setOnItemClickListener(newAdapterView.OnItemClickListener(){publicvoidonItemClick(AdapterView>adapter,Viewview,intposition,longid){finallongtemp=id;AlertDialog.BuilderadBuilder=newAlertDialog.Builder(MainActivity.this);adBuilder.setMessage("确认要删除记录吗?").setPositiveButton("确认",newDialogInterface.OnClickListener(){OverridepublicvoidonClick(DialogInterfacedialog,intwhich){//TODOAuto-generatedmethodstubdbHelper.del((int)temp);Cursorcursor=dbHelper.query();String[]from={"_id","name","tel","groupName"};int[]to={R.id._id,R.id.name,R.id.tel,R.id.group};SimpleCursorAdapterscadapter=newSimpleCursorAdapter(getApplicationContext(),R.layout.relationlist,cursor,from,to);MainActivity.this.listView.setAdapter(scadapter);}}).setNegativeButton("取消",newDialogInterface.OnClickListener(){OverridepublicvoidonClick(DialogInterfacedialog,intwhich){}});AlertDialogaleraDialog=adBuilder.create();aleraDialog.show();}});dbHelper.close();}publicvoidadd(Viewview){Intentintent=newIntent(MainActivity.this,AddrelationActivity.class);startActivityForResult(intent,0x111);}protectedvoidonActivityResult(intrequestCode,intresultCode,Intentdata){super.onActivityResult(requestCode,resultCode,data);if(requestCode==0x111&&resultCode==0x111){getRelationFromDB();}}OverridepublicbooleanonCreateOptionsMenu(Menumenu){//Inflatethemenu;thisaddsitemstotheactionbarifitispresent.getMenuInflater().inflate(R.menu.main,menu);returntrue;}OverridepublicbooleanonOptionsItemSelected(MenuItemitem){//Handleactionbaritemclickshere.Theactionbarwill//automaticallyhandleclicksontheHome/Upbutton,solong//asyouspecifyaparentactivityinAndroidManifest.xml.intid=item.getItemId();if(id==R.id.action_settings){returntrue;}returnsuper.onOptionsItemSelected(item);}}添加联系人AddrelationActivity.javaimportandroid.app.Activity;importandroid.app.AlertDialog;importandroid.content.ContentValues;importandroid.content.DialogInterface;importandroid.content.Intent;importandroid.os.Bundle;importandroid.view.View;importandroid.widget.EditText;importandroid.widget.Spinner;publicclassAddrelationActivityextendsActivity{privateEditTextaddName,addTel;privateSpinneraddGroup;protectedvoidonCreate(BundlesavedInstanceState){super.onCreate(savedInstanceState);setContentView(R.layout.addrelation);addName=(EditText)findViewById(R.id.addName);addTel=(EditText)findViewById(R.id.addTel);addGroup=(Spinner)findViewById(R.id.addGroup);}publicvoidsave(Viewview){finalContentValuesvalues=newContentValues();values.put("name",addName.getText().toString());values.put("tel",addTel.getText().toString());values.put("groupName",addGroup.getSelectedItem().toString());finalDatabaseHelperdbHelper=newDatabaseHelper(getApplicationContext());finalAlertDialog.BuilderadBuilder=newAlertDialog.Builder(this);adBuilder.setMessage("确认保存记录吗?").setPositiveButton("确认",newDialogInterface.OnClickListener(){OverridepublicvoidonClick(DialogInterfacedialog,intwhich){//TODOAuto-generatedmethodstubdbHelper.insert(values);Intentintent=getIntent();setResult(0x111,intent);AddrelationActivity.this.finish();}}).setNegativeButton("取消",newDialogInterface.OnClickListener(){publicvoidonClick(DialogInterfacedialog,intwhich){}});AlertDialogaleraDialog=adBuilder.create();aleraDialog.show();}}
id="+id/save"
layout_gravity="center"
text="保存"/>
主Activity文件MainActivity.java:
importandroid.support.v7.app.ActionBarActivity;
importandroid.app.AlertDialog;
importandroid.app.Application;
importandroid.content.DialogInterface;
importandroid.content.Intent;
importandroid.database.Cursor;
importandroid.os.Bundle;
importandroid.view.Menu;
importandroid.view.MenuItem;
importandroid.view.View;
importandroid.widget.AdapterView;
importandroid.widget.ListView;
importandroid.widget.SimpleCursorAdapter;
publicclassMainActivityextendsActionBarActivity{
privateListViewlistView;
protectedvoidonCreate(BundlesavedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView=(ListView)findViewById(R.id.listView);
//getRelationFromDB();
}
privatevoidgetRelationFromDB(){
finalDatabaseHelperdbHelper=newDatabaseHelper(this);
Cursorcursor=dbHelper.query();
String[]from={"_id","name","tel","groupName"};
int[]to={R.id._id,R.id.name,R.id.tel,R.id.group};
SimpleCursorAdapterscadapter=newSimpleCursorAdapter(this,
R.layout.relationlist,cursor,from,to);
listView.setAdapter(scadapter);
listView.setOnItemClickListener(newAdapterView.OnItemClickListener(){
publicvoidonItemClick(AdapterView
>adapter,Viewview,
intposition,longid){
finallongtemp=id;
AlertDialog.BuilderadBuilder=newAlertDialog.Builder(
MainActivity.this);
adBuilder
.setMessage("确认要删除记录吗?
")
.setPositiveButton("确认",
newDialogInterface.OnClickListener(){
Override
publicvoidonClick(DialogInterfacedialog,
intwhich){
//TODOAuto-generatedmethodstub
dbHelper.del((int)temp);
String[]from={"_id","name","tel",
"groupName"};
int[]to={R.id._id,R.id.name,
R.id.tel,R.id.group};
SimpleCursorAdapterscadapter=newSimpleCursorAdapter(
getApplicationContext(),
R.layout.relationlist,cursor,
from,to);
MainActivity.this.listView
.setAdapter(scadapter);
})
.setNegativeButton("取消",
});
AlertDialogaleraDialog=adBuilder.create();
aleraDialog.show();
dbHelper.close();
publicvoidadd(Viewview){
Intentintent=newIntent(MainActivity.this,AddrelationActivity.class);
startActivityForResult(intent,0x111);
protectedvoidonActivityResult(intrequestCode,intresultCode,Intentdata){
super.onActivityResult(requestCode,resultCode,data);
if(requestCode==0x111&&resultCode==0x111){
getRelationFromDB();
publicbooleanonCreateOptionsMenu(Menumenu){
//Inflatethemenu;thisaddsitemstotheactionbarifitispresent.
getMenuInflater().inflate(R.menu.main,menu);
returntrue;
publicbooleanonOptionsItemSelected(MenuItemitem){
//Handleactionbaritemclickshere.Theactionbarwill
//automaticallyhandleclicksontheHome/Upbutton,solong
//asyouspecifyaparentactivityinAndroidManifest.xml.
intid=item.getItemId();
if(id==R.id.action_settings){
returnsuper.onOptionsItemSelected(item);
添加联系人AddrelationActivity.java
importandroid.app.Activity;
importandroid.content.ContentValues;
importandroid.widget.EditText;
importandroid.widget.Spinner;
publicclassAddrelationActivityextendsActivity{
privateEditTextaddName,addTel;
privateSpinneraddGroup;
setContentView(R.layout.addrelation);
addName=(EditText)findViewById(R.id.addName);
addTel=(EditText)findViewById(R.id.addTel);
addGroup=(Spinner)findViewById(R.id.addGroup);
publicvoidsave(Viewview){
finalContentValuesvalues=newContentValues();
values.put("name",addName.getText().toString());
values.put("tel",addTel.getText().toString());
values.put("groupName",addGroup.getSelectedItem().toString());
finalDatabaseHelperdbHelper=newDatabaseHelper(
getApplicationContext());
finalAlertDialog.BuilderadBuilder=newAlertDialog.Builder(this);
adBuilder.setMessage("确认保存记录吗?
.setPositiveButton("确认",newDialogInterface.OnClickListener(){
publicvoidonClick(DialogInterfacedialog,intwhich){
dbHelper.insert(values);
Intentintent=getIntent();
setResult(0x111,intent);
AddrelationActivity.this.finish();
.setNegativeButton("取消",newDialogInterface.OnClickListener(){
下载文档到电脑,查找使用更方便
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
copyright@ 2008-2022 冰点文档网站版权所有
经营许可证编号:鄂ICP备2022015515号-1