Android 通讯录源码


目录 1. 目录结构............................................................................................................................... - 1 - 2. 界面设计.................................................................................................................................- 2 - 2.1 Main.xml.............................................................................................................................. - 2 - 2.2 main_menu_grid.xml.......................................................................................................... - 3 - 2.3 addnew.xml.......................................................................................................................... - 4 - 2.4 imageswitch.xml.............................................................................................................. - 10 - 2.5 item_menu.xml.................................................................................................................. - 11 - 2.5 item_menu.xml.................................................................................................................. - 11 - 2.7 backup_progress.xml...................................................................................................... - 17 - 2.8 enterfilename.xml.......................................................................................................... - 18 - 2.9 listitem.xml.................................................................................................................... - 18 - 2.10 login.xml........................................................................................................................ - 20 - 2.11 numchoose.xml................................................................................................................ - 23 - 3. Activity 设计................................................................................................................... - 23 - 3.1 AddNew.java...................................................................................................................... - 23 - 3.2 Main.java.......................................................................................................................... - 31 - 3.3 MainPrivacy.java............................................................................................................ - 51 - 3.4 UserDetail.java.............................................................................................................. - 70 - 3.5 DBHelper.java.................................................................................................................. - 85 - 3.6 User.java.......................................................................................................................... - 95 - - 1 - 1.1.1.1. 目录结构 - 2 - 2.2.2.2. 界面设计 2.1 Main.xmlMain.xmlMain.xmlMain.xml 2.2 main_menu_grid.xml - 4 - 2.3 addnew.xml - 5 - - 8 - 2.8 enterfilename.xml 2.9 listitem.xml 2.10 login.xml 2.11 numchoose.xml - 23 - 3.3.3.3. Activity 设计 3.1 AddNew.java publicpublicpublicpublicclassclassclassclassAddNewextendsextendsextendsextendsActivityimplementsimplementsimplementsimplementsViewFactory{ EditTextet_name; EditTextet_mobilePhone; EditTextet_officePhone; EditTextet_familyPhone; EditTextet_position; EditTextet_company; EditTextet_address; EditTextet_zipCode; EditTextet_otherContact; EditTextet_email; EditTextet_remark; Buttonbtn_save; Buttonbtn_return; intintintintprivacy;//用于判断添加的用户是不是保密的 ImageButtonimageButton;//头像按钮 ViewimageChooseView;//图像选择的视图 AlertDialogimageChooseDialog;//头像选择对话框 Gallerygallery;//头像的Gallery ImageSwitcheris;//头像的ImageSwitcher intintintintcurrentImagePosition;//用于记录当前选中图像在图像数组中的位置 intintintintpreviousImagePosition;//用于记录上一次图片的位置 booleanbooleanbooleanbooleanimageChanged;//判断头像有没有变化 /** *所有的图像图片 */ privateprivateprivateprivateintintintint[]images =newnewnewnewintintintint[]{R.drawable.icon ,R.drawable.image1,R.drawable.image2,R.drawable.image3 ,R.drawable.image4,R.drawable.image5,R.drawable.image6 ,R.drawable.image7,R.drawable.image8,R.drawable.image9 ,R.drawable.image10,R.drawable.image11,R.drawable.image12 ,R.drawable.image13,R.drawable.image14,R.drawable.image15 ,R.drawable.image16,R.drawable.image17,R.drawable.image18 ,R.drawable.image19,R.drawable.image20,R.drawable.image21 ,R.drawable.image22,R.drawable.image23,R.drawable.image24 ,R.drawable.image25,R.drawable.image26,R.drawable.image27 - 24 - ,R.drawable.image28,R.drawable.image29,R.drawable.image30}; @Override protectedprotectedprotectedprotectedvoidvoidvoidvoidonCreate(BundlesavedInstanceState){ supersupersupersuper.onCreate(savedInstanceState); setContentView(R.layout.addnew); Intentintent=getIntent(); //先确定好保密类型 ifififif(intent.getExtras()!=nullnullnullnull&& intent.getExtras().getInt("privacy")==1){ privacy=1; }elseelseelseelse{ privacy=0; } et_name=(EditText)findViewById(R.id.username); et_mobilePhone=(EditText)findViewById(R.id.mobilephone); et_officePhone=(EditText)findViewById(R.id.officephone); et_familyPhone=(EditText)findViewById(R.id.familyphone); et_position=(EditText)findViewById(R.id.position); et_company=(EditText)findViewById(R.id.company); et_address=(EditText)findViewById(R.id.address); et_zipCode=(EditText)findViewById(R.id.zipcode); et_otherContact=(EditText)findViewById(R.id.othercontact); et_email=(EditText)findViewById(R.id.email); et_remark=(EditText)findViewById(R.id.remark); btn_save=(Button)findViewById(R.id.save); btn_return=(Button)findViewById(R.id.btn_return); imageButton=(ImageButton)findViewById(R.id.image_button); /** *响应点击事件 */ btn_save.setOnClickListener(newnewnewnewOnClickListener(){ @Override publicpublicpublicpublicvoidvoidvoidvoidonClick(Viewv){ //判断姓名是否为空 Stringname=et_name.getText().toString(); ifififif(name.trim().equals("")){ Toast.makeText(AddNew.thisthisthisthis,"姓名不许为空",Toast.LENGTH_LONG).show(); returnreturnreturnreturn; } - 25 - //从表单上获取数据 Useruser=newnewnewnewUser(); user.username=name; user.address=et_address.getText().toString(); user.company=et_company.getText().toString(); user.email=et_email.getText().toString(); user.familyPhone=et_familyPhone.getText().toString(); user.mobilePhone=et_mobilePhone.getText().toString(); user.officePhone=et_officePhone.getText().toString(); user.otherContact=et_otherContact.getText().toString(); user.position=et_position.getText().toString(); user.remark=et_remark.getText().toString(); user.zipCode=et_zipCode.getText().toString(); //判断头像是否改变,若改变,则用当前的位置,若没有改变,则用前一回的位置 ifififif(imageChanged){ user.imageId= images[currentImagePosition%images.length]; }elseelseelseelse{ user.imageId= images[previousImagePosition%images.length]; } user.privacy=privacy; //创建数据库帮助类 DBHelperhelper=newnewnewnewDBHelper(AddNew.thisthisthisthis); //打开数据库 helper.openDatabase(); //把user存储到数据库里 longlonglonglongresult=helper.insert(user); //通过结果来判断是否插入成功,若为1,则表示插入数据失败 ifififif(result==-1){ Toast.makeText(AddNew.thisthisthisthis,"添加失败",Toast.LENGTH_LONG); } setTitle("用户添加成功!"); //返回到上一个Activity,也就是Main.activity setResult(3); //销毁当前视图 finish(); } }); btn_return.setOnClickListener(newnewnewnewOnClickListener(){ @Override - 26 - publicpublicpublicpublicvoidvoidvoidvoidonClick(Viewv){ finish(); } }); imageButton.setOnClickListener(newnewnewnewOnClickListener(){ @Override publicpublicpublicpublicvoidvoidvoidvoidonClick(Viewv){ loadImage();//为gallery装载图片 initImageChooseDialog();//初始化imageChooseDialog imageChooseDialog.show(); } }); } publicpublicpublicpublicvoidvoidvoidvoidloadImage(){ ifififif(imageChooseView==nullnullnullnull){ LayoutInflaterli=LayoutInflater.from(AddNew.thisthisthisthis); imageChooseView=li.inflate(R.layout.imageswitch,nullnullnullnull); //通过渲染xml文件,得到一个视图(View),再拿到这个View里面的Gallery gallery= (Gallery)imageChooseView.findViewById(R.id.gallery); //为Gallery装载图片 gallery.setAdapter(newnewnewnewImageAdapter(thisthisthisthis)); gallery.setSelection(images.length/2); is=(ImageSwitcher)imageChooseView.findViewById(R.id.imageswitch); is.setFactory(thisthisthisthis); is.setInAnimation(AnimationUtils.loadAnimation(thisthisthisthis, android.R.anim.fade_in)); //卸载图片的动画效果 is.setOutAnimation(AnimationUtils.loadAnimation(thisthisthisthis, android.R.anim.fade_out)); gallery.setOnItemSelectedListener(newnewnewnewOnItemSelectedListener(){ @Override publicpublicpublicpublicvoidvoidvoidvoidonItemSelected(AdapterViewarg0,Viewarg1, intintintintarg2,longlonglonglongarg3){ //当前的头像位置为选中的位置 currentImagePosition=arg2; //为ImageSwitcher设置图像 is.setImageResource(images[arg2%images.length]); - 27 - } @Override publicpublicpublicpublicvoidvoidvoidvoidonNothingSelected(AdapterViewarg0){ }}); } } /** *自定义Gallery的适配器 *@author@author@author@authorAdministrator * */ classclassclassclassImageAdapterextendsextendsextendsextendsBaseAdapter{ privateprivateprivateprivateContextcontext; publicpublicpublicpublicImageAdapter(Contextcontext){ thisthisthisthis.context=context; } @Override publicpublicpublicpublicintintintintgetCount(){ returnreturnreturnreturnInteger.MAX_VALUE; } @Override publicpublicpublicpublicObjectgetItem(intintintintposition){ returnreturnreturnreturnposition; } @Override publicpublicpublicpubliclonglonglonglonggetItemId(intintintintposition){ returnreturnreturnreturnposition; } /** *gallery从这个方法中拿到image */ @Override publicpublicpublicpublicViewgetView(intintintintposition,ViewconvertView,ViewGroup parent){ - 28 - ImageViewiv=newnewnewnewImageView(context); iv.setImageResource(images[position%images.length]); iv.setAdjustViewBounds(truetruetruetrue); iv.setLayoutParams(newnewnewnewGallery.LayoutParams(80,80)); iv.setPadding(15,10,15,10); returnreturnreturnreturniv; } } @Override publicpublicpublicpublicViewmakeView(){ ImageViewview=newnewnewnewImageView(thisthisthisthis); view.setBackgroundColor(0xff000000); view.setScaleType(ScaleType.FIT_CENTER); view.setLayoutParams(newnewnewnewImageSwitcher.LayoutParams(90,90)); returnreturnreturnreturnview; } publicpublicpublicpublicvoidvoidvoidvoidinitImageChooseDialog(){ ifififif(imageChooseDialog==nullnullnullnull){ AlertDialog.Builderbuilder=newnewnewnewAlertDialog.Builder(thisthisthisthis); builder.setTitle("请选择图像") .setView(imageChooseView).setPositiveButton("确定",newnewnewnew DialogInterface.OnClickListener(){ @Override publicpublicpublicpublicvoidvoidvoidvoidonClick(DialogInterfacedialog,intintintintwhich){ imageChanged=truetruetruetrue; previousImagePosition=currentImagePosition; imageButton.setImageResource(images[currentImagePosition%images.l ength]); } }) .setNegativeButton("取消",newnewnewnew DialogInterface.OnClickListener(){ @Override publicpublicpublicpublicvoidvoidvoidvoidonClick(DialogInterfacedialog,intintintintwhich){ currentImagePosition=previousImagePosition; } - 29 - }); imageChooseDialog=builder.create(); } } /** *当退出的时候,回收资源 */ @Override protectedprotectedprotectedprotectedvoidvoidvoidvoidonDestroy(){ ifififif(is!=nullnullnullnull){ is=nullnullnullnull; } ifififif(gallery!=nullnullnullnull){ gallery=nullnullnullnull; } ifififif(imageChooseDialog!=nullnullnullnull){ imageChooseDialog=nullnullnullnull; } ifififif(imageChooseView!=nullnullnullnull){ imageChooseView=nullnullnullnull; } ifififif(imageButton!=nullnullnullnull){ imageButton=nullnullnullnull; } supersupersupersuper.onDestroy(); } } 3.2 Main.java publicpublicpublicpublicclassclassclassclassMainextendsextendsextendsextendsActivity{ //显示所有数据的ListView ListViewlv; ArrayListlist; //拥有所有数据的Adapter SimpleAdapteradapter; //屏幕下方的工具栏 GridViewbottomMenuGrid; //主菜单的布局 - 30 - GridViewmainMenuGrid; //主菜单的视图 ViewmainMenuView; //登录的视图 ViewloginView; //装搜索框的linearlayout,默认情况下visibility=gone LinearLayoutsearchLinearout; LinearLayoutmainLinearLayout; //搜索框 EditTextet_search; EditTextet_enter_file_name; //主菜单的对话框 AlertDialogmainMenuDialog; //确认对话框 AlertDialogconfirmDialog; //进度条对话框 AlertDialogprogressDialog; //输入文件名的对话框 AlertDialogenterFileNameDialog; //输入用户名密码的对话框 AlertDialogloginDialog; //表示保密状态 booleanbooleanbooleanbooleanprivacy=falsefalsefalsefalse; //存储标记的数目 intintintintmarkedNum; //存储标记条目的_id号 ArrayListdeleteId; //菜单文字 String[]main_menu_itemName={"显示所有","删除所有","备份数据"," 还原数据","更新","后退"}; //主菜单图片 intintintint[]main_menu_itemSource={ R.drawable.showall, R.drawable.menu_delete, R.drawable.menu_backup, R.drawable.menu_restore, R.drawable.menu_fresh, R.drawable.menu_return}; String[]bottom_menu_itemName={"增加","查找","删除","菜单","退 出"}; StringfileName; - 31 - intintintint[]bottom_menu_itemSource={ R.drawable.menu_new_user, R.drawable.menu_search, R.drawable.menu_delete, R.drawable.controlbar_showtype_list, R.drawable.menu_exit}; /** *onCreate做的工作就是把listView显示出来 *bottomMenuGrid,mainMenuGrid,searchLinearout都是到要用 *的时候再初始化,并且只初始化一次 */ @Override publicpublicpublicpublicvoidvoidvoidvoidonCreate(BundlesavedInstanceState){ supersupersupersuper.onCreate(savedInstanceState); setContentView(R.layout.main); mainLinearLayout=(LinearLayout)findViewById(R.id.list_ll); DBHelperhelper=newnewnewnewDBHelper(thisthisthisthis);//获得所有用户的list helper.openDatabase();//打开数据库,就打开这一次,因为Helper中的 SQLiteDatabase是静态的。 list=helper.getAllUser(privacy);//拿到所有保密状态为privacy的用户 的list lv=(ListView)findViewById(R.id.lv_userlist);//创建ListView对 象 ifififif(list.size()==0){ Drawablenodata_bg=getResources().getDrawable(R.drawable.nodata_bg); mainLinearLayout.setBackgroundDrawable(nodata_bg); setTitle("没有查到任何数据"); } //将数据与adapter集合起来 adapter=newnewnewnewSimpleAdapter(thisthisthisthis, list, R.layout.listitem, newnewnewnew String[]{"imageid","name","mobilephone"}, newnewnewnew intintintint[]{R.id.user_image,R.id.tv_name,R.id.tv_mobilephone}); lv.setAdapter(adapter);//将整合好的adapter交给listview,显示给用户看 - 32 - lv.setOnItemClickListener(newnewnewnewOnItemClickListener(){ /** *响应单击事件,单点击某一个选项的时候,跳转到用户详细信息页面 */ @Override publicpublicpublicpublicvoidvoidvoidvoidonItemClick(AdapterViewarg0,Viewarg1,intintintintarg2, longlonglonglongarg3){ HashMapitem=(HashMap)arg0.getItemAtPosition(arg2); intintintint_id=Integer.parseInt(String.valueOf(item.get("_id"))); Intentintent=newnewnewnewIntent(Main.thisthisthisthis,UserDetail.classclassclassclass); Useruser=newnewnewnewUser(); user._id= Integer.parseInt(String.valueOf(item.get("_id"))); user.address=String.valueOf(item.get("address")); user.company=String.valueOf(item.get("company")); user.email=String.valueOf(item.get("email")); user.familyPhone= String.valueOf(item.get("familyphone")); user.mobilePhone=String.valueOf(item.get("mobilephone")); user.officePhone=String.valueOf(item.get("officephone")); user.otherContact=String.valueOf(item.get("othercontact")); user.position=String.valueOf(item.get("position")); user.remark=String.valueOf(item.get("remark")); user.username=String.valueOf(item.get("name")); user.zipCode=String.valueOf(item.get("zipcode")); user.imageId=Integer.parseInt(String.valueOf(item.get("imageid"))); intent.putExtra("user",user); ifififif(searchLinearout!=nullnullnullnull&& searchLinearout.getVisibility()==View.VISIBLE){ searchLinearout.setVisibility(View.GONE); } /*将arg2作为请求码传过去 用于标识修改项的位置*/ startActivityForResult(intent,arg2); } }); lv.setCacheColorHint(Color.TRANSPARENT);//设置ListView的背景为透 明 lv.setOnItemLongClickListener(newnewnewnewOnItemLongClickListener(){ - 33 - @Override publicpublicpublicpublicbooleanbooleanbooleanbooleanonItemLongClick(AdapterViewarg0,View arg1, intintintintarg2,longlonglonglongarg3){ ifififif(deleteId==nullnullnullnull){ deleteId=newnewnewnewArrayList(); } HashMapitem=(HashMap)arg0.getItemAtPosition(arg2); Integer_id=Integer.parseInt(String.valueOf(item.get("_id"))); RelativeLayoutr=(RelativeLayout)arg1; ImageViewmarkedView=(ImageView)r.getChildAt(2); ifififif(markedView.getVisibility()==View.VISIBLE){ markedView.setVisibility(View.GONE); deleteId.remove(_id); }elseelseelseelse{ markedView.setVisibility(View.VISIBLE); deleteId.add(_id); } returnreturnreturnreturntruetruetruetrue; } }); //为list添加item选择器 DrawablebgDrawable= getResources().getDrawable(R.drawable.list_bg); lv.setSelector(bgDrawable); } @Override protectedprotectedprotectedprotectedvoidvoidvoidvoidonActivityResult(intintintintrequestCode,intintintintresultCode, Intentdata){ supersupersupersuper.onActivityResult(requestCode,resultCode,data); //清除deleteId的内容 ifififif(deleteId!=nullnullnullnull){ deleteId.clear(); } //当resultCode==3时代表添加了一个用户返回,当resultCode==4的时候代表修 改了用户,或者删除了用户,其他条件代表数据没有变化 ifififif(resultCode==3||resultCode==4){ DBHelperhelper=newnewnewnewDBHelper(thisthisthisthis); list=helper.getAllUser(privacy); - 34 - adapter= newnewnewnewSimpleAdapter( thisthisthisthis, list, R.layout.listitem, newnewnewnew String[]{"imageid","name","mobilephone"}, newnewnewnew intintintint[]{R.id.user_image,R.id.tv_name,R.id.tv_mobilephone}); ifififif(list.size()>0){ mainLinearLayout.setBackgroundDrawable(nullnullnullnull); } } lv.setAdapter(adapter);//将整合好的adapter交给listview,显示给用户 看 /** *resultCode只有3、4、5 *当等于4或者5的时候,代表由UserDetail转过来的。在转想UserDetail的时候, requestCode的值设置的是选中项的位置 */ ifififif(resultCode==3){ lv.setSelection(list.size()); }elseelseelseelse{ lv.setSelection(requestCode); } } /** *响应点击Menu按钮时的事件,用于设置底部菜单是否可见 */ @Override publicpublicpublicpublicbooleanbooleanbooleanbooleanonKeyDown(intintintintkeyCode,KeyEventevent){ ifififif(keyCode==KeyEvent.KEYCODE_MENU){ loadBottomMenu(); ifififif(bottomMenuGrid.getVisibility()==View.VISIBLE){ ifififif(searchLinearout!=nullnullnullnull&& searchLinearout.getVisibility()==View.VISIBLE){ searchLinearout.setVisibility(View.GONE); } bottomMenuGrid.setVisibility(View.GONE); }elseelseelseelse{ - 35 - bottomMenuGrid.setVisibility(View.VISIBLE); } } returnreturnreturnreturnsupersupersupersuper.onKeyDown(keyCode,event); } privateprivateprivateprivatevoidvoidvoidvoidloadBottomMenu(){ ifififif(bottomMenuGrid==nullnullnullnull){ bottomMenuGrid=(GridView) findViewById(R.id.gv_buttom_menu); bottomMenuGrid.setBackgroundResource(R.drawable.channelgallery_bg) ;//设置背景 bottomMenuGrid.setNumColumns(5);//设置每行列数 bottomMenuGrid.setGravity(Gravity.CENTER);//位置居中 bottomMenuGrid.setVerticalSpacing(10);//垂直间隔 bottomMenuGrid.setHorizontalSpacing(10);//水平间隔 bottomMenuGrid.setAdapter(getMenuAdapter(bottom_menu_itemName, bottom_menu_itemSource));//设置菜单Adapter /**监听底部菜单选项 **/ bottomMenuGrid.setOnItemClickListener(newnewnewnew OnItemClickListener(){ publicpublicpublicpublicvoidvoidvoidvoidonItemClick(AdapterViewarg0,Viewarg1, intintintintarg2, longlonglonglongarg3){ switchswitchswitchswitch(arg2){ casecasecasecase0:{ ifififif(searchLinearout!=nullnullnullnull&& searchLinearout.getVisibility()==View.VISIBLE){ searchLinearout.setVisibility(View.GONE); } ifififif(bottomMenuGrid.getVisibility()==View.VISIBLE){ bottomMenuGrid.setVisibility(View.GONE); } Intentintent=newnewnewnewIntent(Main.thisthisthisthis,AddNew.classclassclassclass); startActivityForResult(intent,3); breakbreakbreakbreak; } - 36 - casecasecasecase1: loadSearchLinearout(); ifififif(searchLinearout.getVisibility()==View.VISIBLE){ searchLinearout.setVisibility(View.GONE); }elseelseelseelse{ searchLinearout.setVisibility(View.VISIBLE); et_search.requestFocus(); et_search.selectAll(); } breakbreakbreakbreak; casecasecasecase2: ifififif(searchLinearout!=nullnullnullnull&& searchLinearout.getVisibility()==View.VISIBLE){ searchLinearout.setVisibility(View.GONE); } ifififif(deleteId==nullnullnullnull||deleteId.size()==0){ Toast.makeText(Main.thisthisthisthis,"没有标记任何记录\n长按一条记录即可标记", Toast.LENGTH_LONG).show(); }elseelseelseelse{ newnewnewnewAlertDialog.Builder(Main.thisthisthisthis).setTitle("确定要删除标记的 "+deleteId.size()+"条记录吗?").setPositiveButton("确定",newnewnewnew DialogInterface.OnClickListener(){ @Override publicpublicpublicpublicvoidvoidvoidvoidonClick(DialogInterfacedialog,intintintintwhich){ DBHelperhelper=newnewnewnewDBHelper(Main.thisthisthisthis); helper.deleteMarked(deleteId); //重置视图 list=helper.getAllUser(privacy); adapter= newnewnewnewSimpleAdapter( Main.thisthisthisthis, list, R.layout.listitem, newnewnewnew String[]{"imageid","name","mobilephone"}, newnewnewnew intintintint[]{R.id.user_image,R.id.tv_name,R.id.tv_mobilephone}); lv.setAdapter(adapter); deleteId.clear(); } }) .setNegativeButton("取消",nullnullnullnull) - 37 - .create() .show(); } breakbreakbreakbreak; casecasecasecase 3: ifififif(searchLinearout!=nullnullnullnull && searchLinearout.getVisibility()==View.VISIBLE){ searchLinearout.setVisibility(View.GONE); } loadMainMenuDialog(); mainMenuDialog.show(); breakbreakbreakbreak; casecasecasecase 4: finish(); breakbreakbreakbreak; } } }); } } privateprivateprivateprivate voidvoidvoidvoid loadMainMenuDialog(){ ifififif(mainMenuDialog==nullnullnullnull){ LayoutInflaterli=LayoutInflater.from(thisthisthisthis); mainMenuView=li.inflate(R.layout.main_menu_grid,nullnullnullnull); //根据主菜单视图,创建主菜单对话框 mainMenuDialog=newnewnewnew AlertDialog.Builder(thisthisthisthis).setView(mainMenuView).create(); //根据主菜单视图,拿到视图文件中的GridView,然后再往里面放Adapter mainMenuGrid= (GridView)mainMenuView.findViewById(R.id.gridview); SimpleAdaptermenuAdapter= getMenuAdapter(main_menu_itemName,main_menu_itemSource); mainMenuGrid.setAdapter(menuAdapter); //响应点击事件 mainMenuGrid.setOnItemClickListener(newnewnewnew OnItemClickListener(){ @Override publicpublicpublicpublic voidvoidvoidvoid onItemClick(AdapterViewarg0,Viewarg1, intintintint arg2, longlonglonglong arg3){ - 38 - switchswitchswitchswitch(arg2){ casecasecasecase0:{ DBHelperhelper=newnewnewnewDBHelper(Main.thisthisthisthis); list=helper.getAllUser(privacy); adapter=newnewnewnewSimpleAdapter( Main.thisthisthisthis, list, R.layout.listitem, newnewnewnew String[]{"imageid","name","mobilephone"}, newnewnewnew intintintint[]{R.id.user_image,R.id.tv_name,R.id.tv_mobilephone}); lv.setAdapter(adapter);//显示所有数据 mainMenuDialog.dismiss(); breakbreakbreakbreak; } casecasecasecase1:{ AlertDialog.Builderbuilder=newnewnewnew AlertDialog.Builder(Main.thisthisthisthis); confirmDialog=builder.create(); builder.setTitle("是否删除所有!?"); builder.setPositiveButton("确定",newnewnewnew DialogInterface.OnClickListener(){ @Override publicpublicpublicpublicvoidvoidvoidvoidonClick(DialogInterface dialog,intintintintwhich){ DBHelperhelper=newnewnewnew DBHelper(Main.thisthisthisthis); helper.deleteAll(0); list=helper.getAllUser(privacy); adapter=newnewnewnewSimpleAdapter( Main.thisthisthisthis, list, R.layout.listitem, newnewnewnew String[]{"imageid","name","mobilephone"}, newnewnewnew intintintint[]{R.id.user_image,R.id.tv_name,R.id.tv_mobilephone}); lv.setAdapter(adapter);//显示所有数 据 mainMenuDialog.dismiss(); } - 39 - }); builder.setNegativeButton("取消",newnewnewnew DialogInterface.OnClickListener(){ @Override publicpublicpublicpublicvoidvoidvoidvoidonClick(DialogInterface dialog,intintintintwhich){ confirmDialog.dismiss(); } }); builder.create().show(); breakbreakbreakbreak; } casecasecasecase2:{ mainMenuDialog.dismiss(); newnewnewnewAlertDialog.Builder(Main.thisthisthisthis) .setTitle("是否需要备份记录到SD卡?") .setPositiveButton("确定",newnewnewnew DialogInterface.OnClickListener(){ @Override publicpublicpublicpublicvoidvoidvoidvoidonClick(DialogInterfacedialog, intintintintwhich){ LayoutInflaterli= LayoutInflater.from(Main.thisthisthisthis); Viewbackup_view= li.inflate(R.layout.backup_progress,nullnullnullnull); progressDialog=newnewnewnew AlertDialog.Builder(Main.thisthisthisthis) .setTitle("备份正在进行中...") .setView(backup_view) .create(); progressDialog.show(); DBHelperhelper=newnewnewnewDBHelper(Main.thisthisthisthis); helper.backupData(privacy); ProgressBarbar=(ProgressBar) backup_view.findViewById(R.id.pb_backup); Buttonbtn_backup_ok= (Button)backup_view.findViewById(R.id.btn_backuup_ok); bar.setMax(list.size()); forforforfor(intintintinti=0;i<=list.size();i++){ bar.setProgress(i); } progressDialog.setTitle("备份完成!一共 "+ list.size()+"条记录"); - 40 - btn_backup_ok.setVisibility(View.VISIBLE); btn_backup_ok.setOnClickListener(newnewnewnew OnClickListener(){ @Override publicpublicpublicpublicvoidvoidvoidvoidonClick(Viewv){ progressDialog.dismiss(); mainMenuDialog.dismiss(); } }); } }) .setNegativeButton("取消",nullnullnullnull) .create() .show(); breakbreakbreakbreak; } casecasecasecase3:{ LayoutInflaterli= LayoutInflater.from(Main.thisthisthisthis); ViewenterFileNameView= li.inflate(R.layout.enterfilename,nullnullnullnull); enterFileNameDialog=newnewnewnew AlertDialog.Builder(Main.thisthisthisthis) .setView(enterFileNameView).setNegativeButton("取消",nullnullnullnull) .setPositiveButton("确定",newnewnewnew DialogInterface.OnClickListener(){ @Override publicpublicpublicpublicvoidvoidvoidvoidonClick(DialogInterfacedialog, intintintintwhich){ DBHelperhelper=newnewnewnew DBHelper(Main.thisthisthisthis); fileName= et_enter_file_name.getText().toString(); ifififif(helper.findFile(fileName)){ newnewnewnew AlertDialog.Builder(Main.thisthisthisthis).setTitle("请选择方式") .setPositiveButton("覆盖",newnewnewnew DialogInterface.OnClickListener(){ - 41 - @Override publicpublicpublicpublicvoidvoidvoidvoid onClick(DialogInterfacedialog,intintintintwhich){ DBHelperhelper=newnewnewnew DBHelper(Main.thisthisthisthis); helper.deleteAll(0); helper.restoreData(fileName); list= helper.getAllUser(privacy); adapter=newnewnewnew SimpleAdapter(Main.thisthisthisthis, list, R.layout.listitem, newnewnewnew String[]{"imageid","name","mobilephone"}, newnewnewnew intintintint[]{R.id.user_image,R.id.tv_name,R.id.tv_mobilephone}); LayoutInflaterli= LayoutInflater.from(Main.thisthisthisthis); Viewbackup_view= li.inflate(R.layout.backup_progress,nullnullnullnull); progressDialog=newnewnewnew AlertDialog.Builder(Main.thisthisthisthis) .setTitle("正在还原数据...") .setView(backup_view) .create(); progressDialog.show(); ProgressBarbar= (ProgressBar)backup_view.findViewById(R.id.pb_backup); Buttonbtn_backup_ok= (Button)backup_view.findViewById(R.id.btn_backuup_ok); bar.setMax(list.size()); forforforfor(intintintint i=0;i<=list.size();i++){ bar.setProgress(i); } progressDialog.setTitle("还 原完成!一共还原了 "+list.size()+"条记录"); btn_backup_ok.setVisibility(View.VISIBLE); btn_backup_ok.setOnClickListener(newnewnewnewOnClickListener(){ - 42 - @Override publicpublicpublicpublicvoidvoidvoidvoidonClick(View v){ progressDialog.dismiss(); mainMenuDialog.dismiss(); ifififif(list.size()!=0) { mainLinearLayout.setBackgroundDrawable(nullnullnullnull); } lv.setAdapter(adapter); } }); } }) .setNegativeButton("添加",newnewnewnew DialogInterface.OnClickListener(){ @Override publicpublicpublicpublicvoidvoidvoidvoid onClick(DialogInterfacedialog,intintintintwhich){ DBHelperhelper=newnewnewnew DBHelper(Main.thisthisthisthis); intintintintpreNum=list.size(); helper.restoreData(fileName); list= helper.getAllUser(privacy); adapter=newnewnewnew SimpleAdapter(Main.thisthisthisthis, list, R.layout.listitem, newnewnewnew String[]{"imageid","name","mobilephone"}, newnewnewnew intintintint[]{R.id.user_image,R.id.tv_name,R.id.tv_mobilephone}); LayoutInflaterli= LayoutInflater.from(Main.thisthisthisthis); - 43 - Viewbackup_view= li.inflate(R.layout.backup_progress,nullnullnullnull); progressDialog=newnewnewnew AlertDialog.Builder(Main.thisthisthisthis) .setTitle("正在还原数据...") .setView(backup_view) .create(); progressDialog.show(); ProgressBarbar= (ProgressBar)backup_view.findViewById(R.id.pb_backup); Buttonbtn_backup_ok= (Button)backup_view.findViewById(R.id.btn_backuup_ok); bar.setMax(list.size()); forforforfor(intintintint i=0;i<=list.size();i++){ bar.setProgress(i); } progressDialog.setTitle("还 原完成!一共还原了 "+(list.size()-preNum)+"条记录"); btn_backup_ok.setVisibility(View.VISIBLE); btn_backup_ok.setOnClickListener(newnewnewnewOnClickListener(){ @Override publicpublicpublicpublicvoidvoidvoidvoidonClick(View v){ progressDialog.dismiss(); mainMenuDialog.dismiss(); lv.setAdapter(adapter); } }); } }) .setNeutralButton("取消",newnewnewnew DialogInterface.OnClickListener(){ @Override publicpublicpublicpublicvoidvoidvoidvoid - 44 - onClick(DialogInterfacedialog,intintintintwhich){ } }).create().show(); }elseelseelseelse{ Toast.makeText(enterFileNameDialog.getContext(),"找不到备份文件", Toast.LENGTH_LONG).show(); } } }) .create(); et_enter_file_name= (EditText)enterFileNameView.findViewById(R.id.et_enter_file_name); et_enter_file_name.setText("comm_data"); et_enter_file_name.requestFocus(); et_enter_file_name.selectAll(); enterFileNameDialog.show(); adapter=newnewnewnewSimpleAdapter( Main.thisthisthisthis, list, R.layout.listitem, newnewnewnew String[]{"imageid","name","mobilephone"}, newnewnewnew intintintint[]{R.id.user_image,R.id.tv_name,R.id.tv_mobilephone}); lv.setAdapter(adapter);//显示所有数据 mainMenuDialog.dismiss(); breakbreakbreakbreak; } casecasecasecase4:{ mainMenuDialog.dismiss(); newnewnewnewAlertDialog.Builder(Main.thisthisthisthis) .setTitle("更新操作将需要支付20元的费用!是否继续? ") .setPositiveButton("确定",newnewnewnew DialogInterface.OnClickListener(){ @Override publicpublicpublicpublicvoidvoidvoidvoidonClick(DialogInterfacedialog, intintintintwhich){ //新建一个activity出来 LayoutInflaterli= - 45 - LayoutInflater.from(Main.thisthisthisthis); loginView=li.inflate(R.layout.login, nullnullnullnull); Buttonbtn_login_ok= (Button)loginView.findViewById(R.id.btn_login_ok); Buttonbtn_login_cancel= (Button)loginView.findViewById(R.id.btn_login_cancel); finalfinalfinalfinalEditTextet_account= (EditText)loginView.findViewById(R.id.et_account); finalfinalfinalfinalEditTextet_password= (EditText)loginView.findViewById(R.id.et_password); btn_login_ok.setOnClickListener(newnewnewnew OnClickListener(){ @Override publicpublicpublicpublicvoidvoidvoidvoidonClick(Viewv){ ifififif(et_account.getText().toString().equals("admin")&& et_password.getText().toString().equals("admin")){ et_account.setText(""); et_password.setText(""); loginDialog.dismiss(); Intentintent=newnewnewnew Intent(Main.thisthisthisthis,MainPrivacy.classclassclassclass); startActivity(intent); }elseelseelseelse{ Toast.makeText(Main.thisthisthisthis," 失败",Toast.LENGTH_LONG).show(); } } }); btn_login_cancel.setOnClickListener(newnewnewnewOnClickListener(){ @Override publicpublicpublicpublicvoidvoidvoidvoidonClick(Viewv){ loginDialog.dismiss(); } }); - 46 - ifififif(loginDialog==nullnullnullnull){ loginDialog=newnewnewnew AlertDialog.Builder(Main.thisthisthisthis).setView(loginView).create(); } loginDialog.show(); } }) .setNegativeButton("取消",nullnullnullnull) .create() .show(); breakbreakbreakbreak; } casecasecasecase5:{ mainMenuDialog.dismiss(); breakbreakbreakbreak; } } }}); } } privateprivateprivateprivatevoidvoidvoidvoidloadSearchLinearout(){ ifififif(searchLinearout==nullnullnullnull){ searchLinearout=(LinearLayout) findViewById(R.id.ll_search); et_search=(EditText)findViewById(R.id.et_search); et_search.setOnKeyListener(newnewnewnewOnKeyListener(){ @Override publicpublicpublicpublicbooleanbooleanbooleanbooleanonKey(Viewarg0,intintintintarg1,KeyEventarg2) { Stringcondition=et_search.getText().toString(); ifififif(condition.equals("")){ lv.setAdapter(adapter); } DBHelperhelper=newnewnewnewDBHelper(Main.thisthisthisthis); list=helper.getUsers(condition,privacy); SimpleAdaptersearchAdapter= newnewnewnewSimpleAdapter( Main.thisthisthisthis, - 47 - list, R.layout.listitem, newnewnewnew String[]{"imageid","name","mobilephone"}, newnewnewnew intintintint[]{R.id.user_image,R.id.tv_name,R.id.tv_mobilephone}); lv.setAdapter(searchAdapter);//将整合好的adapter交给 listview,显示给用户看 ifififif(list.size()==0){ Drawablenodata_bg= getResources().getDrawable(R.drawable.nodata_bg); mainLinearLayout.setBackgroundDrawable(nodata_bg); setTitle("没有查到任何数据"); }elseelseelseelse{ setTitle("共查到 "+list.size()+"条记录"); mainLinearLayout.setBackgroundDrawable(nullnullnullnull); } returnreturnreturnreturnfalsefalsefalsefalse; }}); } } privateprivateprivateprivateSimpleAdaptergetMenuAdapter(String[]menuNameArray, intintintint[]imageResourceArray){ ArrayList>data=newnewnewnew ArrayList>(); forforforfor(intintintinti=0;imap=newnewnewnewHashMap(); map.put("itemImage",imageResourceArray[i]); map.put("itemText",menuNameArray[i]); data.add(map); } SimpleAdaptersimperAdapter= newnewnewnewSimpleAdapter( thisthisthisthis, data, R.layout.item_menu, newnewnewnewString[]{"itemImage","itemText"}, newnewnewnewintintintint[]{R.id.item_image, R.id.item_text}); - 48 - returnreturnreturnreturnsimperAdapter; } /** *当退出的时候,回收资源 */ @Override protectedprotectedprotectedprotectedvoidvoidvoidvoidonDestroy(){ ifififif(confirmDialog!=nullnullnullnull){ confirmDialog=nullnullnullnull; } ifififif(mainMenuDialog!=nullnullnullnull){ mainMenuDialog=nullnullnullnull; } ifififif(searchLinearout!=nullnullnullnull){ searchLinearout=nullnullnullnull; } ifififif(mainMenuView!=nullnullnullnull){ mainMenuView=nullnullnullnull; } ifififif(mainMenuGrid!=nullnullnullnull){ mainMenuGrid=nullnullnullnull; } ifififif(bottomMenuGrid!=nullnullnullnull){ bottomMenuGrid=nullnullnullnull; } ifififif(adapter!=nullnullnullnull){ adapter=nullnullnullnull; } ifififif(list!=nullnullnullnull){ list=nullnullnullnull; } ifififif(lv!=nullnullnullnull){ lv=nullnullnullnull; } ifififif(DBHelper.dbInstance!=nullnullnullnull){ DBHelper.dbInstance.close(); DBHelper.dbInstance=nullnullnullnull; } System.out.println("destory!!!"); supersupersupersuper.onDestroy(); } - 49 - } 3.3 MainPrivacy.java publicpublicpublicpublicclassclassclassclassMainPrivacyextendsextendsextendsextendsActivity{ //显示所有数据的ListView ListViewlv; ArrayListlist; //拥有所有数据的Adapter SimpleAdapteradapter; //屏幕下方的工具栏 GridViewbottomMenuGrid; //主菜单的布局 GridViewmainMenuGrid; //主菜单的视图 ViewmainMenuView; ViewloginView; //装搜索框的linearlayout,默认情况下visibility=gone LinearLayoutsearchLinearout; LinearLayoutmainLinearLayout; //搜索框 EditTextet_search; EditTextet_enter_file_name; //主菜单的对话框 AlertDialogmainMenuDialog; AlertDialogconfirmDialog; AlertDialogprogressDialog; AlertDialogenterFileNameDialog; AlertDialogloginDialog; booleanbooleanbooleanbooleanprivacy=truetruetruetrue; intintintinti; ArrayListdeleteId; /**菜单文字 **/ String[]main_menu_itemName={"显示所有","删除所有","备份数据"," - 50 - 还原数据","退出秘密仓库","后退"}; //主菜单图片 intintintint[]main_menu_itemSource={ R.drawable.showall, R.drawable.menu_delete, R.drawable.menu_backup, R.drawable.menu_restore, R.drawable.menu_quit, R.drawable.menu_return}; String[]bottom_menu_itemName={"增加","查找","删除","菜单","退 出"}; StringfileName; intintintint[]bottom_menu_itemSource={ R.drawable.menu_new_user, R.drawable.menu_search, R.drawable.menu_delete, R.drawable.controlbar_showtype_list, R.drawable.menu_exit}; /** *onCreate做的工作就是把listView显示出来 *bottomMenuGrid,mainMenuGrid,searchLinearout都是到要用 *的时候再初始化,并且只初始化一次 */ @Override publicpublicpublicpublicvoidvoidvoidvoidonCreate(BundlesavedInstanceState){ supersupersupersuper.onCreate(savedInstanceState); setContentView(R.layout.main); setTitle("秘密仓库"); Toast.makeText(thisthisthisthis,"成功进入秘密仓库", Toast.LENGTH_LONG).show(); mainLinearLayout=(LinearLayout)findViewById(R.id.list_ll); DBHelperhelper=newnewnewnewDBHelper(thisthisthisthis);//获得所有用户的list helper.openDatabase();//打开数据库,就打开这一次,因为Helper中的 SQLiteDatabase是静态的。 list=helper.getAllUser(privacy);//拿到所有用户的list lv=(ListView)findViewById(R.id.lv_userlist);//创建ListView对 象 ifififif(list.size()==0){ Drawablenodata_bg= getResources().getDrawable(R.drawable.nodata_bg); - 51 - mainLinearLayout.setBackgroundDrawable(nodata_bg); setTitle("没有查到任何数据"); } //将数据与adapter集合起来 adapter=newnewnewnewSimpleAdapter(thisthisthisthis, list, R.layout.listitem, newnewnewnew String[]{"imageid","name","mobilephone"}, newnewnewnew intintintint[]{R.id.user_image,R.id.tv_name,R.id.tv_mobilephone}); lv.setAdapter(adapter);//将整合好的adapter交给listview,显示给用户看 lv.setOnItemClickListener(newnewnewnewOnItemClickListener(){ /** *响应单击事件,单点击某一个选项的时候,跳转到用户详细信息页面 */ @Override publicpublicpublicpublicvoidvoidvoidvoidonItemClick(AdapterViewarg0,Viewarg1,intintintint arg2, longlonglonglongarg3){ HashMapitem=(HashMap)arg0.getItemAtPosition(arg2); intintintint_id= Integer.parseInt(String.valueOf(item.get("_id"))); Intentintent=newnewnewnew Intent(MainPrivacy.thisthisthisthis,UserDetail.classclassclassclass); Useruser=newnewnewnewUser(); user._id= Integer.parseInt(String.valueOf(item.get("_id"))); user.address=String.valueOf(item.get("address")); user.company=String.valueOf(item.get("company")); user.email=String.valueOf(item.get("email")); user.familyPhone= String.valueOf(item.get("familyphone")); user.mobilePhone= String.valueOf(item.get("mobilephone")); user.officePhone= String.valueOf(item.get("officephone")); user.otherContact= String.valueOf(item.get("othercontact")); - 52 - user.position=String.valueOf(item.get("position")); user.remark=String.valueOf(item.get("remark")); user.username=String.valueOf(item.get("name")); user.zipCode=String.valueOf(item.get("zipcode")); user.imageId= Integer.parseInt(String.valueOf(item.get("imageid"))); intent.putExtra("user",user); ifififif(searchLinearout!=nullnullnullnull&& searchLinearout.getVisibility()==View.VISIBLE){ searchLinearout.setVisibility(View.GONE); } /*将arg2作为请求码传过去 用于标识修改项的位置*/ startActivityForResult(intent,arg2); } }); lv.setCacheColorHint(Color.TRANSPARENT);//设置ListView的背景为透 明 lv.setOnItemLongClickListener(newnewnewnewOnItemLongClickListener(){ @Override publicpublicpublicpublicbooleanbooleanbooleanbooleanonItemLongClick(AdapterViewarg0,View arg1, intintintintarg2,longlonglonglongarg3){ ifififif(deleteId==nullnullnullnull){ deleteId=newnewnewnewArrayList(); } HashMapitem=(HashMap)arg0.getItemAtPosition(arg2); Integer_id= Integer.parseInt(String.valueOf(item.get("_id"))); RelativeLayoutr=(RelativeLayout)arg1; ImageViewiv=(ImageView)r.getChildAt(2); iv.setEnabled(falsefalsefalsefalse); ifififif(iv.getVisibility()==View.VISIBLE){ iv.setVisibility(View.GONE); deleteId.remove(_id); }elseelseelseelse{ iv.setVisibility(View.VISIBLE); deleteId.add(_id); } - 53 - returnreturnreturnreturntruetruetruetrue; } }); //为list添加item选择器 DrawablebgDrawableSelector= getResources().getDrawable(R.drawable.list_bg); lv.setSelector(bgDrawableSelector); } @Override protectedprotectedprotectedprotectedvoidvoidvoidvoidonActivityResult(intintintintrequestCode,intintintintresultCode, Intentdata){ supersupersupersuper.onActivityResult(requestCode,resultCode,data); //当resultCode==3时代表添加了一个用户返回,当resultCode==4的时候代表修 改了用户,或者删除了用户,其他条件代表数据没有变化 ifififif(deleteId!=nullnullnullnull){ deleteId.clear(); } ifififif(resultCode==3||resultCode==4){ DBHelperhelper=newnewnewnewDBHelper(thisthisthisthis); list=helper.getAllUser(privacy); adapter= newnewnewnewSimpleAdapter( thisthisthisthis, list, R.layout.listitem, newnewnewnew String[]{"imageid","name","mobilephone"}, newnewnewnew intintintint[]{R.id.user_image,R.id.tv_name,R.id.tv_mobilephone}); } ifififif(list.size()>0){ mainLinearLayout.setBackgroundDrawable(nullnullnullnull); } lv.setAdapter(adapter);//将整合好的adapter交给listview,显示给用户 看 /** - 54 - *resultCode只有3、4、5 *当等于4或者5的时候,代表由UserDetail转过来的。在转想UserDetail的时候, requestCode的值设置的是选中项的位置 */ ifififif(resultCode==3){ lv.setSelection(list.size()); }elseelseelseelse{ lv.setSelection(requestCode); } } /** *响应点击Menu按钮时的事件,用于设置底部菜单是否可见 */ @Override publicpublicpublicpublicbooleanbooleanbooleanbooleanonKeyDown(intintintintkeyCode,KeyEventevent){ ifififif(keyCode==KeyEvent.KEYCODE_MENU){ loadBottomMenu(); ifififif(bottomMenuGrid.getVisibility()==View.VISIBLE){ ifififif(searchLinearout!=nullnullnullnull&& searchLinearout.getVisibility()==View.VISIBLE){ searchLinearout.setVisibility(View.GONE); } bottomMenuGrid.setVisibility(View.GONE); }elseelseelseelse{ bottomMenuGrid.setVisibility(View.VISIBLE); } } returnreturnreturnreturnsupersupersupersuper.onKeyDown(keyCode,event); } privateprivateprivateprivatevoidvoidvoidvoidloadBottomMenu(){ ifififif(bottomMenuGrid==nullnullnullnull){ bottomMenuGrid=(GridView) findViewById(R.id.gv_buttom_menu); bottomMenuGrid.setBackgroundResource(R.drawable.channelgallery_bg) ;//设置背景 bottomMenuGrid.setNumColumns(5);//设置每行列数 bottomMenuGrid.setGravity(Gravity.CENTER);//位置居中 - 55 - bottomMenuGrid.setVerticalSpacing(10);//垂直间隔 bottomMenuGrid.setHorizontalSpacing(10);//水平间隔 bottomMenuGrid.setAdapter(getMenuAdapter(bottom_menu_itemName, bottom_menu_itemSource));//设置菜单Adapter /**监听底部菜单选项 **/ bottomMenuGrid.setOnItemClickListener(newnewnewnew OnItemClickListener(){ publicpublicpublicpublicvoidvoidvoidvoidonItemClick(AdapterViewarg0,Viewarg1, intintintintarg2, longlonglonglongarg3){ switchswitchswitchswitch(arg2){ casecasecasecase0:{ ifififif(searchLinearout!=nullnullnullnull&& searchLinearout.getVisibility()==View.VISIBLE){ searchLinearout.setVisibility(View.GONE); } ifififif(bottomMenuGrid.getVisibility()== View.VISIBLE){ bottomMenuGrid.setVisibility(View.GONE); } Intentintent=newnewnewnew Intent(MainPrivacy.thisthisthisthis,AddNew.classclassclassclass); intent.putExtra("privacy",1); startActivityForResult(intent,3); breakbreakbreakbreak; } casecasecasecase1: loadSearchLinearout(); ifififif(searchLinearout.getVisibility()==View.VISIBLE){ searchLinearout.setVisibility(View.GONE); }elseelseelseelse{ searchLinearout.setVisibility(View.VISIBLE); et_search.requestFocus(); et_search.selectAll(); } breakbreakbreakbreak; casecasecasecase2: ifififif(searchLinearout!=nullnullnullnull&& - 56 - searchLinearout.getVisibility()==View.VISIBLE){ searchLinearout.setVisibility(View.GONE); } ifififif(deleteId==nullnullnullnull||deleteId.size()==0){ Toast.makeText(MainPrivacy.thisthisthisthis,"没有 标记任何记录\n长按一条记录即可标记",Toast.LENGTH_LONG).show(); }elseelseelseelse{ newnewnewnewAlertDialog.Builder(MainPrivacy.thisthisthisthis) .setTitle("确定要删除标记的 "+deleteId.size()+"条记录吗?") .setPositiveButton("确定",newnewnewnew DialogInterface.OnClickListener(){ @Override publicpublicpublicpublicvoidvoidvoidvoidonClick(DialogInterface dialog,intintintintwhich){ DBHelperhelper=newnewnewnew DBHelper(MainPrivacy.thisthisthisthis); helper.deleteMarked(deleteId); //重置视图 list=helper.getAllUser(privacy); adapter= newnewnewnewSimpleAdapter( MainPrivacy.thisthisthisthis, list, R.layout.listitem, newnewnewnew String[]{"imageid","name","mobilephone"}, newnewnewnew intintintint[]{R.id.user_image,R.id.tv_name,R.id.tv_mobilephone}); lv.setAdapter(adapter); deleteId.clear(); } }) .setNegativeButton("取消",nullnullnullnull) .create() .show(); } breakbreakbreakbreak; casecasecasecase3: ifififif(searchLinearout!=nullnullnullnull&& searchLinearout.getVisibility()==View.VISIBLE){ searchLinearout.setVisibility(View.GONE); } - 57 - loadMainMenuDialog(); mainMenuDialog.show(); breakbreakbreakbreak; casecasecasecase4: finish(); breakbreakbreakbreak; } } }); } } privateprivateprivateprivatevoidvoidvoidvoidloadMainMenuDialog(){ ifififif(mainMenuDialog==nullnullnullnull){ LayoutInflaterli=LayoutInflater.from(thisthisthisthis); mainMenuView=li.inflate(R.layout.main_menu_grid,nullnullnullnull); //根据主菜单视图,创建主菜单对话框 mainMenuDialog=newnewnewnew AlertDialog.Builder(thisthisthisthis).setView(mainMenuView).create(); //根据主菜单视图,拿到视图文件中的GridView,然后再往里面放Adapter mainMenuGrid= (GridView)mainMenuView.findViewById(R.id.gridview); SimpleAdaptermenuAdapter= getMenuAdapter(main_menu_itemName,main_menu_itemSource); mainMenuGrid.setAdapter(menuAdapter); //响应点击事件 mainMenuGrid.setOnItemClickListener(newnewnewnew OnItemClickListener(){ @Override publicpublicpublicpublicvoidvoidvoidvoidonItemClick(AdapterViewarg0,Viewarg1, intintintintarg2, longlonglonglongarg3){ switchswitchswitchswitch(arg2){ casecasecasecase0:{ DBHelperhelper=newnewnewnew DBHelper(MainPrivacy.thisthisthisthis); list=helper.getAllUser(privacy); adapter=newnewnewnewSimpleAdapter( MainPrivacy.thisthisthisthis, list, R.layout.listitem, newnewnewnew - 58 - String[]{"imageid","name","mobilephone"}, newnewnewnew intintintint[]{R.id.user_image,R.id.tv_name,R.id.tv_mobilephone}); lv.setAdapter(adapter);//显示所有数据 mainMenuDialog.dismiss(); breakbreakbreakbreak; } casecasecasecase1:{ mainMenuDialog.dismiss(); AlertDialog.Builderbuilder=newnewnewnew AlertDialog.Builder(MainPrivacy.thisthisthisthis); confirmDialog=builder.create(); builder.setTitle("是否删除所有!?"); builder.setPositiveButton("确定",newnewnewnew DialogInterface.OnClickListener(){ @Override publicpublicpublicpublicvoidvoidvoidvoidonClick(DialogInterface dialog,intintintintwhich){ DBHelperhelper=newnewnewnew DBHelper(MainPrivacy.thisthisthisthis); helper.deleteAll(1); list=helper.getAllUser(privacy); adapter=newnewnewnewSimpleAdapter( MainPrivacy.thisthisthisthis, list, R.layout.listitem, newnewnewnew String[]{"imageid","name","mobilephone"}, newnewnewnew intintintint[]{R.id.user_image,R.id.tv_name,R.id.tv_mobilephone}); lv.setAdapter(adapter);//显示所有数 据 } }); builder.setNegativeButton("取消",newnewnewnew DialogInterface.OnClickListener(){ @Override publicpublicpublicpublicvoidvoidvoidvoidonClick(DialogInterface dialog,intintintintwhich){ confirmDialog.dismiss(); } - 59 - }); builder.create().show(); breakbreakbreakbreak; } casecasecasecase2:{ mainMenuDialog.dismiss(); newnewnewnewAlertDialog.Builder(MainPrivacy.thisthisthisthis) .setTitle("是否需要备份记录到SD卡?") .setPositiveButton("确定",newnewnewnew DialogInterface.OnClickListener(){ @Override publicpublicpublicpublicvoidvoidvoidvoidonClick(DialogInterfacedialog, intintintintwhich){ LayoutInflaterli= LayoutInflater.from(MainPrivacy.thisthisthisthis); Viewbackup_view= li.inflate(R.layout.backup_progress,nullnullnullnull); progressDialog=newnewnewnew AlertDialog.Builder(MainPrivacy.thisthisthisthis) .setTitle("备份正在进行中...") .setView(backup_view) .create(); progressDialog.show(); DBHelperhelper=newnewnewnew DBHelper(MainPrivacy.thisthisthisthis); helper.backupData(privacy); ProgressBarbar=(ProgressBar) backup_view.findViewById(R.id.pb_backup); Buttonbtn_backup_ok= (Button)backup_view.findViewById(R.id.btn_backuup_ok); bar.setMax(list.size()); forforforfor(intintintinti=0;i<=list.size();i++){ bar.setProgress(i); } progressDialog.setTitle("备份完成!一共 "+ list.size()+"条记录"); btn_backup_ok.setVisibility(View.VISIBLE); btn_backup_ok.setOnClickListener(newnewnewnew OnClickListener(){ @Override publicpublicpublicpublicvoidvoidvoidvoidonClick(Viewv){ progressDialog.dismiss(); - 60 - mainMenuDialog.dismiss(); } }); } }) .setNegativeButton("取消",nullnullnullnull) .create() .show(); breakbreakbreakbreak; } casecasecasecase3:{ LayoutInflaterli= LayoutInflater.from(MainPrivacy.thisthisthisthis); ViewenterFileNameView= li.inflate(R.layout.enterfilename,nullnullnullnull); enterFileNameDialog=newnewnewnew AlertDialog.Builder(MainPrivacy.thisthisthisthis) .setView(enterFileNameView).setNegativeButton("取消",nullnullnullnull) .setPositiveButton("确定",newnewnewnew DialogInterface.OnClickListener(){ @Override publicpublicpublicpublicvoidvoidvoidvoidonClick(DialogInterfacedialog, intintintintwhich){ DBHelperhelper=newnewnewnew DBHelper(MainPrivacy.thisthisthisthis); fileName= et_enter_file_name.getText().toString(); ifififif(helper.findFile(fileName)){ newnewnewnew AlertDialog.Builder(MainPrivacy.thisthisthisthis).setTitle("请选择方式") .setPositiveButton("覆盖",newnewnewnew DialogInterface.OnClickListener(){ @Override publicpublicpublicpublicvoidvoidvoidvoid onClick(DialogInterfacedialog,intintintintwhich){ DBHelperhelper=newnewnewnew - 61 - DBHelper(MainPrivacy.thisthisthisthis); helper.deleteAll(1); helper.restoreData(fileName); list= helper.getAllUser(privacy); adapter=newnewnewnew SimpleAdapter(MainPrivacy.thisthisthisthis, list, R.layout.listitem, newnewnewnew String[]{"imageid","name","mobilephone"}, newnewnewnew intintintint[]{R.id.user_image,R.id.tv_name,R.id.tv_mobilephone}); LayoutInflaterli= LayoutInflater.from(MainPrivacy.thisthisthisthis); Viewbackup_view= li.inflate(R.layout.backup_progress,nullnullnullnull); progressDialog=newnewnewnew AlertDialog.Builder(MainPrivacy.thisthisthisthis) .setTitle("正在还原数据...") .setView(backup_view) .create(); progressDialog.show(); ProgressBarbar= (ProgressBar)backup_view.findViewById(R.id.pb_backup); Buttonbtn_backup_ok= (Button)backup_view.findViewById(R.id.btn_backuup_ok); bar.setMax(list.size()); forforforfor(intintintint i=0;i<=list.size();i++){ bar.setProgress(i); } progressDialog.setTitle("还 原完成!一共还原了 "+list.size()+"条记录"); btn_backup_ok.setVisibility(View.VISIBLE); btn_backup_ok.setOnClickListener(newnewnewnewOnClickListener(){ @Override publicpublicpublicpublicvoidvoidvoidvoidonClick(View v){ - 62 - progressDialog.dismiss(); mainMenuDialog.dismiss(); lv.setAdapter(adapter); } }); } }) .setNegativeButton("添加",newnewnewnew DialogInterface.OnClickListener(){ @Override publicpublicpublicpublicvoidvoidvoidvoid onClick(DialogInterfacedialog,intintintintwhich){ DBHelperhelper=newnewnewnew DBHelper(MainPrivacy.thisthisthisthis); intintintintpreNum=list.size(); helper.restoreData(fileName); list= helper.getAllUser(privacy); adapter=newnewnewnew SimpleAdapter(MainPrivacy.thisthisthisthis, list, R.layout.listitem, newnewnewnew String[]{"imageid","name","mobilephone"}, newnewnewnew intintintint[]{R.id.user_image,R.id.tv_name,R.id.tv_mobilephone}); LayoutInflaterli= LayoutInflater.from(MainPrivacy.thisthisthisthis); Viewbackup_view= li.inflate(R.layout.backup_progress,nullnullnullnull); progressDialog=newnewnewnew AlertDialog.Builder(MainPrivacy.thisthisthisthis) .setTitle("正在还原数据...") .setView(backup_view) .create(); progressDialog.show(); ProgressBarbar= - 63 - (ProgressBar)backup_view.findViewById(R.id.pb_backup); Buttonbtn_backup_ok= (Button)backup_view.findViewById(R.id.btn_backuup_ok); bar.setMax(list.size()); forforforfor(intintintint i=0;i<=list.size();i++){ bar.setProgress(i); } progressDialog.setTitle("还 原完成!一共还原了 "+(list.size()-preNum)+"条记录"); btn_backup_ok.setVisibility(View.VISIBLE); btn_backup_ok.setOnClickListener(newnewnewnewOnClickListener(){ @Override publicpublicpublicpublicvoidvoidvoidvoidonClick(View v){ progressDialog.dismiss(); mainMenuDialog.dismiss(); ifififif(list.size()!=0) { mainLinearLayout.setBackgroundDrawable(nullnullnullnull); } lv.setAdapter(adapter); } }); } }) .setNeutralButton("取消",newnewnewnew DialogInterface.OnClickListener(){ @Override publicpublicpublicpublicvoidvoidvoidvoid onClick(DialogInterfacedialog,intintintintwhich){ } }).create().show(); - 64 - }elseelseelseelse{ Toast.makeText(enterFileNameDialog.getContext(),"找不到备份文件", Toast.LENGTH_LONG).show(); } } }) .create(); et_enter_file_name= (EditText)enterFileNameView.findViewById(R.id.et_enter_file_name); et_enter_file_name.setText("priv_data"); et_enter_file_name.requestFocus(); et_enter_file_name.selectAll(); enterFileNameDialog.show(); adapter=newnewnewnewSimpleAdapter( MainPrivacy.thisthisthisthis, list, R.layout.listitem, newnewnewnew String[]{"imageid","name","mobilephone"}, newnewnewnew intintintint[]{R.id.user_image,R.id.tv_name,R.id.tv_mobilephone}); lv.setAdapter(adapter);//显示所有数据 mainMenuDialog.dismiss(); breakbreakbreakbreak; } casecasecasecase4:{ finish(); breakbreakbreakbreak; } casecasecasecase5:{ mainMenuDialog.dismiss(); breakbreakbreakbreak; } } }}); } } - 65 - privateprivateprivateprivatevoidvoidvoidvoidloadSearchLinearout(){ ifififif(searchLinearout==nullnullnullnull){ searchLinearout=(LinearLayout) findViewById(R.id.ll_search); et_search=(EditText)findViewById(R.id.et_search); et_search.setOnKeyListener(newnewnewnewOnKeyListener(){ @Override publicpublicpublicpublicbooleanbooleanbooleanbooleanonKey(Viewarg0,intintintintarg1,KeyEventarg2) { Stringcondition=et_search.getText().toString(); ifififif(condition.equals("")){ lv.setAdapter(adapter); } DBHelperhelper=newnewnewnewDBHelper(MainPrivacy.thisthisthisthis); list=helper.getUsers(condition,privacy); SimpleAdaptersearchAdapter= newnewnewnewSimpleAdapter( MainPrivacy.thisthisthisthis, list, R.layout.listitem, newnewnewnew String[]{"imageid","name","mobilephone"}, newnewnewnew intintintint[]{R.id.user_image,R.id.tv_name,R.id.tv_mobilephone}); lv.setAdapter(searchAdapter);//将整合好的adapter交给 listview,显示给用户看 ifififif(list.size()==0){ Drawablenodata_bg= getResources().getDrawable(R.drawable.nodata_bg); mainLinearLayout.setBackgroundDrawable(nodata_bg); setTitle("没有查到任何数据"); }elseelseelseelse{ setTitle("共查到 "+list.size()+"条记录"); mainLinearLayout.setBackgroundDrawable(nullnullnullnull); } returnreturnreturnreturnfalsefalsefalsefalse; }}); } - 66 - } privateprivateprivateprivateSimpleAdaptergetMenuAdapter(String[]menuNameArray, intintintint[]imageResourceArray){ ArrayList>data=newnewnewnew ArrayList>(); forforforfor(intintintinti=0;imap=newnewnewnewHashMap(); map.put("itemImage",imageResourceArray[i]); map.put("itemText",menuNameArray[i]); data.add(map); } SimpleAdaptersimperAdapter= newnewnewnewSimpleAdapter( thisthisthisthis, data, R.layout.item_menu, newnewnewnewString[]{"itemImage","itemText"}, newnewnewnewintintintint[]{R.id.item_image, R.id.item_text}); returnreturnreturnreturnsimperAdapter; } /** *当退出的时候,回收资源 */ @Override protectedprotectedprotectedprotectedvoidvoidvoidvoidonDestroy(){ ifififif(confirmDialog!=nullnullnullnull){ confirmDialog=nullnullnullnull; } ifififif(mainMenuDialog!=nullnullnullnull){ mainMenuDialog=nullnullnullnull; } ifififif(searchLinearout!=nullnullnullnull){ searchLinearout=nullnullnullnull; } ifififif(mainMenuView!=nullnullnullnull){ mainMenuView=nullnullnullnull; } ifififif(mainMenuGrid!=nullnullnullnull){ mainMenuGrid=nullnullnullnull; } ifififif(bottomMenuGrid!=nullnullnullnull){ - 67 - bottomMenuGrid=nullnullnullnull; } ifififif(adapter!=nullnullnullnull){ adapter=nullnullnullnull; } ifififif(list!=nullnullnullnull){ list=nullnullnullnull; } ifififif(lv!=nullnullnullnull){ lv=nullnullnullnull; } Toast.makeText(thisthisthisthis,"退出秘密仓库",Toast.LENGTH_LONG).show(); System.out.println("destory!!!"); supersupersupersuper.onDestroy(); } } 3.4 UserDetail.java publicpublicpublicpublicclassclassclassclassUserDetailextendsextendsextendsextendsActivityimplementsimplementsimplementsimplementsViewFactory{ EditTextet_name; EditTextet_mobilePhone; EditTextet_officePhone; EditTextet_familyPhone; EditTextet_position; EditTextet_company; EditTextet_address; EditTextet_zipCode; EditTextet_otherContact; EditTextet_email; EditTextet_remark; Buttonbtn_save; Buttonbtn_return; Buttonbtn_delete; //头像的按钮 ImageButtonimageButton; //用flag来判断按钮的状态 false表示查看点击修改状态 true表示点击修改保存 状态 booleanbooleanbooleanbooleanflag=falsefalsefalsefalse; booleanbooleanbooleanbooleanimageChanged=falsefalsefalsefalse; - 68 - booleanbooleanbooleanbooleanisDataChanged=falsefalsefalsefalse; intintintintcurrentImagePosition; intintintintpreviousImagePosition; String[]callData; //表示状态:打电话,发短信,发邮件 Stringstatus; //拥有一个user实例,这个对象由Intent传过来 Useruser; Gallerygallery; ImageSwitcheris; ViewnumChooseView; ViewimageChooseView; //号码选择的对话框 AlertDialognumChooseDialog; AlertDialogimageChooseDialog; /** *所有的图像图片 */ privateprivateprivateprivateintintintint[]images =newnewnewnewintintintint[]{R.drawable.icon ,R.drawable.image1,R.drawable.image2,R.drawable.image3 ,R.drawable.image4,R.drawable.image5,R.drawable.image6 ,R.drawable.image7,R.drawable.image8,R.drawable.image9 ,R.drawable.image10,R.drawable.image11,R.drawable.image12 ,R.drawable.image13,R.drawable.image14,R.drawable.image15 ,R.drawable.image16,R.drawable.image17,R.drawable.image18 ,R.drawable.image19,R.drawable.image20,R.drawable.image21 ,R.drawable.image22,R.drawable.image23,R.drawable.image24 ,R.drawable.image25,R.drawable.image26,R.drawable.image27 ,R.drawable.image28,R.drawable.image29,R.drawable.image30}; @Override publicpublicpublicpublicvoidvoidvoidvoidonCreate(BundlesavedInstanceState){ supersupersupersuper.onCreate(savedInstanceState); setContentView(R.layout.userdetail); //获得意图 Intentintent=getIntent(); - 69 - //从意图中得到需要的user对象 user=(User)intent.getSerializableExtra("user"); //加载数据,往控件上赋值 loadUserData(); //设置EditText不可编辑 setEditTextDisable(); //为按钮添加监听类 btn_save.setOnClickListener(newnewnewnewOnClickListener(){ @Override publicpublicpublicpublicvoidvoidvoidvoidonClick(Viewarg0){ ifififif(!flag){ btn_save.setText("保存修改"); setEditTextAble(); flag=truetruetruetrue; }elseelseelseelse{ //往数据库里面更新数据 setTitle("modify"); modify(); setEditTextDisable(); setColorToWhite(); btn_save.setText("修改"); flag=falsefalsefalsefalse; } }}); btn_return.setOnClickListener(newnewnewnewOnClickListener(){ @Override publicpublicpublicpublicvoidvoidvoidvoidonClick(Viewv){ ifififif(isDataChanged){ setResult(4); }elseelseelseelse{ setResult(5); } finish(); }}); btn_delete.setOnClickListener(newnewnewnewOnClickListener(){ @Override - 70 - publicpublicpublicpublicvoidvoidvoidvoidonClick(Viewv){ newnewnewnewAlertDialog.Builder(UserDetail.thisthisthisthis). setPositiveButton("确定",newnewnewnew DialogInterface.OnClickListener(){ @Override publicpublicpublicpublicvoidvoidvoidvoidonClick(DialogInterfacedialog,intintintintwhich) { delete(); setResult(4); finish(); } }).setNegativeButton("取消",newnewnewnew DialogInterface.OnClickListener(){ @Override publicpublicpublicpublicvoidvoidvoidvoidonClick(DialogInterfacedialog,intintintintwhich) { } }).setTitle("是否要删除?").create().show(); }}); imageButton.setOnClickListener(newnewnewnewOnClickListener(){ @Override publicpublicpublicpublicvoidvoidvoidvoidonClick(Viewv){ loadImage();//加载imageChooseView,只加载一次 initImageChooseDialog();//加载imageChooseDialog,只加载一 次 imageChooseDialog.show(); }}); } /** *获得布局文件中的控件,并且根据传递过来user对象对控件进行赋值 */ publicpublicpublicpublicvoidvoidvoidvoidloadUserData(){ - 71 - // 获得EditText控件 et_name = (EditText) findViewById(R.id.username); et_mobilePhone = (EditText) findViewById(R.id.mobilephone); et_officePhone = (EditText) findViewById(R.id.officephone); et_familyPhone = (EditText) findViewById(R.id.familyphone); et_position = (EditText) findViewById(R.id.position); et_company = (EditText) findViewById(R.id.company); et_address = (EditText) findViewById(R.id.address); et_zipCode = (EditText) findViewById(R.id.zipcode); et_otherContact = (EditText) findViewById(R.id.othercontact); et_email = (EditText) findViewById(R.id.email); et_remark = (EditText) findViewById(R.id.remark); // 获得Button控件 btn_save = (Button)findViewById(R.id.save); btn_return = (Button)findViewById(R.id.btn_return); btn_delete = (Button)findViewById(R.id.delete); imageButton = (ImageButton)findViewById(R.id.image_button); // 为控件赋值 et_name.setText(user.username); et_mobilePhone.setText(user.mobilePhone); et_familyPhone.setText(user.familyPhone); et_officePhone.setText(user.officePhone); et_company.setText(user.company); et_address.setText(user.address); et_zipCode.setText(user.zipCode); et_otherContact.setText(user.otherContact); et_email.setText(user.email); et_remark.setText(user.remark); et_position.setText(user.position); imageButton.setImageResource(user.imageId); } /** * 设置EditText为不可用 */ privateprivateprivateprivate voidvoidvoidvoid setEditTextDisable() { et_name.setEnabled(falsefalsefalsefalse); et_mobilePhone.setEnabled(falsefalsefalsefalse); et_officePhone.setEnabled(falsefalsefalsefalse); et_familyPhone.setEnabled(falsefalsefalsefalse); et_position.setEnabled(falsefalsefalsefalse); et_company.setEnabled(falsefalsefalsefalse); - 72 - et_address.setEnabled(falsefalsefalsefalse); et_zipCode.setEnabled(falsefalsefalsefalse); et_otherContact.setEnabled(falsefalsefalsefalse); et_email.setEnabled(falsefalsefalsefalse); et_remark.setEnabled(falsefalsefalsefalse); imageButton.setEnabled(falsefalsefalsefalse); setColorToWhite(); } /** *设置EditText为可用状态 */ privateprivateprivateprivatevoidvoidvoidvoidsetEditTextAble(){ et_name.setEnabled(truetruetruetrue); et_mobilePhone.setEnabled(truetruetruetrue); et_officePhone.setEnabled(truetruetruetrue); et_familyPhone.setEnabled(truetruetruetrue); et_position.setEnabled(truetruetruetrue); et_company.setEnabled(truetruetruetrue); et_address.setEnabled(truetruetruetrue); et_zipCode.setEnabled(truetruetruetrue); et_otherContact.setEnabled(truetruetruetrue); et_email.setEnabled(truetruetruetrue); et_remark.setEnabled(truetruetruetrue); imageButton.setEnabled(truetruetruetrue); setColorToBlack(); } /** *设置显示的字体颜色为黑色 */ privateprivateprivateprivatevoidvoidvoidvoidsetColorToBlack(){ et_name.setTextColor(Color.BLACK); et_mobilePhone.setTextColor(Color.BLACK); et_officePhone.setTextColor(Color.BLACK); et_familyPhone.setTextColor(Color.BLACK); et_position.setTextColor(Color.BLACK); et_company.setTextColor(Color.BLACK); et_address.setTextColor(Color.BLACK); et_zipCode.setTextColor(Color.BLACK); et_otherContact.setTextColor(Color.BLACK); et_email.setTextColor(Color.BLACK); - 73 - et_remark.setTextColor(Color.BLACK); } /** *设置显示的字体颜色为白色 */ privateprivateprivateprivatevoidvoidvoidvoidsetColorToWhite(){ et_name.setTextColor(Color.WHITE); et_mobilePhone.setTextColor(Color.WHITE); et_officePhone.setTextColor(Color.WHITE); et_familyPhone.setTextColor(Color.WHITE); et_position.setTextColor(Color.WHITE); et_company.setTextColor(Color.WHITE); et_address.setTextColor(Color.WHITE); et_zipCode.setTextColor(Color.WHITE); et_otherContact.setTextColor(Color.WHITE); et_email.setTextColor(Color.WHITE); et_remark.setTextColor(Color.WHITE); } /** *获得最新数据,创建DBHelper对象,更新数据库 */ privateprivateprivateprivatevoidvoidvoidvoidmodify(){ user.username=et_name.getText().toString(); user.address=et_address.getText().toString(); user.company=et_company.getText().toString(); user.email=et_email.getText().toString(); user.familyPhone=et_familyPhone.getText().toString(); user.mobilePhone=et_mobilePhone.getText().toString(); user.officePhone=et_officePhone.getText().toString(); user.otherContact=et_otherContact.getText().toString(); user.position=et_position.getText().toString(); user.remark=et_remark.getText().toString(); user.zipCode=et_zipCode.getText().toString(); ifififif(imageChanged){ user.imageId=images[currentImagePosition%images.length]; } DBHelperhelper=newnewnewnewDBHelper(thisthisthisthis); //打开数据库 helper.openDatabase(); helper.modify(user); isDataChanged=truetruetruetrue; - 74 - } privateprivateprivateprivatevoidvoidvoidvoiddelete(){ DBHelperhelper=newnewnewnewDBHelper(thisthisthisthis); //打开数据库 helper.openDatabase(); helper.delete(user._id); } /** *为Menu添加几个选项 */ @Override publicpublicpublicpublicbooleanbooleanbooleanbooleanonCreateOptionsMenu(Menumenu){ menu.addSubMenu(0,Menu.FIRST,1,"打电话"); menu.addSubMenu(0,Menu.FIRST+1,2,"发短信"); menu.addSubMenu(0,Menu.FIRST+2,3,"发邮件"); //为每一个Item设置图标 MenuItemitem=menu.getItem(Menu.FIRST-1); item.setIcon(R.drawable.dial); MenuItemitem1=menu.getItem(Menu.FIRST); item1.setIcon(R.drawable.send_sms); MenuItemitem2=menu.getItem(Menu.FIRST+1); item2.setIcon(R.drawable.mail); returnreturnreturnreturnsupersupersupersuper.onCreateOptionsMenu(menu); } /** *为每一个MenuItem添加事件 */ @Override publicpublicpublicpublicbooleanbooleanbooleanbooleanonMenuItemSelected(intintintintfeatureId,MenuItemitem){ switchswitchswitchswitch(item.getItemId()){ casecasecasecaseMenu.FIRST:{ //将状态设置为打电话 status=Intent.ACTION_CALL; ifififif(callData==nullnullnullnull){ //加载可用的号码 loadAvailableCallData(); } - 75 - ifififif(callData.length==0){ //提示没有可用的号码 Toast.makeText(thisthisthisthis,"没有可用的号码!", Toast.LENGTH_LONG).show(); }elseelseelseelseifififif(callData.length==1){ //如果之有一个可用的号码,这直接使用这个号码拨出 Intentintent= newnewnewnewIntent(Intent.ACTION_CALL,Uri.parse("tel://" +callData[0])); startActivity(intent); }elseelseelseelse{ //如果有2个或者2个以上号码,弹出号码选择对话框 initNumChooseDialog(); } breakbreakbreakbreak; } casecasecasecaseMenu.FIRST+1:{ status=Intent.ACTION_SENDTO; ifififif(callData==nullnullnullnull){ loadAvailableCallData(); } ifififif(callData.length==0){ //提示没有可用的号码 Toast.makeText(thisthisthisthis,"没有可用的号码!", Toast.LENGTH_LONG).show(); }elseelseelseelseifififif(callData.length==1){ //如果之后又一个可用的号码,这直接使用这个号码拨出 Intentintent= newnewnewnew Intent(Intent.ACTION_SENDTO,Uri.parse("smsto://"+callData[0])); startActivity(intent); }elseelseelseelse{ initNumChooseDialog(); } breakbreakbreakbreak; } casecasecasecaseMenu.FIRST+2:{ ifififif(user.email.equals("")){ Toast.makeText(thisthisthisthis,"没有可用的邮箱!", Toast.LENGTH_LONG).show(); }elseelseelseelse{ UriemailUri=Uri.parse("mailto:"+user.email); Intentintent=newnewnewnewIntent(Intent.ACTION_SENDTO, - 76 - emailUri); startActivity(intent); } breakbreakbreakbreak; } } returnreturnreturnreturnsupersupersupersuper.onMenuItemSelected(featureId,item); } /** *装载头像 */ publicpublicpublicpublicvoidvoidvoidvoidloadImage(){ ifififif(imageChooseView==nullnullnullnull){ LayoutInflaterli=LayoutInflater.from(UserDetail.thisthisthisthis); imageChooseView=li.inflate(R.layout.imageswitch,nullnullnullnull); gallery= (Gallery)imageChooseView.findViewById(R.id.gallery); gallery.setAdapter(newnewnewnewImageAdapter(thisthisthisthis)); gallery.setSelection(images.length/2); is= (ImageSwitcher)imageChooseView.findViewById(R.id.imageswitch); is.setFactory(thisthisthisthis); gallery.setOnItemSelectedListener(newnewnewnew OnItemSelectedListener(){ @Override publicpublicpublicpublicvoidvoidvoidvoidonItemSelected(AdapterViewarg0,Viewarg1, intintintintarg2,longlonglonglongarg3){ //TODOTODOTODOTODOAuto-generatedmethodstub currentImagePosition=arg2%images.length; is.setImageResource(images[arg2%images.length]); } @Override publicpublicpublicpublicvoidvoidvoidvoidonNothingSelected(AdapterViewarg0){ }}); } } - 77 - publicpublicpublicpublicvoidvoidvoidvoidinitNumChooseDialog(){ ifififif(numChooseDialog==nullnullnullnull){ AlertDialog.Builderbuilder=newnewnewnewAlertDialog.Builder(thisthisthisthis); LayoutInflaterinflater=LayoutInflater.from(thisthisthisthis); numChooseView=inflater.inflate(R.layout.numchoose,nullnullnullnull); ListViewlv= (ListView)numChooseView.findViewById(R.id.num_list); ArrayAdapterarray= newnewnewnew ArrayAdapter(thisthisthisthis,android.R.layout.simple_list_item_1,callData); lv.setAdapter(array); lv.setOnItemClickListener(newnewnewnewOnItemClickListener(){ @Override publicpublicpublicpublicvoidvoidvoidvoidonItemClick(AdapterViewarg0,Viewarg1, intintintintarg2, longlonglonglongarg3){ Stringnum= String.valueOf(arg0.getItemAtPosition(arg2)); Intentintent=nullnullnullnull; ifififif(status.equals(Intent.ACTION_CALL)){ intent=newnewnewnew Intent(Intent.ACTION_CALL,Uri.parse("tel://"+num)); }elseelseelseelse{ intent=newnewnewnew Intent(Intent.ACTION_SENDTO,Uri.parse("smsto://"+num)); } startActivity(intent); //对话框消失 numChooseDialog.dismiss(); }}); builder.setView(numChooseView); numChooseDialog=builder.create(); } numChooseDialog.show(); } publicpublicpublicpublicvoidvoidvoidvoidinitImageChooseDialog(){ ifififif(imageChooseDialog==nullnullnullnull){ AlertDialog.Builderbuilder=newnewnewnewAlertDialog.Builder(thisthisthisthis); builder.setTitle("请选择图像") - 78 - .setView(imageChooseView).setPositiveButton("确定",newnewnewnew DialogInterface.OnClickListener(){ @Override publicpublicpublicpublicvoidvoidvoidvoidonClick(DialogInterfacedialog,intintintintwhich){ imageChanged=truetruetruetrue; previousImagePosition=currentImagePosition; imageButton.setImageResource(images[currentImagePosition%images.l ength]); } }) .setNegativeButton("取消",newnewnewnew DialogInterface.OnClickListener(){ @Override publicpublicpublicpublicvoidvoidvoidvoidonClick(DialogInterfacedialog,intintintintwhich){ currentImagePosition=previousImagePosition; } }); imageChooseDialog=builder.create(); } } /** *装载可用的号码 */ publicpublicpublicpublicvoidvoidvoidvoidloadAvailableCallData(){ ArrayListcallNums=newnewnewnewArrayList(); ifififif(!user.mobilePhone.equals("")){ callNums.add(user.mobilePhone); } ifififif(!user.familyPhone.equals("")){ callNums.add(user.familyPhone); } ifififif(!user.officePhone.equals("")){ callNums.add(user.officePhone); } callData=newnewnewnewString[callNums.size()]; forforforfor(intintintinti=0;ideleteId){ StringBufferstrDeleteId=newnewnewnewStringBuffer(); strDeleteId.append("_id="); forforforfor(intintintinti=0;i >>>
还剩91页未读

继续阅读

下载pdf到电脑,查找使用更方便

pdf的实际排版效果,会与网站的显示效果略有不同!!

需要 10 金币 [ 分享pdf获得金币 ] 4 人已下载

下载pdf

pdf贡献者

piaoxue

贡献于2013-07-07

下载需要 10 金币 [金币充值 ]
亲,您也可以通过 分享原创pdf 来获得金币奖励!
下载pdf