Android 个人通讯录

本文介绍了一个使用SQLite实现的带密码保护的个人通讯录应用程序。文章分享了如何利用SQLite进行数据库操作,并强调了资源管理的重要性。同时介绍了adapter的notifyDataSetChanged()方法在数据更新时的应用技巧。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 最近写数据库,我就自己写了个带密码的个人通讯录,感觉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 程序的入口类

[java]  view plain copy print ?
  1. package com.cn.daming;  
  2.   
  3. import android.app.Activity;  
  4. import android.content.Intent;  
  5. import android.os.Bundle;  
  6. import android.view.View;  
  7. import android.widget.Button;  
  8. import android.widget.EditText;  
  9. import android.widget.Toast;  
  10.   
  11. import com.cn.daming.databases.DBOpenHelper;  
  12.   
  13. public class Login extends Activity {  
  14.   
  15.     private DBOpenHelper db;  
  16.     private EditText et;  
  17.     private Button login_button;  
  18.     private String password;  
  19.       
  20.     @Override  
  21.     protected void onCreate(Bundle bundle) {  
  22.         super.onCreate(bundle);  
  23.           
  24.         db = new DBOpenHelper(this);  
  25.         password = db.getPwd();  
  26.         db.close();  
  27.         if(password.equals("")){  
  28.             Intent intent = new Intent(Login.this,PasswordManage.class);  
  29.             startActivity(intent);  
  30.             finish();  
  31.             return;  
  32.         }  
  33.         setContentView(R.layout.login);  
  34.           
  35.         et = (EditText)findViewById(R.id.login_password);  
  36.         login_button = (Button)findViewById(R.id.note_login);  
  37.           
  38.         login_button.setOnClickListener(new Button.OnClickListener(){  
  39.       
  40.             public void onClick(View v) {  
  41.                 String input_pwd = et.getText().toString();  
  42.                 if(password.equals(input_pwd)){  
  43.                     Toast.makeText(Login.this, R.string.login_success, Toast.LENGTH_LONG).show();  
  44.                     Intent intent = new Intent(Login.this,MainActivity.class);  
  45.                     intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);  
  46.                     startActivity(intent);  
  47.                     finish();  
  48.                 }else{  
  49.                     Toast.makeText(Login.this, R.string.error_password, Toast.LENGTH_LONG).show();  
  50.                     et.setText("");  
  51.                 }  
  52.             }  
  53.               
  54.         });  
  55.     }  
  56. }  


2、MainActivity.java类:程序listView列表显示类

