第九次作业
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#55000000"
android:orientation="vertical"
android:paddingLeft="30dp"
android:paddingRight="30dp">
<LinearLayout
android:layout_marginTop="80dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:orientation="horizontal" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="通讯录"
android:textColor="#fff"
android:textSize="50dp" />
</LinearLayout>
<EditText
android:id="@+id/userid"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="30dp"
android:background="@null"
android:hint="请输入姓名"
android:maxLength="13"
android:singleLine="true"
android:textColor="#fff"
android:textSize="25sp"
android:textColorHint="#eee" />
<View
android:layout_width="match_parent"
android:layout_height="1px"
android:layout_marginTop="10dp"
android:background="#eee" />
<EditText
android:id="@+id/password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:background="@null"
android:hint="请输入手机号码"
android:inputType="textPassword"
android:maxLength="13"
android:singleLine="true"
android:textColor="#fff"
android:textSize="25dp"
android:textColorHint="#eee" />
<View
android:layout_width="match_parent"
android:layout_height="1px"
android:layout_marginTop="10dp"
android:background="#eee" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:orientation="horizontal" >
<Button
android:id="@+id/btn_1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="添加"/>
<Button
android:id="@+id/btn_2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="查询"/>
<Button
android:id="@+id/btn_3"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="修改"/>
<Button
android:id="@+id/btn_4"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="删除"/>
</LinearLayout>
</LinearLayout>
</LinearLayout>
package com.example.director;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Switch;
import android.widget.Toast;
import java.security.cert.Extension;
public class MainActivity extends AppCompatActivity implements View.OnClickListener{
MyHelper myHelper;
private EditText userid;
private EditText password;
private Button btn_1;
private Button btn_2;
private Button btn_3;
private Button btn_4;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myHelper =new MyHelper(this);
init();
}
private void init() {
userid=(EditText)findViewById(R.id.userid);
password=(EditText)findViewById(R.id.password);
btn_1=(Button) findViewById(R.id.btn_1);
btn_2=(Button) findViewById(R.id.btn_2);
btn_3=(Button) findViewById(R.id.btn_3);
btn_4=(Button) findViewById(R.id.btn_4);
btn_1.setOnClickListener(this);
btn_2.setOnClickListener(this);
btn_3.setOnClickListener(this);
btn_4.setOnClickListener(this);
}
@Override
public void onClick(View v) {
String name ,phone;
SQLiteDatabase db;
ContentValues values;
switch (v.getId()){
//添加数据
case R.id.btn_1:
name=userid.getText().toString();
phone=password.getText().toString();
db=myHelper.getWritableDatabase();
values=new ContentValues();
values.put("name",name);
values.put("phone",phone);
db.insert("information",null,values);
Toast.makeText(this,"信息已添加",Toast.LENGTH_SHORT).show();
db.close();
break;
//查询数据
case R.id.btn_2:
db =myHelper.getReadableDatabase();
Cursor cursor=db.query("information",null,null,null,null,null,null);
if (cursor.getCount()==0){
Toast.makeText(this,"没有数据",Toast.LENGTH_SHORT).show();
}else{
cursor.moveToFirst();
}
while (cursor.moveToNext()){
}
cursor.close();
db.close();
break;
//修改数据
case R.id.btn_3:
db=myHelper.getWritableDatabase();
values=new ContentValues();
values.put("phone",phone=password.getText().toString());
db.update("information",values,"name=?",new String[]{userid.getText().toString()});
Toast.makeText(this,"信息已修改",Toast.LENGTH_SHORT).show();
db.close();
break;
//删除数据
case R.id.btn_4:
db=myHelper.getWritableDatabase();
db.delete("information",null,null);
Toast.makeText(this,"信息已删除",Toast.LENGTH_SHORT).show();
db.close();
break;
}
}
}
package com.example.director;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
class MyHelper extends SQLiteOpenHelper {
public MyHelper(Context context) {
super(context,"itcast.db",null,1);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("CREATE TABLE information (_id INTEGER PRIMARY KEY AUTOINCREMENT,name VARCHAR(20),phone VARCHAR(20))");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
}