Candroid:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:padding="8dp"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:text="我的通讯录" android:textColor="#000000" android:textSize="26sp" /> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="姓 名:" android:textColor="#000000" android:textSize="20sp"/> <EditText android:id="@+id/et_name" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="联系人姓名"/> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="联系方式:" android:textColor="#000000" android:textSize="20sp"/> <EditText android:id="@+id/et_phone_number" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="联系方式:" android:inputType="number"/> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <Button android:id="@+id/btn_insert" android:layout_weight="1" android:layout_height="wrap_content" android:layout_width="0dp" android:text="添加"/> <Button android:id="@+id/btn_delete" android:layout_weight="1" android:layout_height="wrap_content" android:layout_width="0dp" android:text="删除"/> <Button android:id="@+id/btn_update" android:layout_weight="1" android:layout_height="wrap_content" android:layout_width="0dp" android:text="修改"/> <Button android:id="@+id/btn_query" android:layout_weight="1" android:layout_height="wrap_content" android:layout_width="0dp" android:text="查询"/> </LinearLayout> <TextView android:id="@+id/tv_list" android:layout_width="match_parent" android:layout_height="wrap_content" android:textSize="20sp"/> </LinearLayout>
ContactDao
public class ContcatDao{ private Context mContext; private DBHelper helper; private static String TABLE_NAME="contact"; public ContcatDao(Context context){ this.mContext=context; helper=new DBHelper(mContext); } public long insert(ContentValues values){ SQLiteDatabase db=helper.getWritableDatabase(); long id=db.insert(TABLE_NAME,null,values); db.close(); return id; } public int deleteAll(){ SQLiteDatabase db=helper.getWritableDatabase(); int number=db.delete(TABLE_NAME,null,null); db.close(); return number; } public int deleteByName(String name){ SQLiteDatabase db=helper.getWritableDatabase(); int number=db.delete(TABLE_NAME,"name=?",new String[]{name}); db.close(); return number; } public int update(ContentValues values,String name){ SQLiteDatabase db=helper.getWritableDatabase(); int number=db.delete(TABLE_NAME,"name=?",new String[]{name}); db.close(); return number; } public Cursor selectAll(){ SQLiteDatabase db=helper.getWritableDatabase(); return db.query(TABLE_NAME,null,null,null,null,null,null); } }
DBHelper
public class DBHelper extends SQLiteOpenHelper{ private static String DB_NAME="contacts.db"; private static int DB_VERSION=1; public DBHelper(Context context) { super(context,DB_NAME, null, DB_VERSION); } @Override public void onCreate(SQLiteDatabase db) { String sql="CREATE TABLE contact(_id INTEGER PRIMARY KEY AUTOINCREMENT,name VARCHAR(10),phonenumber VARCHAR(12))"; db.execSQL(sql); } @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { } }
M
public class MainActivity extends AppCompatActivity implements View.OnClickListener { private EditText etName; private EditText etPhoneNumber; private Button btnInsert; private Button btnDelete; private Button btnUpdate; private Button btnQuery; private TextView tvList; private com.example.administrator.s519300214331zly.dao.ContcatDao dao; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initView(); dao = new com.example.administrator.s519300214331zly.dao.ContcatDao(this); } private void initView() { etName = (EditText) findViewById(R.id.et_name); etPhoneNumber = (EditText) findViewById(R.id.et_phone_number); btnInsert = (Button) findViewById(R.id.btn_insert); btnDelete = (Button) findViewById(R.id.btn_delete); btnUpdate = (Button) findViewById(R.id.btn_update); btnQuery = (Button) findViewById(R.id.btn_query); tvList = (TextView) findViewById(R.id.tv_list);//为按钮注册监听 btnInsert.setOnClickListener(this); btnDelete.setOnClickListener(this); btnUpdate.setOnClickListener(this); btnQuery.setOnClickListener(this); } @Override public void onClick(View v) { String name = null; String phoneNum = null; switch (v.getId()) { case R.id.btn_insert: name = etName.getText().toString().trim(); phoneNum = etPhoneNumber.getText().toString().trim(); if (!TextUtils.isEmpty(name) && !TextUtils.isEmpty(phoneNum)) { ContentValues values = new ContentValues(); values.put("name", name); values.put("phonenumber", phoneNum); if(dao.insert(values) > 0) { Toast.makeText(this,"联系方式添加成功", Toast.LENGTH_SHORT).show(); etName.setText(""); etPhoneNumber.setText(""); } } break; case R.id.btn_delete:// 删除数据 name = etName.getText().toString().trim(); //如果etName不为空则删除名字叫这个的记录 if (!TextUtils.isEmpty(name)) { if (dao.deleteByName(name) > 0){ Toast.makeText(this, "联系人删除成功", Toast.LENGTH_SHORT).show(); } }else{ //删除所有记录 if (dao.deleteAll() > 0) { Toast.makeText(this, "成功删除所有联系人", Toast.LENGTH_SHORT).show(); } } break; case R.id.btn_update: // 更新数据 name = etName.getText().toString().trim(); phoneNum = etPhoneNumber.getText().toString().trim(); if (!TextUtils.isEmpty(name) && !TextUtils.isEmpty(phoneNum)) { ContentValues values = new ContentValues(); values.put("phonenumber", phoneNum); if (dao.update(values, name) > 0) { Toast.makeText(this, "修改联系方式成功", Toast.LENGTH_SHORT).show(); etName.setText(""); etPhoneNumber.setText(""); } } break; case R.id.btn_query: // 查询数据 Cursor cursor = dao.selectAll(); if (cursor.getCount() ==0){ tvList.setText(""); } else{ cursor.moveToFirst(); tvList.setText(cursor.getString(1) + ":" + cursor.getString(2)); } while (cursor.moveToNext()) { tvList.append("\n" + cursor.getString(1) + ":" + cursor.getString(2)); } cursor.close(); break; } } }