[java]  view plain copy print ?
  1. package com.cn.daming;  
  2.   
  3. import java.util.ArrayList;  
  4. import java.util.List;  
  5.   
  6. import android.app.Activity;  
  7. import android.app.AlertDialog;  
  8. import android.content.DialogInterface;  
  9. import android.content.Intent;  
  10. import android.database.Cursor;  
  11. import android.os.Bundle;  
  12. import android.view.Menu;  
  13. import android.view.MenuItem;  
  14. import android.view.View;  
  15. import android.widget.AdapterView;  
  16. import android.widget.AdapterView.OnItemClickListener;  
  17. import android.widget.AdapterView.OnItemLongClickListener;  
  18. import android.widget.ListView;  
  19. import android.widget.Toast;  
  20.   
  21. import com.cn.daming.adapter.ContantsAdapter;  
  22. import com.cn.daming.databases.DBOpenHelper;  
  23.   
  24. public class MainActivity extends Activity {  
  25.   
  26.     private DBOpenHelper dbOpenHelper;  
  27.     private ContantsAdapter contantsAdapter;  
  28.     private ListView dbListView;  
  29.     private Cursor cursor;  
  30.       
  31.     final int MENU_ADD = Menu.FIRST;  
  32.     final int MENU_CHANGE = Menu.FIRST+1;  
  33.       
  34.     private List<String> ids;  
  35.     private List<String> names;  
  36.     private List<String> phones;  
  37.     AlertDialog dialog;  
  38.     @Override  
  39.     public void onCreate(Bundle savedInstanceState) {  
  40.         super.onCreate(savedInstanceState);  
  41.         setContentView(R.layout.main);  
  42.         dbOpenHelper = new DBOpenHelper(this);  
  43.         dbListView = (ListView)findViewById(R.id.db_listview);  
  44.         refreshDBOpenHelper();  
  45.   
  46.         dbListView.setOnItemClickListener(new OnItemClickListener(){  
  47.   
  48.             public void onItemClick(AdapterView<?> arg0, View arg1, int position,  
  49.                     long arg3) {  
  50.                 Intent intent= new Intent();  
  51.                 intent.putExtra("cmd"0);    
  52.                 String contants_id = ids.get(position);//0代表查询联系人,1代表添加联系人  
  53.                 intent.putExtra("id", contants_id);  
  54.                 intent.setClass(MainActivity.this, DetailContantsActivity.class);  
  55.                 startActivity(intent);  
  56.             }  
  57.         });  
  58.           
  59.         dbListView.setOnItemLongClickListener(new OnItemLongClickListener(){  
  60.   
  61.             public boolean onItemLongClick(AdapterView<?> arg0, View arg1,  
  62.                     final int position, long arg3) {  
  63.                 dialog = new AlertDialog.Builder(MainActivity.this)  
  64.                 .setTitle("提示!!")  
  65.                 .setMessage("确定要删除这条记录?")  
  66.                 .setPositiveButton("确定",  
  67.                  new DialogInterface.OnClickListener()  
  68.                  {  
  69.                      public void onClick(DialogInterface dialog, int whichButton)  
  70.                      {  
  71.                          String contants_id = ids.get(position);  
  72.                          dbOpenHelper.deleteContants(contants_id);  
  73.                          dbOpenHelper.close();  
  74.                          Toast.makeText(MainActivity.this"正在删除数据库,请稍后。。。", Toast.LENGTH_LONG).show();  
  75.                          refreshDBOpenHelper();  
  76.                          contantsAdapter.notifyDataSetChanged();  
  77.                      }  
  78.                      })  
  79.                  .setNegativeButton("取消",new DialogInterface.OnClickListener()  
  80.                  {  
  81.                       public void onClick(DialogInterface dialog, int whichButton)  
  82.                       {  
  83.                          dialog.dismiss();  
  84.                       }  
  85.                   }).show();  
  86.                 return false;  
  87.             }  
  88.         });  
  89.     }  
  90.       
  91.     @Override  
  92.     protected void onResume() {  
  93.         refreshDBOpenHelper();    
  94.         contantsAdapter.notifyDataSetChanged();  
  95.         super.onResume();  
  96.     }  
  97.   
  98.     public void refreshDBOpenHelper(){  
  99.         cursor = dbOpenHelper.selectContants();  
  100.         ids = new ArrayList<String>();  
  101.         names = new ArrayList<String>();  
  102.         phones = new ArrayList<String>();  
  103.           
  104.         int count = cursor.getCount();  
  105.         if(count>0){  
  106.             for(int i=0;i<count;i++){  
  107.                 cursor.moveToPosition(i);  
  108.                 ids.add(cursor.getString(0));  
  109.                 names.add(cursor.getString(1));  
  110.                 phones.add(cursor.getString(2));  
  111.             }  
  112.         }else{  
  113.             Toast.makeText(this, R.string.not_dbcursor_values, Toast.LENGTH_SHORT).show();  
  114.         }  
  115.         contantsAdapter = new ContantsAdapter(this,names,phones);  
  116.         dbListView.setAdapter(contantsAdapter);  
  117.         cursor.close();  
  118.         dbOpenHelper.close();  
  119.     }  
  120.       
  121.     @Override  
  122.     public boolean onCreateOptionsMenu(Menu menu) {  
  123.         menu.add(0, MENU_ADD, 0, R.string.menu_add)  
  124.             .setIcon(R.drawable.add);   //add - add_button   
  125.         menu.add(0, MENU_CHANGE, 0, R.string.menu_change)  
  126.             .setIcon(R.drawable.modify);        //add -add_change_password  
  127.         return super.onCreateOptionsMenu(menu);  
  128.     }  
  129.     @Override  
  130.     public boolean onOptionsItemSelected(MenuItem item) {  
  131.         switch(item.getItemId()){       //  
  132.         case MENU_ADD:          //press change add button  
  133.             Intent add_intent= new Intent(this,DetailContantsActivity.class);  
  134.             add_intent.putExtra("cmd"1);  
  135.             startActivity(add_intent);  
  136.             break;  
  137.         case MENU_CHANGE:   //press change password button  
  138.             Intent change_password_intent = new Intent(MainActivity.this,PasswordManage.class);  
  139.             startActivityForResult(change_password_intent,0);  
  140.             break;  
  141.         }  
  142.         return super.onOptionsItemSelected(item);  
  143.     }  
  144. }  


3、PasswordManage.java,密码设置类:

[java]  view plain copy print ?
  1. package com.cn.daming;  
  2.   
  3. import com.cn.daming.databases.DBOpenHelper;  
  4.   
  5. import android.app.Activity;  
  6. import android.content.Intent;  
  7. import android.os.Bundle;  
  8. import android.view.View;  
  9. import android.widget.Button;  
  10. import android.widget.EditText;  
  11. import android.widget.Toast;  
  12.   
  13. public class PasswordManage extends Activity {  
  14.       
  15.     private EditText et1;  
  16.     private EditText et2;  
  17.     private Button button;  
  18.     private DBOpenHelper db;  
  19.     private int CHANGE_PWD_SUCCESS = 100;  
  20.       
  21.     public void onCreate(Bundle bundle){  
  22.         super.onCreate(bundle);  
  23.         db = new DBOpenHelper(this);  
  24.         setContentView(R.layout.password_manage);  
  25.           
  26.         et1 = (EditText)findViewById(R.id.new_password);  
  27.         et2 = (EditText)findViewById(R.id.repeat_password);  
  28.         button = (Button)findViewById(R.id.save_password);  
  29.           
  30.         button.setOnClickListener(new Button.OnClickListener(){  
  31.   
  32.             public void onClick(View v) {  
  33.                 String new_pwd = et1.getText().toString();  
  34.                 String repeat_pwd = et2.getText().toString();  
  35.                 if(new_pwd==null||new_pwd.equals("")){  
  36.                     Toast.makeText(PasswordManage.this, R.string.password_is_null, Toast.LENGTH_LONG).show();  
  37.                 }else{  
  38.                     if(new_pwd.equals(repeat_pwd)){  
  39.                         if(db.getPwd().equals("")){  
  40.                             db.insertPwd(new_pwd);  
  41.                             Toast.makeText(PasswordManage.this, R.string.password_set_success, Toast.LENGTH_LONG).show();  
  42.                             toNoteList();  
  43.                         }else{  
  44.                             db.updatePwd(new_pwd);  
  45.                             Toast.makeText(PasswordManage.this, R.string.password_change_success, Toast.LENGTH_LONG).show();  
  46.                             PasswordManage.this.setResult(CHANGE_PWD_SUCCESS);  
  47.                             PasswordManage.this.finish();  
  48.                         }  
  49.                     }else{  
  50.                         Toast.makeText(PasswordManage.this, R.string.password_isnot_equal, Toast.LENGTH_LONG).show();  
  51.                     }  
  52.                 }  
  53.             }  
  54.         });  
  55.     }  
  56.       
  57.     public void toNoteList(){  
  58.         Toast.makeText(PasswordManage.this, R.string.password_set_success, Toast.LENGTH_LONG);  
  59.         Intent intent = new Intent(PasswordManage.this,MainActivity.class);  
  60.         intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);  
  61.         startActivity(intent);  
  62.         finish();  
  63.     }  
  64.   
  65. }  


