java源代码:
package cn.edu.xynu.computer.mybooks;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
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.TextView;
import android.widget.Toast;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
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.TextView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity implements View.OnClickListener{
MyHelper myHelper;
private EditText mEtName;
private EditText mEtPhone;
private EditText mEtInfo;
private EditText mEtPrice;
private TextView mTvShow;
private Button mBtnAdd;
private Button mBtnQuery;
private Button mBtnUpdate;
private Button mBtnDelete;
MyHelper myHelper;
private EditText mEtName;
private EditText mEtPhone;
private EditText mEtInfo;
private EditText mEtPrice;
private TextView mTvShow;
private Button mBtnAdd;
private Button mBtnQuery;
private Button mBtnUpdate;
private Button mBtnDelete;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myHelper = new MyHelper(this);
init();
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myHelper = new MyHelper(this);
init();
}
private void init(){
mEtName = findViewById(R.id.et_name);
mEtPhone = findViewById(R.id.et_phone);
mEtInfo = findViewById(R.id.et_info);
mEtPrice = findViewById(R.id.et_price);
private void init(){
mEtName = findViewById(R.id.et_name);
mEtPhone = findViewById(R.id.et_phone);
mEtInfo = findViewById(R.id.et_info);
mEtPrice = findViewById(R.id.et_price);
mTvShow = findViewById(R.id.tv_show);
mBtnAdd = findViewById(R.id.btn_add);
mBtnQuery=findViewById(R.id.btn_query);
mBtnUpdate =findViewById(R.id.btn_update);
mBtnDelete=findViewById(R.id.btn_delete);
mBtnAdd.setOnClickListener(this);
mBtnQuery.setOnClickListener(this);
mBtnUpdate.setOnClickListener(this);
mBtnDelete.setOnClickListener(this);
}
mBtnAdd = findViewById(R.id.btn_add);
mBtnQuery=findViewById(R.id.btn_query);
mBtnUpdate =findViewById(R.id.btn_update);
mBtnDelete=findViewById(R.id.btn_delete);
mBtnAdd.setOnClickListener(this);
mBtnQuery.setOnClickListener(this);
mBtnUpdate.setOnClickListener(this);
mBtnDelete.setOnClickListener(this);
}
@Override
public void onClick(View v) {
String name;
String phone;
String info;
String price;
SQLiteDatabase db;
ContentValues values;
switch(v.getId()){
case R.id.btn_add:
name = mEtName.getText().toString();
phone=mEtPhone.getText().toString();
info = mEtInfo.getText().toString();
price =mEtPrice.getText().toString();
public void onClick(View v) {
String name;
String phone;
String info;
String price;
SQLiteDatabase db;
ContentValues values;
switch(v.getId()){
case R.id.btn_add:
name = mEtName.getText().toString();
phone=mEtPhone.getText().toString();
info = mEtInfo.getText().toString();
price =mEtPrice.getText().toString();
db=myHelper.getWritableDatabase();
values =new ContentValues();
values =new ContentValues();
values.put("name",name);
values.put("phone",phone);
values.put("info",info);
values.put("price",price);
values.put("phone",phone);
values.put("info",info);
values.put("price",price);
db.insert("information",null,values);
Toast.makeText(this,"信息已添加",Toast.LENGTH_SHORT).show();
db.close();
break;
Toast.makeText(this,"信息已添加",Toast.LENGTH_SHORT).show();
db.close();
break;
case R.id.btn_query:
db=myHelper.getReadableDatabase();
Cursor cursor = db.query("information",null,null,null,null,null,null);
if(cursor.getCount()==0){
mTvShow.setText("");
Toast.makeText(this,"没有数据", Toast.LENGTH_SHORT).show();
}else{
cursor.moveToFirst();
mTvShow.setText("Name:"+cursor.getString(1)+
";Tel:"+cursor.getString(2)+
";Info:"+cursor.getString(3)+
";price:"+cursor.getString(4));
}
while(cursor.moveToNext()){
mTvShow.append("\n"+"Name:"+cursor.getString(1)+
";Tel:"+cursor.getString(2)+
";info:"+cursor.getString(3)+
";price:"+cursor.getString(4));
}
cursor.close();
db.close();
break;
case R.id.btn_update:
db=myHelper.getWritableDatabase();
values =new ContentValues();
values.put("phone",phone=mEtPhone.getText().toString());
db.update("information",values,"name=?",new String[]{mEtName.getText().toString()});
Toast.makeText(this,"信息已修改",Toast.LENGTH_SHORT).show();
mTvShow.setText("");
db.close();
break;
case R.id.btn_delete:
db=myHelper.getWritableDatabase();
db.delete("information",null,null);
Toast.makeText(this,"信息已删除",Toast.LENGTH_SHORT).show();
mTvShow.setText("");
break;
}
}
class MyHelper extends SQLiteOpenHelper{
public MyHelper(Context context){
super (context,"itcast.db",null,1);
db=myHelper.getReadableDatabase();
Cursor cursor = db.query("information",null,null,null,null,null,null);
if(cursor.getCount()==0){
mTvShow.setText("");
Toast.makeText(this,"没有数据", Toast.LENGTH_SHORT).show();
}else{
cursor.moveToFirst();
mTvShow.setText("Name:"+cursor.getString(1)+
";Tel:"+cursor.getString(2)+
";Info:"+cursor.getString(3)+
";price:"+cursor.getString(4));
}
while(cursor.moveToNext()){
mTvShow.append("\n"+"Name:"+cursor.getString(1)+
";Tel:"+cursor.getString(2)+
";info:"+cursor.getString(3)+
";price:"+cursor.getString(4));
}
cursor.close();
db.close();
break;
case R.id.btn_update:
db=myHelper.getWritableDatabase();
values =new ContentValues();
values.put("phone",phone=mEtPhone.getText().toString());
db.update("information",values,"name=?",new String[]{mEtName.getText().toString()});
Toast.makeText(this,"信息已修改",Toast.LENGTH_SHORT).show();
mTvShow.setText("");
db.close();
break;
case R.id.btn_delete:
db=myHelper.getWritableDatabase();
db.delete("information",null,null);
Toast.makeText(this,"信息已删除",Toast.LENGTH_SHORT).show();
mTvShow.setText("");
break;
}
}
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),info varchar(50),price char(4))");
}
public void onCreate(SQLiteDatabase db) {
db.execSQL("create table information(_id integer primary key Autoincrement,name varchar(20),phone varchar(20),info varchar(50),price char(4))");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int NewVersion) {
public void onUpgrade(SQLiteDatabase db, int oldVersion, int NewVersion) {
}
}
}
}
XML的源代码:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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="16dp"
android:paddingLeft="16dp"
android:paddingRight="16dp"
android:paddingTop="16dp"
android:background="@drawable/bg03">
<RelativeLayout 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="16dp"
android:paddingLeft="16dp"
android:paddingRight="16dp"
android:paddingTop="16dp"
android:background="@drawable/bg03">
<LinearLayout
android:id="@+id/ll_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="@+id/ll_phone"
android:layout_alignLeft="@+id/ll_btn"
android:layout_alignStart="@+id/ll_btn"
android:orientation="horizontal">
android:id="@+id/ll_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="@+id/ll_phone"
android:layout_alignLeft="@+id/ll_btn"
android:layout_alignStart="@+id/ll_btn"
android:orientation="horizontal">
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="书名"
android:textSize="16sp" />
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="书名"
android:textSize="16sp" />
<EditText
android:id="@+id/et_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:ems="10"
android:hint="请输入书名"
android:textSize="16sp" />
</LinearLayout>
android:id="@+id/et_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:ems="10"
android:hint="请输入书名"
android:textSize="16sp" />
</LinearLayout>
<LinearLayout
android:id="@+id/ll_phone"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="@+id/ll_info"
android:layout_alignLeft="@+id/ll_name"
android:layout_alignStart="@+id/ll_name"
android:orientation="horizontal">
android:id="@+id/ll_phone"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="@+id/ll_info"
android:layout_alignLeft="@+id/ll_name"
android:layout_alignStart="@+id/ll_name"
android:orientation="horizontal">
<TextView
android:id="@+id/textView2"
android:layout_width="199dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="书号"
android:textSize="18sp" />
android:id="@+id/textView2"
android:layout_width="199dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="书号"
android:textSize="18sp" />
<EditText
android:id="@+id/et_phone"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:hint="请输入书号"
android:textSize="16sp" />
android:id="@+id/et_phone"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:hint="请输入书号"
android:textSize="16sp" />
</LinearLayout>
<LinearLayout
android:id="@+id/ll_info"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="@+id/ll_price"
android:layout_alignLeft="@+id/ll_phone"
android:layout_alignStart="@+id/ll_phone"
android:orientation="horizontal">
android:id="@+id/ll_info"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="@+id/ll_price"
android:layout_alignLeft="@+id/ll_phone"
android:layout_alignStart="@+id/ll_phone"
android:orientation="horizontal">
<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="信息"
android:textSize="18sp" />
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="信息"
android:textSize="18sp" />
<EditText
android:id="@+id/et_info"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:ems="10"
android:hint="请输入书的信息"
android:textSize="16sp" />
android:id="@+id/et_info"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:ems="10"
android:hint="请输入书的信息"
android:textSize="16sp" />
</LinearLayout>
<LinearLayout
android:id="@+id/ll_price"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="@+id/ll_btn"
android:layout_alignLeft="@+id/ll_info"
android:layout_alignStart="@+id/ll_info"
android:layout_marginBottom="10dp"
android:orientation="horizontal">
android:id="@+id/ll_price"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="@+id/ll_btn"
android:layout_alignLeft="@+id/ll_info"
android:layout_alignStart="@+id/ll_info"
android:layout_marginBottom="10dp"
android:orientation="horizontal">
<TextView
android:id="@+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="价格"
android:textSize="18sp"
/>
android:id="@+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="价格"
android:textSize="18sp"
/>
<EditText
android:id="@+id/et_price"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:ems="10"
android:hint="请输入书的价格"
android:textSize="16sp" />
android:id="@+id/et_price"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:ems="10"
android:hint="请输入书的价格"
android:textSize="16sp" />
</LinearLayout>
<LinearLayout
android:id="@+id/ll_btn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:orientation="horizontal">
android:id="@+id/ll_btn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:orientation="horizontal">
<Button
android:id="@+id/btn_add"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textSize="18sp"
android:background="#B9B9EF"
android:layout_marginRight="2dp"
android:text="添加"
/>
android:id="@+id/btn_add"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textSize="18sp"
android:background="#B9B9EF"
android:layout_marginRight="2dp"
android:text="添加"
/>
<Button
android:id="@+id/btn_query"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textSize="18sp"
android:background="#DCB5FF"
android:layout_marginRight="2dp"
android:text="查询" />
android:id="@+id/btn_query"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textSize="18sp"
android:background="#DCB5FF"
android:layout_marginRight="2dp"
android:text="查询" />
<Button
android:id="@+id/btn_update"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textSize="18sp"
android:background="#E6CAFF"
android:layout_marginRight="2dp"
android:text="修改"/>
android:id="@+id/btn_update"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textSize="18sp"
android:background="#E6CAFF"
android:layout_marginRight="2dp"
android:text="修改"/>
<Button
android:id="@+id/btn_delete"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textSize="18sp"
android:background="#ACD6FF"
android:layout_marginRight="2dp"
android:text="删除" />
</LinearLayout>
<TextView
android:id="@+id/tv_show"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/ll_btn"
android:layout_marginTop="25dp"
android:text="TextView"
android:textSize="20sp" />
</RelativeLayout>
android:id="@+id/btn_delete"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textSize="18sp"
android:background="#ACD6FF"
android:layout_marginRight="2dp"
android:text="删除" />
</LinearLayout>
<TextView
android:id="@+id/tv_show"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/ll_btn"
android:layout_marginTop="25dp"
android:text="TextView"
android:textSize="20sp" />
</RelativeLayout>