• 第九次作业


    <?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="55px"/>
            <EditText
                android:id="@+id/et_name"
                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="55px"/>
            <EditText
                android:id="@+id/et_phone"
                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:text="添加"/>
            <Button
                android:id="@+id/btn_query"
                android:layout_height="wrap_content"
                android:layout_width="0dp"
                android:layout_weight="1"
                android:text="查询"/>
            <Button
                android:id="@+id/btn_update"
                android:layout_height="wrap_content"
                android:layout_width="0dp"
                android:layout_weight="1"
                android:text="修改"/>
            <Button
                android:id="@+id/btn_delect"
                android:layout_height="wrap_content"
                android:layout_width="0dp"
                android:layout_weight="1"
                android:text="删除"/>
        </LinearLayout>
        <TextView
            android:id="@+id/tv_show"
            android:layout_height="wrap_content"
            android:layout_width="match_parent"
            android:layout_below="@+id/linearlayout3"
            android:textSize="15dp"/>
    </RelativeLayout>
    package com.itcase.directory;
    
    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.et_name);
            mEtphone = (EditText) findViewById(R.id.et_phone);
            mTvshow = (TextView) findViewById(R.id.tv_show);
            mBtnAdd = (Button) findViewById(R.id.btn_add);
            mBtnQuery = (Button) findViewById(R.id.btn_query);
            mBtnUpdate = (Button) findViewById(R.id.btn_update);
            mBtnDelect = (Button) findViewById(R.id.btn_delect);
            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_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));
                    }
                    while (cursor.moveToNext()){
                        mTvshow.append("
    " + "Name : " + cursor.getString(1) + " ; Tel : "+cursor.getString(2));
                    }
                    cursor.close();
                    db.close();
                    break;
                case R.id.btn_update :
                    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_delect :
                    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){
            }
        }
    }

  • 相关阅读:
    mysql逻辑备份
    Configuring ProxySQL
    CSS伸缩布局
    溢出文字隐藏三种方式
    CSS过渡效果transition和动画
    伪元素before和after本质
    css滑动门技术
    字体图标iconfont
    CSS精灵技术(sprite)
    行内块和文字垂直对齐vertical-agign
  • 原文地址:https://www.cnblogs.com/rongrui/p/11773343.html
Copyright © 2020-2023  润新知