4、DetailContantsActivity。java类,每一条记录,联系人的类:

 

[java]  view plain copy print ?
  1. package com.cn.daming;  
  2.   
  3. import android.app.Activity;  
  4. import android.content.ContentValues;  
  5. import android.content.Intent;  
  6. import android.database.Cursor;  
  7. import android.database.sqlite.SQLiteDatabase;  
  8. import android.os.Bundle;  
  9. import android.view.View;  
  10. import android.view.View.OnClickListener;  
  11. import android.widget.EditText;  
  12. import android.widget.ImageButton;  
  13. import android.widget.Toast;  
  14.   
  15. import com.cn.daming.databases.DBOpenHelper;  
  16.   
  17. public class DetailContantsActivity extends Activity{  
  18.   
  19.  Cursor cursor;  
  20.  DBOpenHelper dbOpenHelper;  
  21.  int id = -1;       
  22.  int [] textIds ={  
  23.   R.id.etName,  
  24.   R.id.etPhone,  
  25.   R.id.etMobile,  
  26.   R.id.etEmail,  
  27.   R.id.etPost,  
  28.   R.id.etAddr,  
  29.   R.id.etComp  
  30.  };  
  31.  EditText [] textArray;  
  32.  ImageButton saveButton; //save Button  
  33.  int status = -1//0表示查看信息,1表示添加联系人,2表示修改联系人  
  34.    
  35.  @Override  
  36.  protected void onCreate(Bundle savedInstanceState) {  
  37.   super.onCreate(savedInstanceState);  
  38.   setContentView(R.layout.contants_detail);  
  39.   textArray = new EditText[textIds.length];  
  40.   for(int i=0;i<textIds.length;i++){  
  41.    textArray[i] = (EditText)findViewById(textIds[i]);  
  42.   }  
  43.   initSaveImageButton();  
  44.   dbOpenHelper = new DBOpenHelper(this);  
  45.   Intent intent = getIntent();  
  46.   status = intent.getExtras().getInt("cmd");   
  47.   switch(status){  
  48.   case 0:     
  49.    String contants_id = intent.getExtras().getString("id");    
  50.    cursor = dbOpenHelper.getContants(contants_id);  
  51.    int count = cursor.getCount();  
  52.    if(count == 0){     
  53.     Toast.makeText(this"对不起,没有找到指定的联系人!", Toast.LENGTH_LONG).show();  
  54.    }  
  55.    else{       
  56.     cursor.moveToFirst();      
  57.     textArray[0].setText(cursor.getString(1));   //设置姓名框中的内容  
  58.     textArray[1].setText(cursor.getString(2));   //设置固话框中的内容  
  59.     textArray[2].setText(cursor.getString(3));   //设置手机号码框中的内容  
  60.     textArray[3].setText(cursor.getString(4));   //设置电子邮件框中的内容  
  61.     textArray[4].setText(cursor.getString(5));   //设置电子邮件框中的内容  
  62.     textArray[5].setText(cursor.getString(6));   //设置电子邮件框中的内容  
  63.     textArray[6].setText(cursor.getString(7));   //设置电子邮件框中的内容  
  64.    }  
  65.    cursor.close();  
  66.    dbOpenHelper.close();  
  67.    break;  
  68.   case 1:     //新建详细人信息  
  69.    for(EditText et:textArray){  
  70.     et.getEditableText().clear();  //清空各个EditText控件中内容  
  71.    }  
  72.    break;  
  73.   }  
  74.  }  
  75.    
  76.  public void initSaveImageButton()  
  77.  {  
  78.   saveButton = (ImageButton)findViewById(R.id.detailSave);  
  79.   saveButton.setOnClickListener(new OnClickListener(){  
  80.   
  81.    public void onClick(View arg0) {  
  82.     String [] strArray = new String[textArray.length];  
  83.     for(int i=0;i<strArray.length;i++){  
  84.      strArray[i] = textArray[i].getText().toString().trim(); //获得用户输入的信息数组  
  85.     }  
  86.     if(strArray[0].equals("") || strArray[1].equals("")){  
  87.      Toast.makeText(DetailContantsActivity.this"对不起,请将姓名和电话填写完整!", Toast.LENGTH_LONG).show();  
  88.     }else{  
  89.      switch(status){  //判断当前的状态  
  90.      case 0:    //查询联系人详细信息时按下保存  
  91.       updateContact(strArray);  //更新联系人信息  
  92.       break;  
  93.      case 1:    //新建联系人时按下保存按钮  
  94.       insertContact(strArray);  //插入联系人信息  
  95.       break;  
  96.      }     
  97.     }  
  98.    }  
  99.   });  
  100.  }  
  101.    
  102.  public void insertContact(String [] strArray){  
  103.   long count = dbOpenHelper.insertContants(strArray);  //插入数据  
  104.   dbOpenHelper.close();  
  105.   if(count == -1){  
  106.    Toast.makeText(this"添加联系人失败!", Toast.LENGTH_LONG).show();  
  107.   }  
  108.   else{  
  109.    Toast.makeText(this"添加联系人成功!", Toast.LENGTH_LONG).show();  
  110.   }  
  111.  }  
  112.    
  113.  public void updateContact(String [] strArray){  
  114.   int count = dbOpenHelper.updateContants(id+"", strArray); //更新数据库  
  115.   dbOpenHelper.close();  
  116.   if(count == 1){  
  117.    Toast.makeText(this"修改联系人成功!", Toast.LENGTH_LONG).show();  
  118.   }  
  119.   else{  
  120.    Toast.makeText(this"修改联系人失败!", Toast.LENGTH_LONG).show();  
  121.   }  
  122.  }  
  123. }  

 

