第九次作业

package com.example.sq;
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.ActionBarActivity;
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 ActionBarActivity implements View.OnClickListener {
    MyHelper myHelper;
    private EditText mEtName;
    private EditText mEtphone;
    private TextView mTvshow;
    private Button mBtnAdd;
    private Button mBtnQuery;
    private Button mBtnUpdate;
    private Button mBtnDelect;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        myHelper = new MyHelper(this);
        init();
    }
    private void init(){
        mEtName = (EditText) findViewById(R.id.et1);
        mEtphone = (EditText) findViewById(R.id.et2);
        mTvshow = (TextView) findViewById(R.id.tv);
        mBtnAdd = (Button) findViewById(R.id.btn_add);
        mBtnQuery = (Button) findViewById(R.id.btn_ser);
        mBtnUpdate = (Button) findViewById(R.id.btn_alt);
        mBtnDelect = (Button) findViewById(R.id.btn_del);
        mBtnAdd.setOnClickListener(this);
        mBtnQuery.setOnClickListener(this);
        mBtnUpdate.setOnClickListener(this);
        mBtnDelect.setOnClickListener(this);
    }
    @Override
    public void onClick(View v){
        String name,phone;
        SQLiteDatabase db;
        ContentValues values;
        switch (v.getId()){
            case R.id.btn_add :
                name = mEtName.getText().toString();
                phone = mEtphone.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_ser :
                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));
                }
                while (cursor.moveToNext()){
                    mTvshow.append("
" + "Name : " + cursor.getString(1) + " ; Tel : "+cursor.getString(2));
                }
                cursor.close();
                db.close();
                break;
            case R.id.btn_alt :
                db = myHelper.getReadableDatabase();
                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();
                db.close();
                break;
            case R.id.btn_del :
                db = myHelper.getReadableDatabase();
                db.delete("information","name = ?",new String[]{
                        mEtName.getText().toString()});
                Toast.makeText(this,"信息已删除",Toast.LENGTH_SHORT).show();
                db.close();
                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))");
        }
        @Override
        public void onUpgrade(SQLiteDatabase db,int oldVersion,int newVersion){
        }
    }
}
<?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="@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.itcase.directory.MainActivity">

    <LinearLayout
        android:id="@+id/linearlayout1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="140dp"
        android:orientation="horizontal">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="姓名:"
            android:textSize="40px"/>
        <EditText
            android:id="@+id/et1"
            android:layout_height="wrap_content"
            android:layout_width="0dp"
            android:layout_weight="2"
            android:hint="请输入姓名"/>
    </LinearLayout>
    <LinearLayout
        android:id="@+id/linearlayout2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/linearlayout1"
        android:orientation="horizontal">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="电话:"
            android:textSize="40px"/>
        <EditText
            android:id="@+id/et2"
            android:layout_height="wrap_content"
            android:layout_width="0dp"
            android:layout_weight="2"
            android:hint="请输入电话"/>
    </LinearLayout>
    <LinearLayout
        android:id="@+id/linearlayout3"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/linearlayout2"
        android:orientation="horizontal">
        <Button
            android:id="@+id/btn_add"
            android:layout_height="wrap_content"
            android:layout_width="0dp"
            android:layout_weight="1"
             android:background="#3A5FCD"
            android:textColor="#FFFFFF" 
            android:text="添加"/>
        <Button
            android:id="@+id/btn_ser"
            android:layout_height="wrap_content"
            android:layout_width="0dp"
            android:layout_weight="1"
             android:background="#3A5FCD"
            android:textColor="#FFFFFF" 
            android:text="查询"/>
        <Button
            android:id="@+id/btn_alt"
            android:layout_height="wrap_content"
            android:layout_width="0dp"
            android:layout_weight="1"
             android:background="#3A5FCD"
            android:textColor="#FFFFFF" 
            android:text="修改"/>
        <Button
            android:id="@+id/btn_del"
            android:layout_height="wrap_content"
            android:layout_width="0dp"
            android:layout_weight="1"
             android:background="#3A5FCD"
            android:textColor="#FFFFFF" 
            android:text="删除"/>
    </LinearLayout>
    <TextView
        android:id="@+id/tv"
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        android:layout_below="@+id/linearlayout3"
        android:textSize="15dp"/>
</RelativeLayout>
    





第九次作业第九次作业