zoukankan      html  css  js  c++  java
  • 通讯录

    1, 界面文件代码

     1 <?xml version="1.0" encoding="utf-8"?>
     2 <LinearLayout 
     3     xmlns:android="http://schemas.android.com/apk/res/android"
     4     xmlns:tools="http://schemas.android.com/tools"
     5     android:layout_width="match_parent"
     6     android:layout_height="match_parent"
     7     tools:context="com.example.wang.tongxunlu.MainActivity"
     8     android:orientation="vertical">
     9 
    10     <ListView
    11         android:layout_width="match_parent"
    12         android:layout_height="0dp"
    13         android:id="@+id/lv_1"
    14         android:layout_weight="1"></ListView>
    15     <Button
    16         android:layout_width="match_parent"
    17         android:layout_height="wrap_content"
    18         android:text="添加联系人"
    19         android:id="@+id/bt_1"
    20         android:onClick="bt_OnClick"/>
    21 </LinearLayout>

    2.实体类代码

     1 package com.example.administrator.myapplication.com.wang.tongxunlu.orm;
     2 
     3 public class TongXunLu {
     4 
     5     private long id;
     6     private String name;
     7     private  String phone_number;
     8 
     9     public long getId() {
    10         return id;
    11     }
    12 
    13     public void setId(long id) {
    14         this.id = id;
    15     }
    16 
    17     public String getPhone_number() {
    18         return phone_number;
    19     }
    20 
    21     public void setPhone_number(String phone_number) {
    22         this.phone_number = phone_number;
    23     }
    24 
    25     public String getName() {
    26         return name;
    27     }
    28 
    29     public void setName(String name) {
    30         this.name = name;
    31     }
    32 
    33     public TongXunLu(long id, String name, String phone_number) {
    34         this.id = id;
    35         this.phone_number = phone_number;
    36         this.name = name;
    37     }
    38 
    39     public TongXunLu(String name, String phone_number) {
    40         this.phone_number = phone_number;
    41         this.name = name;
    42     }
    43 }

    3.数据库创建代码

     1 package com.example.administrator.myapplication.com.wang.tongxunlu.orm;
     2 
     3 import android.content.Context;
     4 import android.database.sqlite.SQLiteDatabase;
     5 import android.database.sqlite.SQLiteOpenHelper;
     6 
     7 
     8 public class DBHelper extends SQLiteOpenHelper {
     9 
    10     @Override
    11     public void onCreate(SQLiteDatabase db) {
    12 
    13         String sql= "CREATE TABLE t_tongxunl (_id  INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL," +
    14                 "name  TEXT,phone_number  INTEGER)";
    15         db.execSQL(sql);
    16 
    17     }
    18 
    19     @Override
    20     public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    21 
    22     }
    23 
    24     public DBHelper(Context context) {
    25         super(context, "TongXunl.db", null, 1);
    26     }
    27 }

    4.数据库操作代码

      1 package com.example.administrator.myapplication.com.wang.tongxunlu.orm;
      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.util.Log;
      8 
      9 import java.util.ArrayList;
     10 
     11 
     12 //数据库操作类
     13 public class TongXunLuDAO {
     14 
     15     private  DBHelper dh;
     16 
     17     private  final String  TABLENAME="t_tongxunl";
     18 
     19     public TongXunLuDAO(Context context)
     20     {
     21         // this.context=context;
     22 
     23         dh=new DBHelper(context);
     24     }
     25 
     26     // 27     //传入参数:实体类的实例
     28     public long insert(TongXunLu tongxunlu)
     29     {
     30         long rtn=0;
     31         //连接数据库
     32         SQLiteDatabase sd=dh.getWritableDatabase();
     33 
     34         ContentValues cv=new ContentValues();
     35 
     36         cv.put("name",tongxunlu.getName());
     37 
     38         cv.put("phone_number",tongxunlu.getPhone_number());
     39 
     40         rtn=sd.insert(TABLENAME,null,cv);
     41 
     42         sd.close();
     43         return rtn;
     44     }
     45 
     46     //
     47     public int delete(long id)
     48     {
     49         int rtn=0;
     50 
     51         SQLiteDatabase sd=dh.getWritableDatabase();
     52 
     53         rtn=sd.delete(TABLENAME,"_id=?",new String[]{id+""});
     54 
     55         sd.close();
     56         return rtn;
     57     }
     58 
     59     //
     60     public int updata(TongXunLu tongxunlu)
     61     {
     62         int rtn=0;
     63 
     64         SQLiteDatabase sd=dh.getWritableDatabase();
     65 
     66         ContentValues cv=new ContentValues();
     67 
     68         cv.put("name",tongxunlu.getName());
     69 
     70         cv.put("phone_number",tongxunlu.getPhone_number());
     71 
     72         rtn=sd.update(TABLENAME,cv,"_id=?",new String[]{tongxunlu.getId()+""});
     73 
     74         Log.e("TAG","rtn="+rtn);
     75 
     76         sd.close();
     77         return  rtn;
     78     }
     79     // 80     //返回查询结果
     81     public ArrayList<TongXunLu> getAll()
     82     {
     83         ArrayList<TongXunLu>  tongxunlus=new ArrayList<>();
     84 
     85         //连接数据库
     86         SQLiteDatabase sd=dh.getWritableDatabase();
     87 
     88         //查询之后得到游标结果集
     89         Cursor cursor=sd.query(TABLENAME, null, null, null, null, null, "_id desc");
     90 
     91         //遍历结果集
     92         while (cursor.moveToNext())
     93         {
     94             //1.把数据转成实体类的实例
     95             TongXunLu bl=new TongXunLu(cursor.getLong(0),cursor.getString(1),cursor.getString(2));
     96 
     97 
     98 
     99             tongxunlus.add(bl);
    100 
    101 
    102         }
    103 
    104         cursor.close();
    105 
    106         sd.close();
    107         return tongxunlus;
    108     }
    109 
    110 }

    5.数据库完成代码

      1 package com.example.administrator.myapplication;
      2 
      3 import android.app.AlertDialog;
      4 import android.content.DialogInterface;
      5 import android.support.v7.app.AppCompatActivity;
      6 import android.os.Bundle;
      7 import android.util.Log;
      8 import android.view.ContextMenu;
      9 import android.view.MenuItem;
     10 import android.view.View;
     11 import android.view.ViewGroup;
     12 import android.widget.AdapterView;
     13 import android.widget.BaseAdapter;
     14 import android.widget.EditText;
     15 import android.widget.ListView;
     16 import android.widget.Toast;
     17 
     18 import com.example.administrator.myapplication.com.wang.tongxunlu.orm.TongXunLu;
     19 import com.example.administrator.myapplication.com.wang.tongxunlu.orm.TongXunLuDAO;
     20 
     21 import java.util.ArrayList;
     22 
     23 public class MainActivity extends AppCompatActivity {
     24 
     25     ListView lv_1;
     26     //数据访问对象
     27     TongXunLuDAO tongXunLuDAO=new TongXunLuDAO(this);
     28     //数据集合
     29     ArrayList<TongXunLu> arrayList;
     30     MyAdapter myAdapter;
     31     int index;
     32 
     33     @Override
     34     protected void onCreate(Bundle savedInstanceState) {
     35         super.onCreate(savedInstanceState);
     36         setContentView(R.layout.activity_main);
     37         lv_1=(ListView)findViewById(R.id.lv_1);
     38         arrayList=tongXunLuDAO.getAll();
     39         myAdapter=new MyAdapter();
     40         lv_1.setAdapter(myAdapter);
     41         lv_1.setOnCreateContextMenuListener(this);
     42     }
     43 
     44     //增加
     45     public void bt_OnClick(View v)
     46     {
     47         final View view= View.inflate(this, R.layout.tongxunlu_activity, null);
     48         AlertDialog alertDialog=new AlertDialog.Builder(this)
     49                 .setTitle("添加联系人信息")
     50                 .setView(view)
     51                 .setPositiveButton("保存", new DialogInterface.OnClickListener() {
     52                     @Override
     53                     public void onClick(DialogInterface dialog, int which) {
     54                         EditText et_name=(EditText)view.findViewById(R.id.et_name);
     55                         EditText et_phone=(EditText)view.findViewById(R.id.et_phone);
     56                         TongXunLu tongXunLu=new TongXunLu(et_name.getText().toString(),
     57                                 et_phone.getText().toString());
     58                         long l=tongXunLuDAO.insert(tongXunLu);
     59 
     60                         if (l>0)
     61                         {
     62                             Toast.makeText(MainActivity.this, "添加成功", Toast.LENGTH_SHORT).show();
     63                         }
     64                         else
     65                         {
     66                             Toast.makeText(MainActivity.this, "添加失败", Toast.LENGTH_SHORT).show();
     67                         }
     68                         arrayList.add(0, tongXunLu);
     69                         myAdapter.notifyDataSetChanged();
     70                     }
     71                 })
     72                 .setNegativeButton("取消",null)
     73                 .show();
     74     }
     75 
     76     @Override
     77     public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
     78         super.onCreateContextMenu(menu, v, menuInfo);
     79 
     80         menu.add(0, 1, 1, "修改");
     81         menu.add(0,2,2,"删除");
     82 
     83         AdapterView.AdapterContextMenuInfo m=(AdapterView.AdapterContextMenuInfo)menuInfo;
     84 
     85         index=m.position;
     86     }
     87 
     88     @Override
     89     public boolean onContextItemSelected(MenuItem item) {
     90 
     91         Log.e("TAG", "index="+index);
     92         switch (item.getItemId())
     93         {
     94             case 1:
     95                 //修改
     96                 View view= View.inflate(this, R.layout.tongxunlu_activity, null);
     97 
     98                 final EditText et_name=(EditText)view.findViewById(R.id.et_name);
     99                 et_name.setText(arrayList.get(index).getName());
    100 
    101                 final EditText et_phone=(EditText)view.findViewById(R.id.et_phone);
    102                 et_phone.setText(arrayList.get(index).getPhone_number());
    103 
    104                 new AlertDialog.Builder(this)
    105                         .setTitle("修改联系人信息")
    106                         .setView(view)
    107                         .setPositiveButton("修改", new DialogInterface.OnClickListener() {
    108                             @Override
    109                             public void onClick(DialogInterface dialog, int which) {
    110 
    111                                 TongXunLu tongxunlu = arrayList.get(index);
    112 
    113                                 tongxunlu.setName(et_name.getText().toString());
    114                                 tongxunlu.setPhone_number(et_phone.getText().toString());
    115 
    116                                 if (tongXunLuDAO.updata(tongxunlu) > 0)
    117                                 {
    118                                     Toast.makeText(MainActivity.this, "修改成功", Toast.LENGTH_SHORT).show();
    119                                 }
    120                                 else
    121                                 {
    122                                     Toast.makeText(MainActivity.this, "修改失败", Toast.LENGTH_SHORT).show();
    123                                 }
    124                             }
    125                         })
    126                         .setNegativeButton("取消", null)
    127                         .show();
    128                 break;
    129             case 2:
    130                 //删除
    131                 new  AlertDialog.Builder(this)
    132                         .setTitle("确定要删除?")
    133                         .setPositiveButton("确定", new DialogInterface.OnClickListener() {
    134                             @Override
    135                             public void onClick(DialogInterface dialog, int which) {
    136                                 //删除
    137                                 if ( tongXunLuDAO.delete(arrayList.get(index).getId())>0)
    138                                 {
    139                                     Toast.makeText(MainActivity.this, "删除成功", Toast.LENGTH_SHORT).show();
    140                                     //更新List
    141                                     arrayList.remove(index);
    142                                 }
    143                                 else
    144                                 {
    145                                     Toast.makeText(MainActivity.this, "删除失败", Toast.LENGTH_SHORT).show();
    146                                 }
    147                             }
    148                         })
    149                         .setNegativeButton("取消",null)
    150                         .show();
    151                 break;
    152         }
    153         return super.onContextItemSelected(item);
    154     }
    155     class MyAdapter extends BaseAdapter
    156     {
    157         @Override
    158         public int getCount() {
    159             return arrayList.size();
    160         }
    161 
    162         @Override
    163         public Object getItem(int position) {
    164             return arrayList.get(position);
    165         }
    166 
    167         @Override
    168         public long getItemId(int position) {
    169             return arrayList.get(position).getId();
    170         }
    171 
    172         @Override
    173         public View getView(int position, View convertView, ViewGroup parent) {
    174 
    175             TongXunLu tongXunLu=arrayList.get(position);
    176 
    177             if (convertView==null)
    178             {
    179                 convertView=View.inflate(MainActivity.this,R.layout.tongxunlu_activity,null);
    180             }
    181 
    182             EditText editText=(EditText)convertView.findViewById(R.id.et_name);
    183 
    184             editText.setText(tongXunLu.getName());
    185 
    186             EditText editText1=(EditText)convertView.findViewById(R.id.et_phone);
    187 
    188             editText1.setText(tongXunLu.getPhone_number());
    189 
    190             return convertView;
    191         }
    192     }
    193 }

    6.自定义对话框界面代码

     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="match_parent"
     5     android:layout_height="match_parent">
     6     <LinearLayout
     7         android:layout_width="match_parent"
     8         android:layout_height="wrap_content">
     9         <TextView
    10             android:layout_width="wrap_content"
    11             android:layout_height="wrap_content"
    12             android:text="姓名:"
    13             android:id="@+id/tv_name"/>
    14         <EditText
    15             android:layout_width="match_parent"
    16             android:layout_height="wrap_content"
    17             android:id="@+id/et_name"/>
    18     </LinearLayout>
    19     <LinearLayout
    20         android:layout_width="match_parent"
    21         android:layout_height="wrap_content">
    22         <TextView
    23             android:layout_width="wrap_content"
    24             android:layout_height="wrap_content"
    25             android:text="电话:"
    26             android:id="@+id/tv_phone"/>
    27         <EditText
    28             android:layout_width="match_parent"
    29             android:layout_height="wrap_content"
    30             android:id="@+id/et_phone"/>
    31     </LinearLayout>
    32 
    33 </LinearLayout>

  • 相关阅读:
    Unity--------------------万向锁的概念
    unity--------prefab嵌套prefab
    ListView嵌套 EditText的光标不显示
    maven clean package 时出现Failed to read artifact descriptor for的问题解决
    maven打包之后为什么class文件中没有注释了?
    Maven中maven-source-plugin,maven-javadoc-plugin插件的使用
    c3p0三种配置方式(automaticTestTable)
    db2设置共享内存
    解决Tomcat数据连接池无法释放
    DB2死锁的解决办法
  • 原文地址:https://www.cnblogs.com/TENOKAWA/p/5577480.html
Copyright © 2011-2022 走看看