在com.cn.daming.databases包下面的类:
5、DBOpenHelper。java,数据库的类:

[java]  view plain copy print ?
  1. package com.cn.daming.databases;  
  2.   
  3. import android.content.ContentValues;  
  4. import android.content.Context;  
  5. import android.database.Cursor;  
  6. import android.database.sqlite.SQLiteDatabase;  
  7. import android.database.sqlite.SQLiteOpenHelper;  
  8.   
  9. public class DBOpenHelper extends SQLiteOpenHelper{  
  10.   
  11.     private final static String DATABASE_NAME = "personal_contacts";  
  12.     private final static int DATABASE_VERSION = 1;  
  13.     private final static String TABLE_NAME = "contants";  
  14.     public static final String ID="_id";       //ID  
  15.     public static final String NAME="name";    //名称  
  16.     public static final String PHONE="phone";  //固定电话  
  17.     public static final String MOBILE="mobile";//手机号码  
  18.     public static final String EMAIL="email";  //电子邮件地址  
  19.     public static final String POST="post";    //邮政编码  
  20.     public static final String ADDR="addr";    //通信地址  
  21.     public static final String COMP="comp";    //公司  
  22.       
  23.     public final static String LOGIN_TABLE_NAME = "contantslogin";  
  24.     public final static String LOGIN_USER = "admin";  
  25.     public final static String LOGIN_PWD = "password";  
  26.       
  27.     public DBOpenHelper(Context context) {  
  28.         super(context, DATABASE_NAME, null, DATABASE_VERSION);  
  29.     }  
  30.   
  31.     @Override  
  32.     public void onCreate(SQLiteDatabase db) {  
  33.         String sql = "create table "+TABLE_NAME+" ("  
  34.         + ID + " integer primary key autoincrement,"  
  35.         + NAME + " varchar,"  
  36.         + PHONE+" varchar,"  
  37.         + MOBILE + " varchar,"  
  38.         + EMAIL + " varchar,"  
  39.         + POST + " varchar,"  
  40.         + ADDR + " varchar,"  
  41.         + COMP + " varchar)";  
  42.         db.execSQL(sql);  
  43.           
  44.         sql = "create table "+LOGIN_TABLE_NAME+" ("  
  45.         +LOGIN_USER+" text, "  
  46.         +LOGIN_PWD+" text )";  
  47.         db.execSQL(sql);          
  48.     }  
  49.   
  50.     @Override  
  51.     public void onUpgrade(SQLiteDatabase db, int arg1, int arg2) {  
  52.         String sql = "drop table if exists "+TABLE_NAME;  
  53.         db.execSQL(sql);  
  54.   
  55.         sql = "drop table if exists "+LOGIN_TABLE_NAME;  
  56.         db.execSQL(sql);  
  57.         onCreate(db);         
  58.     }  
  59.   
  60.     public Cursor selectContants(){  
  61.         SQLiteDatabase db = this.getReadableDatabase();  
  62.         Cursor cursor = db.query(TABLE_NAME, nullnullnullnullnullnull);  
  63.         return cursor;  
  64.     }  
  65.       
  66.     public long insertContants(String[] strArray){  
  67.         SQLiteDatabase db = this.getWritableDatabase();  
  68.         ContentValues cv = new ContentValues();  
  69.         cv.put(NAME, strArray[0]);  
  70.         cv.put(PHONE, strArray[1]);  
  71.         cv.put(MOBILE, strArray[2]);  
  72.         cv.put(EMAIL, strArray[3]);   
  73.         cv.put(POST, strArray[4]);  
  74.         cv.put(ADDR, strArray[5]);  
  75.         cv.put(COMP, strArray[6]);  
  76.         return db.insert(TABLE_NAME, null, cv);  
  77.     }  
  78.       
  79.     public void deleteContants(String id){  
  80.         SQLiteDatabase db = this.getWritableDatabase();  
  81.         String where = ID+"=?";  
  82.         String[] whereValues = {id};  
  83.         db.delete(TABLE_NAME, where, whereValues);  
  84.     }  
  85.       
  86.     public int updateContants(String id,String[] strArray){  
  87.         SQLiteDatabase db = this.getWritableDatabase();  
  88.         String where = ID+"=?";  
  89.         String[] whereValues = {id};  
  90.         ContentValues cv = new ContentValues();  
  91.         cv.put(NAME, strArray[0]);  
  92.         cv.put(PHONE, strArray[1]);  
  93.         cv.put(MOBILE, strArray[2]);  
  94.         cv.put(EMAIL, strArray[3]);   
  95.         cv.put(POST, strArray[4]);  
  96.         cv.put(ADDR, strArray[5]);  
  97.         cv.put(COMP, strArray[6]);  
  98.         return db.update(TABLE_NAME, cv, where, whereValues);  
  99.     }  
  100.       
  101.     public Cursor getContants(String id){  
  102.         SQLiteDatabase db = this.getReadableDatabase();  
  103.         String where = ID+"=?";  
  104.         String[] whereValues = {id};  
  105.         Cursor cursor = db.query(TABLE_NAME, null, where, whereValues, nullnullnull);  
  106.         return cursor;  
  107.     }  
  108.       
  109.     public long insertPwd(String password){  
  110.         SQLiteDatabase db = this.getWritableDatabase();  
  111.         ContentValues cv = new ContentValues();  
  112.         cv.put(LOGIN_USER, LOGIN_USER);  
  113.         cv.put(LOGIN_PWD, password);  
  114.         return db.insert(LOGIN_TABLE_NAME, null, cv);  
  115.     }  
  116.       
  117.     public int updatePwd(String password){  
  118.         SQLiteDatabase db = this.getWritableDatabase();  
  119.         String where = LOGIN_USER+"=?";  
  120.         String[] whereValues = {LOGIN_USER};  
  121.         ContentValues cv = new ContentValues();  
  122.         cv.put(LOGIN_PWD, password);  
  123.         return db.update(LOGIN_TABLE_NAME, cv, where, whereValues);  
  124.     }  
  125.   
  126.     public String getPwd(){  
  127.         SQLiteDatabase db = this.getReadableDatabase();  
  128.         String where = LOGIN_USER+"=?";  
  129.         String[] whereValues = {LOGIN_USER};   
  130.         Cursor cursor = db.query(LOGIN_TABLE_NAME, null, where, whereValues, nullnullnull);  
  131.         if(cursor.moveToFirst()){  
  132.             return cursor.getString(cursor.getColumnIndex(LOGIN_PWD));  
  133.         }else{  
  134.             return "";  
  135.         }  
  136.     }  
  137. }  


