作业九

package com.example.demo01;

import java.util.Map;

import android.app.Activity;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.text.TextUtils;
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 {
     
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main1111);
    }
    class MyHelper extends SQLiteOpenHelper{

        public MyHelper(Context context) {
            super(context, "itcast.db", null, 1);
            // TODO Auto-generated constructor stub
        }

        @Override
        public void onCreate(SQLiteDatabase db) {
            // TODO Auto-generated method stub
            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) {
            // TODO Auto-generated method stub
            
        }
        
    }
    public void Click(View v) {
        String name, phone;
        MyHelper myhelper=new MyHelper(this);
        EditText mEtName=(EditText)findViewById(R.id.editText2);
        EditText mEtPhone=(EditText)findViewById(R.id.editText1);
        TextView mTvShow=(TextView)findViewById(R.id.tv1);
        Button mBtnAdd=(Button)findViewById(R.id.button1);
        Button mBtnQuery=(Button)findViewById(R.id.button2);
        Button mBtnUpdate=(Button)findViewById(R.id.button3);
        Button mBtnDelete=(Button)findViewById(R.id.button4);
        switch(v.getId()) {
            case R.id.button1:
                name = mEtName.getText().toString();
                phone = mEtPhone.getText().toString();
                SQLiteDatabase db = myhelper.getWritableDatabase();
                ContentValues 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.button3:
                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 if (cursor.getCount()!=0) {
                    while (cursor.moveToNext()) {
                    mTvShow.append("\n"+"Name :"+cursor.getString(cursor.getColumnIndex("name"))
                                    +"Tel :"+cursor.getString(cursor.getColumnIndex("phone")));
                }
                }
                
                cursor.close();
                db.close();
                break;
            case R.id.button2:
                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();
                db.close();
                break;
            case R.id.button4:
                db = myhelper.getWritableDatabase();
                db.delete("information", "name= ?", new String[] {mEtName.getText().toString()});
                Toast.makeText(this, "信息已删除", Toast.LENGTH_SHORT).show();
                mTvShow.setText("");
                db.close();
                break;
        }
    }
}
<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.example.homework2.MainActivity" >
    
    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="20dp"
        android:layout_marginTop="70dp"
        android:text="姓 名:"
        android:textSize="25dp" />

    <TextView
        android:id="@+id/textView3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_marginTop="21dp"
        android:layout_toRightOf="@+id/textView1"
        android:text="通 信 录"
        android:textSize="25dp"
        android:textStyle="italic" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/textView1"
        android:layout_below="@+id/textView1"
        android:layout_marginTop="29dp"
        android:text="电 话:"
        android:textSize="25dp" />

    <EditText
        android:id="@+id/editText1"
        android:layout_width="190dp"
        android:layout_height="30dp"
        android:layout_alignBaseline="@+id/textView2"
        android:layout_alignBottom="@+id/textView2"
        android:layout_alignParentRight="true"
        android:background="#fff"
        android:ems="10" />

    <Button
        android:id="@+id/button3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/button2"
        android:layout_alignRight="@+id/editText1"
        android:layout_toRightOf="@+id/textView3"
        android:text="查 询" 
        android:textSize="20sp"
        android:onClick="Click"
        android:background="#00bfff"
        android:textColor="#fff"/>

    <Button
        android:id="@+id/button2"
        android:layout_width="250dp"
        android:layout_height="wrap_content"
        android:layout_below="@+id/button1"
        android:layout_marginTop="21dp"
        android:layout_toLeftOf="@+id/editText1" 
        android:text="修 改"
        android:textSize="20sp"
        android:onClick="Click"
        android:background="#00bfff"
        android:textColor="#fff"/>

    <EditText
        android:id="@+id/editText2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/textView2"
        android:layout_alignLeft="@+id/editText1"
        android:layout_alignTop="@+id/textView1"
        android:background="#fff"
        android:ems="10" />

    <Button
        android:id="@+id/button1"
        android:layout_width="250dp"
        android:layout_height="wrap_content"
        android:layout_below="@+id/editText1"
        android:layout_marginTop="16dp"
        android:layout_toLeftOf="@+id/editText1"
        android:text="添 加" 
        android:textSize="20sp"
        android:onClick="Click"
        android:background="#00bfff"
        android:textColor="#fff"/>

    <Button
        android:id="@+id/button4"
        android:layout_width="115dp"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/button2"
        android:layout_alignBottom="@+id/button2"
        android:layout_alignLeft="@+id/button3"
        android:layout_alignRight="@+id/button3"
        android:background="#00bfff"
        android:textColor="#fff"
        android:onClick="Click"
        android:text="删 除"
        android:textSize="20sp" />

    <TextView
        android:id="@+id/tv1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/button2"
        android:layout_alignRight="@+id/button4"
        android:layout_below="@+id/button4"
         />
    
</RelativeLayout>

posted @ 2019-11-06 15:05  申小奇  阅读(112)  评论(0编辑  收藏  举报