最近写数据库,我就自己写了个带密码的个人通讯录,感觉sqlite特别好用,和mysql,sqlserver都一样,真是简单的关系型数据库,注意:开启数据库,cursor后一定要记得关闭close()掉,避免浪费资源。另外adapter的notifyDataSetChanged()这个方法也特别好用,就是数据库更新的时候,调用一下,baseAdapter中的getview就会重新加载一遍,这样界面就会更新数据,而不用onCreate()方法来更新,另外可以把notifyDataSetChanged()方法写在onResume()方法中,这样在两个activity跳转后按back键也可以达到刷新界面的效果!
想要源码的可以留言,有问题可以留言,同时也欢迎指正我的纰漏;
转载请标明出处:http://blog.youkuaiyun.com/wdaming1986/article/details/6727032
另:csdn下载连接地址:http://download.youkuaiyun.com/source/3555843
程序启动后输入密码的界面:第一次启动程序和点击修改密码界面:
点击确定,进入联系人列表界面: 点击menu菜单,有两个菜单键:
单击每一个列表进入修改界面:长按每一个联系人弹dialog删除记录:
下面看代码:
在com.cn.daming包下的类:
1、Login.java 程序的入口类
package com.cn.daming; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.Toast; import com.cn.daming.databases.DBOpenHelper; public class Login extends Activity { private DBOpenHelper db; private EditText et; private Button login_button; private String password; @Override protected void onCreate(Bundle bundle) { super.onCreate(bundle); db = new DBOpenHelper(this); password = db.getPwd(); db.close(); if(password.equals("")){ Intent intent = new Intent(Login.this,PasswordManage.class); startActivity(intent); finish(); return; } setContentView(R.layout.login); et = (EditText)findViewById(R.id.login_password); login_button = (Button)findViewById(R.id.note_login); login_button.setOnClickListener(new Button.OnClickListener(){ public void onClick(View v) { String input_pwd = et.getText().toString(); if(password.equals(input_pwd)){ Toast.makeText(Login.this, R.string.login_success, Toast.LENGTH_LONG).show(); Intent intent = new Intent(Login.this,MainActivity.class); intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); startActivity(intent); finish(); }else{ Toast.makeText(Login.this, R.string.error_password, Toast.LENGTH_LONG).show(); et.setText(""); } } }); } }
2、MainActivity.java类:程序listView列表显示类
3、PasswordManage.java,密码设置类:
4、DetailContantsActivity。java类,每一条记录,联系人的类:
在com.cn.daming.databases包下面的类:
5、DBOpenHelper。java,数据库的类:
在com.cn.daming.adapter包下的类:
6、ContantsAdapter。java,适配器类:
package com.cn.daming.adapter; import java.util.List; import android.content.Context; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.TextView; import com.cn.daming.R; public class ContantsAdapter extends BaseAdapter { private List<String> names; private List<String> phones; private LayoutInflater inflater; private Context context; public ContantsAdapter(Context context,List<String> names ,List<String> phones){ inflater = LayoutInflater.from(context); this.names = names; this.phones = phones; this.context = context; } public int getCount() { return names.size(); } public Object getItem(int position) { return names.get(position); } public long getItemId(int position) { return position; } public View getView(int position, View view, ViewGroup group) { ContantsHolder holder = new ContantsHolder(); if(view==null){ view = inflater.inflate(R.layout.contants_list_view, null); holder.contansName = (TextView)view.findViewById(R.id.name_textview); holder.contantsPhone = (TextView)view.findViewById(R.id.phone_textview); view.setTag(holder); }else{ holder = (ContantsHolder)view.getTag(); } holder.contansName.setText(names.get(position)); holder.contantsPhone.setText(phones.get(position)); return view; } public class ContantsHolder { private TextView contansName; private TextView contantsPhone; } }
布局文件
1、login。xml布局文件:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:paddingTop="10dip" android:orientation="vertical" android:background="@drawable/background" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/please_input_password" android:textSize="24dip" android:textStyle="bold" android:textColor="@drawable/black" android:layout_gravity="center" /> <EditText android:id="@+id/login_password" android:layout_width="250dip" android:layout_height="wrap_content" android:password="true" android:layout_gravity="center" android:layout_marginTop="15dip" /> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:layout_marginTop="15dip" > <Button android:id="@+id/note_login" android:layout_width="100dip" android:layout_height="wrap_content" android:text="@string/ok" /> </LinearLayout> </LinearLayout>2、contants_detail.xml布局文件
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="wrap_content" android:background="@drawable/background" > <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" > <!-- 显示联系人姓名线性布局 --> <TextView android:layout_width="100px" android:layout_height="wrap_content" android:textSize="18px" android:textColor="@color/text" android:layout_gravity="left|center_vertical" android:text="@string/contantsName" /> <EditText android:id="@+id/etName" android:layout_width="fill_parent" android:layout_height="wrap_content" /> </LinearLayout> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="wrap_content" > <!-- 显示联系人固定电话的线性布局 --> <TextView android:layout_width="100px" android:layout_height="wrap_content" android:textSize="18px" android:textColor="@color/text" android:layout_gravity="center_vertical|left" android:text="@string/contantsPhone" /> <EditText android:id="@+id/etPhone" android:layout_width="fill_parent" android:layout_height="wrap_content" android:phoneNumber="true" /> </LinearLayout> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="wrap_content" > <!-- 显示联系人手机号码的线性布局 --> <TextView android:layout_width="100px" android:layout_height="wrap_content" android:textSize="18px" android:textColor="@color/text" android:layout_gravity="left|center_vertical" android:text="@string/contantsMobile" /> <EditText android:id="@+id/etMobile" android:layout_width="fill_parent" android:layout_height="wrap_content" android:phoneNumber="true" /> </LinearLayout> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="wrap_content" > <!-- 显示联系人电子邮件的线性布局 --> <TextView android:layout_width="100px" android:layout_height="wrap_content" android:textSize="18px" android:textColor="@color/text" android:layout_gravity="left|center_vertical" android:text="@string/contantsEmail" /> <EditText android:id="@+id/etEmail" android:layout_width="fill_parent" android:layout_height="wrap_content" /> </LinearLayout> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="wrap_content" > <!-- 显示联系人邮编的线性布局 --> <TextView android:layout_width="100px" android:layout_height="wrap_content" android:textSize="18px" android:textColor="@color/text" android:layout_gravity="left|center_vertical" android:text="@string/contantsPost" /> <EditText android:id="@+id/etPost" android:layout_width="fill_parent" android:layout_height="wrap_content" /> </LinearLayout> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="wrap_content" > <!-- 显示联系人通信地址的线性布局 --> <TextView android:layout_width="100px" android:layout_height="wrap_content" android:textSize="18px" android:textColor="@color/text" android:layout_gravity="left|center_vertical" android:text="@string/contantsAddr" /> <EditText android:id="@+id/etAddr" android:layout_width="fill_parent" android:layout_height="wrap_content" /> </LinearLayout> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="wrap_content" > <!-- 显示联系人公司的线性布局 --> <TextView android:layout_width="100px" android:layout_height="wrap_content" android:textSize="18px" android:textColor="@color/text" android:layout_gravity="left|center_vertical" android:text="@string/contantsComp" /> <EditText android:id="@+id/etComp" android:layout_width="fill_parent" android:layout_height="wrap_content" /> </LinearLayout> <ImageButton android:id="@+id/detailSave" android:layout_width="fill_parent" android:layout_height="wrap_content" android:src="@drawable/save" /> </LinearLayout>3、contants_list_view.xml,每一个list的item布局文件
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="horizontal"> <TextView android:id="@+id/name_textview" android:layout_width="150dip" android:layout_height="wrap_content" android:layout_marginRight="10dip" android:textColor="#000000" android:textSize="10pt" /> <TextView android:id="@+id/phone_textview" android:layout_width="165dip" android:layout_height="wrap_content" android:textColor="#000000" android:textSize="10pt" /> </LinearLayout>4、main.xml布局文件
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="@drawable/background" > <LinearLayout android:orientation="horizontal" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:text="@string/title" android:textSize="24px" android:textColor="@color/text" /> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/title" /> </LinearLayout> <ListView android:id="@+id/db_listview" android:layout_width="fill_parent" android:layout_height="fill_parent" android:choiceMode="singleChoice" /> </LinearLayout>5、password_manage.xml布局文件
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="vertical" android:background="@drawable/background" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:textStyle="bold" android:textSize="26dip" android:padding="6dip" android:paddingBottom="10dip" android:textColor="@drawable/black" android:text="@string/password_manage" /> <TableLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_marginTop="20dip" android:layout_marginLeft="10dip" > <TableRow android:layout_width="fill_parent" android:layout_height="50dip" > <TextView android:layout_width="wrap_content" android:layout_height="fill_parent" android:text="@string/new_password" android:layout_gravity="right" android:gravity="center" android:textColor="#000000" android:textSize="11pt" /> <EditText android:id="@+id/new_password" android:layout_width="180dip" android:layout_height="wrap_content" android:password="true" android:layout_marginLeft="20dip" /> </TableRow> <TableRow android:layout_width="fill_parent" android:layout_height="50dip" android:layout_marginTop="10dip" android:gravity="center_vertical" > <TextView android:layout_width="wrap_content" android:layout_height="fill_parent" android:text="@string/repeat_password" android:layout_gravity="right" android:gravity="center_vertical" android:textColor="#000000" android:textSize="11pt" /> <EditText android:id="@+id/repeat_password" android:layout_width="180dip" android:layout_height="wrap_content" android:password="true" android:layout_marginLeft="20dip" /> </TableRow> </TableLayout> <LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:gravity="center" android:layout_marginTop="20dip" > <Button android:id="@+id/save_password" android:layout_width="80dip" android:layout_height="wrap_content" android:text="@string/ok" android:textSize="16dip" /> </LinearLayout> </LinearLayout>values下的文件
1、string。xml文件
<?xml version="1.0" encoding="utf-8"?> <resources> <string name="hello">Hello World, MainActivity!</string> <string name="app_name">ContantsApp</string> <string name="please_input_password">请输入大明通讯录密码:</string> <string name="ok">确定</string> <string name="login_success">恭喜,登陆成功!</string> <string name="error_password">您输入的密码错误,请重新输入!</string> <string name="password_manage">大明通讯录密码管理:</string> <string name="new_password">新密码:</string> <string name="repeat_password">再一次:</string> <string name="password_is_null">输入密码为空,请输入密码!</string> <string name="password_set_success">密码设置成功!</string> <string name="password_change_success">密码修改成功!</string> <string name="password_isnot_equal">两次输入的密码不一致,请重新输入密码!</string> <string name="title">大明通讯录列表</string> <string name="not_dbcursor_values">数据库中没有记录,请点击菜单新建通讯录!</string> <string name="contantsName">姓名:</string> <string name="contantsPhone">固定电话:</string> <string name="contantsMobile">移动电话:</string> <string name="contantsEmail">电子邮件:</string> <string name="contantsPost">邮政编码:</string> <string name="contantsAddr">通讯地址:</string> <string name="contantsComp">公司地址:</string> <string name="menu_add">增加</string> <string name="menu_change">修改密码</string> </resources>2、color。xml文件
<?xml version="1.0" encoding="utf-8"?> <resources> <drawable name="white">#FFFFFF</drawable> <drawable name="black">#FF000000</drawable> <drawable name="dackgray">#666666</drawable> <drawable name="red">#FFFF0000</drawable> <drawable name="blue">#FF0000FF</drawable> <drawable name="yellow">#FFFFFF00</drawable> <drawable name="green">#FF00FF00</drawable> <color name="text">#000000</color> <drawable name="changshise">#FFD19275</drawable> </resources>AndroidManifest.xml:
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.cn.daming" android:versionCode="1" android:versionName="1.0"> <uses-sdk android:minSdkVersion="8" /> <application android:icon="@drawable/icon" android:label="@string/app_name"> <activity android:name=".Login" android:label="@string/app_name"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <activity android:name=".MainActivity"></activity> <activity android:name=".PasswordManage"></activity> <activity android:name=".DetailContantsActivity"></activity> </application> </manifest>