在com.cn.daming.adapter包下的类:

6、ContantsAdapter。java,适配器类:

[java]  view plain copy print ?
  1. package com.cn.daming.adapter;  
  2.   
  3. import java.util.List;  
  4.   
  5. import android.content.Context;  
  6. import android.view.LayoutInflater;  
  7. import android.view.View;  
  8. import android.view.ViewGroup;  
  9. import android.widget.BaseAdapter;  
  10. import android.widget.TextView;  
  11.   
  12. import com.cn.daming.R;  
  13.   
  14. public class ContantsAdapter extends BaseAdapter {  
  15.   
  16.     private List<String> names;  
  17.     private List<String> phones;  
  18.     private LayoutInflater inflater;  
  19.     private Context context;  
  20.       
  21.     public ContantsAdapter(Context context,List<String> names ,List<String> phones){  
  22.         inflater = LayoutInflater.from(context);  
  23.         this.names = names;  
  24.         this.phones = phones;  
  25.         this.context = context;  
  26.     }  
  27.       
  28.     public int getCount() {  
  29.         return names.size();  
  30.     }  
  31.   
  32.     public Object getItem(int position) {  
  33.         return names.get(position);  
  34.     }  
  35.   
  36.     public long getItemId(int position) {  
  37.         return position;  
  38.     }  
  39.   
  40.     public View getView(int position, View view, ViewGroup group) {  
  41.         ContantsHolder holder = new ContantsHolder();  
  42.         if(view==null){  
  43.             view = inflater.inflate(R.layout.contants_list_view, null);  
  44.             holder.contansName = (TextView)view.findViewById(R.id.name_textview);  
  45.             holder.contantsPhone = (TextView)view.findViewById(R.id.phone_textview);  
  46.             view.setTag(holder);  
  47.         }else{  
  48.             holder = (ContantsHolder)view.getTag();  
  49.         }  
  50.         holder.contansName.setText(names.get(position));  
  51.         holder.contantsPhone.setText(phones.get(position));  
  52.         return view;  
  53.     }  
  54.   
  55.     public class ContantsHolder  
  56.     {  
  57.         private TextView contansName;  
  58.         private TextView contantsPhone;  
  59.     }  
  60. }  


布局文件

1、login。xml布局文件:

[html]  view plain copy print ?
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout  
  3.   xmlns:android="http://schemas.android.com/apk/res/android"  
  4.   android:layout_width="fill_parent"  
  5.   android:layout_height="fill_parent"  
  6.   android:paddingTop="10dip"  
  7.   android:orientation="vertical"  
  8.   android:background="@drawable/background"  
  9. >  
  10.   
  11. <TextView  
  12.   android:layout_width="wrap_content"  
  13.   android:layout_height="wrap_content"  
  14.   android:text="@string/please_input_password"  
  15.   android:textSize="24dip"  
  16.   android:textStyle="bold"  
  17.   android:textColor="@drawable/black"  
  18.   android:layout_gravity="center"  
  19. />  
  20.   
  21. <EditText  
  22.   android:id="@+id/login_password"  
  23.   android:layout_width="250dip"  
  24.   android:layout_height="wrap_content"  
  25.   android:password="true"  
  26.   android:layout_gravity="center"  
  27.   android:layout_marginTop="15dip"  
  28. />  
  29.   
  30. <LinearLayout  
  31.   android:layout_width="wrap_content"  
  32.   android:layout_height="wrap_content"  
  33.   android:layout_gravity="center"  
  34.   android:layout_marginTop="15dip"  
  35. >  
  36. <Button  
  37.   android:id="@+id/note_login"  
  38.   android:layout_width="100dip"  
  39.   android:layout_height="wrap_content"  
  40.   android:text="@string/ok"  
  41. />  
  42.   
  43. </LinearLayout>  
  44.   
  45. </LinearLayout>  

