![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
package com.example.wang.tongxunlu; import android.app.AlertDialog; import android.content.DialogInterface; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.util.Log; import android.view.ContextMenu; import android.view.MenuItem; import android.view.View; import android.view.ViewGroup; import android.widget.AdapterView; import android.widget.BaseAdapter; import android.widget.EditText; import android.widget.ListView; import android.widget.Toast; import com.example.wang.tongxunlu.com.wang.tongxunlu.orm.TongXunLu; import com.example.wang.tongxunlu.com.wang.tongxunlu.orm.TongXunLuDAO; import java.util.ArrayList; public class MainActivity extends AppCompatActivity { ListView lv_1; //数据访问对象 TongXunLuDAO tongXunLuDAO=new TongXunLuDAO(this); //数据集合 ArrayList<TongXunLu> arrayList; MyAdapter myAdapter; int index; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); lv_1=(ListView)findViewById(R.id.lv_1); arrayList=tongXunLuDAO.getAll(); myAdapter=new MyAdapter(); lv_1.setAdapter(myAdapter); lv_1.setOnCreateContextMenuListener(this); } //增加 public void bt_OnClick(View v) { final View view= View.inflate(this, R.layout.tongxunlu_activity, null); AlertDialog alertDialog=new AlertDialog.Builder(this) .setTitle("添加联系人信息") .setView(view) .setPositiveButton("保存", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { EditText et_name=(EditText)view.findViewById(R.id.et_name); EditText et_phone=(EditText)view.findViewById(R.id.et_phone); TongXunLu tongXunLu=new TongXunLu(et_name.getText().toString(), et_phone.getText().toString()); long l=tongXunLuDAO.insert(tongXunLu); if (l>0) { Toast.makeText(MainActivity.this, "添加成功", Toast.LENGTH_SHORT).show(); } else { Toast.makeText(MainActivity.this, "添加失败", Toast.LENGTH_SHORT).show(); } arrayList.add(0, tongXunLu); myAdapter.notifyDataSetChanged(); } }) .setNegativeButton("取消",null) .show(); } @Override public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) { super.onCreateContextMenu(menu, v, menuInfo); menu.add(0, 1, 1, "修改"); menu.add(0,2,2,"删除"); AdapterView.AdapterContextMenuInfo m=(AdapterView.AdapterContextMenuInfo)menuInfo; index=m.position; } @Override public boolean onContextItemSelected(MenuItem item) { Log.e("TAG","index="+index); switch (item.getItemId()) { case 1: //修改 View view= View.inflate(this, R.layout.tongxunlu_activity, null); final EditText et_name=(EditText)view.findViewById(R.id.et_name); et_name.setText(arrayList.get(index).getName()); final EditText et_phone=(EditText)view.findViewById(R.id.et_phone); et_phone.setText(arrayList.get(index).getPhone_number()); new AlertDialog.Builder(this) .setTitle("修改联系人信息") .setView(view) .setPositiveButton("修改", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { TongXunLu tongxunlu = arrayList.get(index); tongxunlu.setName(et_name.getText().toString()); tongxunlu.setPhone_number(et_phone.getText().toString()); if (tongXunLuDAO.updata(tongxunlu) > 0) { Toast.makeText(MainActivity.this, "修改成功", Toast.LENGTH_SHORT).show(); } else { Toast.makeText(MainActivity.this, "修改失败", Toast.LENGTH_SHORT).show(); } //刷新列表 myAdapter.notifyDataSetChanged(); } }) .setNegativeButton("取消", null) .show(); break; case 2: //删除 new AlertDialog.Builder(this) .setTitle("确定要删除?") .setPositiveButton("确定", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { //删除 if ( tongXunLuDAO.delete(arrayList.get(index).getId())>0) { Toast.makeText(MainActivity.this, "删除成功", Toast.LENGTH_SHORT).show(); //更新List arrayList.remove(index); //刷新列表 myAdapter.notifyDataSetChanged(); } else { Toast.makeText(MainActivity.this, "删除失败", Toast.LENGTH_SHORT).show(); } } }) .setNegativeButton("取消",null) .show(); break; } return super.onContextItemSelected(item); } class MyAdapter extends BaseAdapter { @Override public int getCount() { return arrayList.size(); } @Override public Object getItem(int position) { return arrayList.get(position); } @Override public long getItemId(int position) { return arrayList.get(position).getId(); } @Override public View getView(int position, View convertView, ViewGroup parent) { TongXunLu tongXunLu=arrayList.get(position); if (convertView==null) { convertView=View.inflate(MainActivity.this,R.layout.tongxunlu_activity,null); } EditText editText=(EditText)convertView.findViewById(R.id.et_name); editText.setText(tongXunLu.getName()); EditText editText1=(EditText)convertView.findViewById(R.id.et_phone); editText1.setText(tongXunLu.getPhone_number()); return convertView; } } }
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
package com.example.wang.tongxunlu.com.wang.tongxunlu.orm; import android.content.ContentValues; import android.content.Context; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; import android.util.Log; import java.util.ArrayList; //数据库操作类 public class TongXunLuDAO { private DBHelper dh; private final String TABLENAME="t_tongxunl"; public TongXunLuDAO(Context context) { // this.context=context; dh=new DBHelper(context); } //增 //传入参数:实体类的实例 public long insert(TongXunLu tongxunlu) { long rtn=0; //连接数据库 SQLiteDatabase sd=dh.getWritableDatabase(); ContentValues cv=new ContentValues(); cv.put("name",tongxunlu.getName()); cv.put("phone_number",tongxunlu.getPhone_number()); rtn=sd.insert(TABLENAME,null,cv); sd.close(); return rtn; } //删 public int delete(long id) { int rtn=0; SQLiteDatabase sd=dh.getWritableDatabase(); rtn=sd.delete(TABLENAME,"_id=?",new String[]{id+""}); sd.close(); return rtn; } //改 public int updata(TongXunLu tongxunlu) { int rtn=0; SQLiteDatabase sd=dh.getWritableDatabase(); ContentValues cv=new ContentValues(); cv.put("name",tongxunlu.getName()); cv.put("phone_number",tongxunlu.getPhone_number()); rtn=sd.update(TABLENAME,cv,"_id=?",new String[]{tongxunlu.getId()+""}); Log.e("TAG","rtn="+rtn); sd.close(); return rtn; } //查 //返回查询结果 public ArrayList<TongXunLu> getAll() { ArrayList<TongXunLu> tongxunlus=new ArrayList<>(); //连接数据库 SQLiteDatabase sd=dh.getWritableDatabase(); //查询之后得到游标结果集 Cursor cursor=sd.query(TABLENAME, null, null, null, null, null, "_id desc"); //遍历结果集 while (cursor.moveToNext()) { //1.把数据转成实体类的实例 TongXunLu bl=new TongXunLu(cursor.getLong(0),cursor.getString(1),cursor.getString(2)); tongxunlus.add(bl); } cursor.close(); sd.close(); return tongxunlus; } }
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
package com.example.wang.tongxunlu.com.wang.tongxunlu.orm; public class TongXunLu { private long id; private String name; private String phone_number; public long getId() { return id; } public void setId(long id) { this.id = id; } public String getPhone_number() { return phone_number; } public void setPhone_number(String phone_number) { this.phone_number = phone_number; } public String getName() { return name; } public void setName(String name) { this.name = name; } public TongXunLu(long id, String name, String phone_number) { this.id = id; this.phone_number = phone_number; this.name = name; } public TongXunLu(String name, String phone_number) { this.phone_number = phone_number; this.name = name; } }
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
package com.example.wang.tongxunlu.com.wang.tongxunlu.orm; import android.content.Context; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteOpenHelper; public class DBHelper extends SQLiteOpenHelper { @Override public void onCreate(SQLiteDatabase db) { String sql= "CREATE TABLE t_tongxunl (_id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL," + "name TEXT,phone_number INTEGER)"; db.execSQL(sql); } @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { } public DBHelper(Context context) { super(context, "TongXunl.db", null, 1); } }
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="com.example.wang.tongxunlu.MainActivity" android:orientation="vertical"> <ListView android:layout_width="match_parent" android:layout_height="0dp" android:id="@+id/lv_1" android:layout_weight="1"></ListView> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:text="添加联系人" android:id="@+id/bt_1" android:onClick="bt_OnClick"/> </LinearLayout>
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="姓名:" android:id="@+id/tv_name"/> <EditText android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/et_name"/> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="电话:" android:id="@+id/tv_phone"/> <EditText android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/et_phone"/> </LinearLayout> </LinearLayout>