2、contants_detail.xml布局文件

[html]  view plain copy print ?
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout  
  3.     xmlns:android="http://schemas.android.com/apk/res/android"  
  4.     android:orientation="vertical"  
  5.     android:layout_width="fill_parent"  
  6.     android:layout_height="wrap_content"  
  7.     android:background="@drawable/background"  
  8.     >  
  9.     <LinearLayout  
  10.         xmlns:android="http://schemas.android.com/apk/res/android"  
  11.         android:orientation="horizontal"  
  12.         android:layout_width="fill_parent"  
  13.         android:layout_height="wrap_content"  
  14.         android:layout_gravity="center_horizontal"  
  15.         >                            <!-- 显示联系人姓名线性布局 -->  
  16.         <TextView  
  17.             android:layout_width="100px"  
  18.             android:layout_height="wrap_content"  
  19.             android:textSize="18px"  
  20.             android:textColor="@color/text"  
  21.             android:layout_gravity="left|center_vertical"  
  22.             android:text="@string/contantsName"  
  23.             />  
  24.         <EditText  
  25.             android:id="@+id/etName"  
  26.             android:layout_width="fill_parent"  
  27.             android:layout_height="wrap_content"  
  28.             />  
  29.         </LinearLayout>  
  30.     <LinearLayout  
  31.         xmlns:android="http://schemas.android.com/apk/res/android"  
  32.         android:orientation="horizontal"  
  33.         android:layout_width="fill_parent"  
  34.         android:layout_height="wrap_content"  
  35.         >                            <!-- 显示联系人固定电话的线性布局 -->  
  36.         <TextView  
  37.             android:layout_width="100px"  
  38.             android:layout_height="wrap_content"  
  39.             android:textSize="18px"  
  40.             android:textColor="@color/text"  
  41.             android:layout_gravity="center_vertical|left"  
  42.             android:text="@string/contantsPhone"  
  43.             />  
  44.         <EditText  
  45.             android:id="@+id/etPhone"  
  46.             android:layout_width="fill_parent"  
  47.             android:layout_height="wrap_content"  
  48.             android:phoneNumber="true"  
  49.             />  
  50.         </LinearLayout>  
  51.     <LinearLayout  
  52.         xmlns:android="http://schemas.android.com/apk/res/android"  
  53.         android:orientation="horizontal"  
  54.         android:layout_width="fill_parent"  
  55.         android:layout_height="wrap_content"  
  56.         >                                        <!-- 显示联系人手机号码的线性布局 -->  
  57.         <TextView  
  58.             android:layout_width="100px"  
  59.             android:layout_height="wrap_content"  
  60.             android:textSize="18px"  
  61.             android:textColor="@color/text"  
  62.             android:layout_gravity="left|center_vertical"  
  63.             android:text="@string/contantsMobile"  
  64.             />  
  65.         <EditText  
  66.             android:id="@+id/etMobile"  
  67.             android:layout_width="fill_parent"  
  68.             android:layout_height="wrap_content"  
  69.             android:phoneNumber="true"  
  70.             />  
  71.         </LinearLayout>  
  72.     <LinearLayout  
  73.         xmlns:android="http://schemas.android.com/apk/res/android"  
  74.         android:orientation="horizontal"  
  75.         android:layout_width="fill_parent"  
  76.         android:layout_height="wrap_content"  
  77.         >                                        <!-- 显示联系人电子邮件的线性布局 -->  
  78.         <TextView  
  79.             android:layout_width="100px"  
  80.             android:layout_height="wrap_content"  
  81.             android:textSize="18px"  
  82.             android:textColor="@color/text"  
  83.             android:layout_gravity="left|center_vertical"  
  84.             android:text="@string/contantsEmail"  
  85.             />  
  86.         <EditText  
  87.             android:id="@+id/etEmail"  
  88.             android:layout_width="fill_parent"  
  89.             android:layout_height="wrap_content"  
  90.             />  
  91.         </LinearLayout>   
  92.     <LinearLayout  
  93.         xmlns:android="http://schemas.android.com/apk/res/android"  
  94.         android:orientation="horizontal"  
  95.         android:layout_width="fill_parent"  
  96.         android:layout_height="wrap_content"  
  97.         >                                        <!-- 显示联系人邮编的线性布局 -->  
  98.         <TextView  
  99.             android:layout_width="100px"  
  100.             android:layout_height="wrap_content"  
  101.             android:textSize="18px"  
  102.             android:textColor="@color/text"  
  103.             android:layout_gravity="left|center_vertical"  
  104.             android:text="@string/contantsPost"  
  105.             />  
  106.         <EditText  
  107.             android:id="@+id/etPost"  
  108.             android:layout_width="fill_parent"  
  109.             android:layout_height="wrap_content"  
  110.             />  
  111.         </LinearLayout>    
  112.     <LinearLayout  
  113.         xmlns:android="http://schemas.android.com/apk/res/android"  
  114.         android:orientation="horizontal"  
  115.         android:layout_width="fill_parent"  
  116.         android:layout_height="wrap_content"  
  117.         >                                        <!-- 显示联系人通信地址的线性布局 -->  
  118.         <TextView  
  119.             android:layout_width="100px"  
  120.             android:layout_height="wrap_content"  
  121.             android:textSize="18px"  
  122.             android:textColor="@color/text"  
  123.             android:layout_gravity="left|center_vertical"  
  124.             android:text="@string/contantsAddr"  
  125.             />  
  126.         <EditText  
  127.             android:id="@+id/etAddr"  
  128.             android:layout_width="fill_parent"  
  129.             android:layout_height="wrap_content"  
  130.             />  
  131.         </LinearLayout>  
  132.     <LinearLayout  
  133.         xmlns:android="http://schemas.android.com/apk/res/android"  
  134.         android:orientation="horizontal"  
  135.         android:layout_width="fill_parent"  
  136.         android:layout_height="wrap_content"  
  137.         >                                        <!-- 显示联系人公司的线性布局 -->  
  138.         <TextView  
  139.             android:layout_width="100px"  
  140.             android:layout_height="wrap_content"  
  141.             android:textSize="18px"  
  142.             android:textColor="@color/text"  
  143.             android:layout_gravity="left|center_vertical"  
  144.             android:text="@string/contantsComp"  
  145.             />  
  146.         <EditText  
  147.             android:id="@+id/etComp"  
  148.             android:layout_width="fill_parent"  
  149.             android:layout_height="wrap_content"  
  150.             />  
  151.         </LinearLayout>  
  152.     <ImageButton  
  153.         android:id="@+id/detailSave"  
  154.         android:layout_width="fill_parent"  
  155.         android:layout_height="wrap_content"  
  156.         android:src="@drawable/save"  
  157.         />  
  158. </LinearLayout>  

 

3、contants_list_view.xml,每一个list的item布局文件

[html]  view plain copy print ?
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout  
  3.   xmlns:android="http://schemas.android.com/apk/res/android"  
  4.   android:layout_width="match_parent"  
  5.   android:layout_height="match_parent"  
  6.   android:orientation="horizontal">  
  7.   <TextView  
  8.       android:id="@+id/name_textview"  
  9.       android:layout_width="150dip"  
  10.       android:layout_height="wrap_content"  
  11.       android:layout_marginRight="10dip"  
  12.       android:textColor="#000000"  
  13.       android:textSize="10pt"  
  14.   />  
  15.   <TextView  
  16.       android:id="@+id/phone_textview"  
  17.       android:layout_width="165dip"  
  18.       android:layout_height="wrap_content"  
  19.       android:textColor="#000000"  
  20.       android:textSize="10pt"  
  21.   />  
  22. </LinearLayout>  

 

4、main.xml布局文件

[html]  view plain copy print ?
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:orientation="vertical"  
  4.     android:layout_width="fill_parent"  
  5.     android:layout_height="fill_parent"  
  6.     android:background="@drawable/background"  
  7.     >  
  8.     <LinearLayout  
  9.         android:orientation="horizontal"  
  10.         android:layout_width="wrap_content"  
  11.         android:layout_height="wrap_content"  
  12.         android:layout_gravity="center_horizontal"  
  13.         >  
  14.         <TextView  
  15.             android:layout_width="wrap_content"  
  16.             android:layout_height="wrap_content"  
  17.             android:layout_gravity="center_horizontal"  
  18.             android:text="@string/title"  
  19.             android:textSize="24px"  
  20.             android:textColor="@color/text"  
  21.             />  
  22.         <ImageView  
  23.             android:layout_width="wrap_content"  
  24.             android:layout_height="wrap_content"  
  25.             android:src="@drawable/title"  
  26.             />  
  27.         </LinearLayout>  
  28.         <ListView      
  29.             android:id="@+id/db_listview"  
  30.             android:layout_width="fill_parent"  
  31.             android:layout_height="fill_parent"  
  32.             android:choiceMode="singleChoice"  
  33.             />   
  34. </LinearLayout>  

 

5、password_manage.xml布局文件

[html]  view plain copy print ?
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout  
  3.   xmlns:android="http://schemas.android.com/apk/res/android"  
  4.   android:layout_width="fill_parent"  
  5.   android:layout_height="wrap_content"  
  6.   android:orientation="vertical"   
  7.   android:background="@drawable/background"   
  8. >  
  9.     
  10. <TextView  
  11.   android:layout_width="wrap_content"  
  12.   android:layout_height="wrap_content"  
  13.   android:layout_gravity="center"  
  14.   android:textStyle="bold"  
  15.   android:textSize="26dip"  
  16.   android:padding="6dip"  
  17.   android:paddingBottom="10dip"  
  18.   android:textColor="@drawable/black"  
  19.   android:text="@string/password_manage"  
  20. />  
  21.   
  22. <TableLayout  
  23.   android:layout_width="fill_parent"  
  24.   android:layout_height="wrap_content"  
  25.   android:layout_marginTop="20dip"  
  26.   android:layout_marginLeft="10dip"  
  27. >  
  28. <TableRow  
  29.   android:layout_width="fill_parent"  
  30.   android:layout_height="50dip"  
  31. >  
  32. <TextView  
  33.   android:layout_width="wrap_content"  
  34.   android:layout_height="fill_parent"  
  35.   android:text="@string/new_password"  
  36.   android:layout_gravity="right"  
  37.   android:gravity="center"  
  38.   android:textColor="#000000"  
  39.   android:textSize="11pt"  
  40. />  
  41. <EditText  
  42.   android:id="@+id/new_password"  
  43.   android:layout_width="180dip"  
  44.   android:layout_height="wrap_content"    
  45.   android:password="true"  
  46.   android:layout_marginLeft="20dip"  
  47. />  
  48. </TableRow>  
  49.   
  50. <TableRow  
  51.   android:layout_width="fill_parent"  
  52.   android:layout_height="50dip"  
  53.   android:layout_marginTop="10dip"  
  54.   android:gravity="center_vertical"  
  55.     
  56. >  
  57. <TextView  
  58.   android:layout_width="wrap_content"  
  59.   android:layout_height="fill_parent"  
  60.   android:text="@string/repeat_password"  
  61.   android:layout_gravity="right"  
  62.   android:gravity="center_vertical"  
  63.   android:textColor="#000000"  
  64.   android:textSize="11pt"  
  65. />  
  66. <EditText  
  67.   android:id="@+id/repeat_password"  
  68.   android:layout_width="180dip"  
  69.   android:layout_height="wrap_content"    
  70.   android:password="true"  
  71.   android:layout_marginLeft="20dip"  
  72. />  
  73. </TableRow>  
  74.   
  75. </TableLayout>  
  76.   
  77. <LinearLayout  
  78.   android:layout_width="fill_parent"  
  79.   android:layout_height="wrap_content"  
  80.   android:gravity="center"  
  81.   android:layout_marginTop="20dip"  
  82. >  
  83. <Button  
  84.   android:id="@+id/save_password"  
  85.   android:layout_width="80dip"  
  86.   android:layout_height="wrap_content"  
  87.   android:text="@string/ok"  
  88.   android:textSize="16dip"  
  89. />  
  90. </LinearLayout>  
  91.   
  92. </LinearLayout>  

 

values下的文件

1、string。xml文件

[html]  view plain copy print ?
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <resources>  
  3.     <string name="hello">Hello World, MainActivity!</string>  
  4.     <string name="app_name">ContantsApp</string>  
  5.       
  6.     <string name="please_input_password">请输入大明通讯录密码:</string>  
  7.     <string name="ok">确定</string>  
  8.       
  9.     <string name="login_success">恭喜,登陆成功!</string>  
  10.     <string name="error_password">您输入的密码错误,请重新输入!</string>  
  11.       
  12.     <string name="password_manage">大明通讯录密码管理:</string>  
  13.     <string name="new_password">新密码:</string>  
  14.     <string name="repeat_password">再一次:</string>  
  15.       
  16.     <string name="password_is_null">输入密码为空,请输入密码!</string>  
  17.     <string name="password_set_success">密码设置成功!</string>  
  18.     <string name="password_change_success">密码修改成功!</string>  
  19.     <string name="password_isnot_equal">两次输入的密码不一致,请重新输入密码!</string>  
  20.       
  21.     <string name="title">大明通讯录列表</string>  
  22.     <string name="not_dbcursor_values">数据库中没有记录,请点击菜单新建通讯录!</string>  
  23.       
  24.     <string name="contantsName">姓名:</string>  
  25.     <string name="contantsPhone">固定电话:</string>  
  26.     <string name="contantsMobile">移动电话:</string>  
  27.     <string name="contantsEmail">电子邮件:</string>  
  28.     <string name="contantsPost">邮政编码:</string>  
  29.     <string name="contantsAddr">通讯地址:</string>  
  30.     <string name="contantsComp">公司地址:</string>  
  31.       
  32.     <string name="menu_add">增加</string>  
  33.     <string name="menu_change">修改密码</string>  
  34. </resources>  

 

2、color。xml文件

[html]  view plain copy print ?
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <resources>  
  3.     <drawable name="white">#FFFFFF</drawable>  
  4.     <drawable name="black">#FF000000</drawable>  
  5.     <drawable name="dackgray">#666666</drawable>  
  6.     <drawable name="red">#FFFF0000</drawable>  
  7.     <drawable name="blue">#FF0000FF</drawable>  
  8.     <drawable name="yellow">#FFFFFF00</drawable>  
  9.     <drawable name="green">#FF00FF00</drawable>  
  10.     <color name="text">#000000</color>  
  11.     <drawable name="changshise">#FFD19275</drawable>  
  12. </resources>  

 

AndroidManifest.xml:

 

[html]  view plain copy print ?
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"  
  3.       package="com.cn.daming"  
  4.       android:versionCode="1"  
  5.       android:versionName="1.0">  
  6.     <uses-sdk android:minSdkVersion="8" />  
  7.   
  8.     <application android:icon="@drawable/icon" android:label="@string/app_name">  
  9.         <activity android:name=".Login"  
  10.                   android:label="@string/app_name">  
  11.             <intent-filter>  
  12.                 <action android:name="android.intent.action.MAIN" />  
  13.                 <category android:name="android.intent.category.LAUNCHER" />  
  14.             </intent-filter>  
  15.         </activity>  
  16.         <activity android:name=".MainActivity"></activity>  
  17.         <activity android:name=".PasswordManage"></activity>  
  18.         <activity android:name=".DetailContantsActivity"></activity>  
  19.     </application>  
  20. </manifest>  


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值