zoukankan      html  css  js  c++  java
  • 第十个作业 简易通讯录

    public class MainActivity extends ActionBarActivity {
     
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
        }
        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("
    "+"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;
            }
        }
    }

    <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"
             />
    
    

     
  • 相关阅读:
    枚举、函数关于oracle函数listagg的使用说明by小雨
    执行、Mongodb MapReduce示例1个by小雨
    事务、异常TSQL 编码时应该注意的10个问题by小雨
    源、执行GoldenGate 单向DDL同步by小雨
    Oracle中的所有权限by小雨
    数据库、版本数据库学习从此开始by小雨
    统计、案例深入理解Oracle索引(10):索引列字符类型统计信息的32位限制by小雨
    字段、数据库表三大范式及存储方式by小雨
    数据库、用户第二章Getting Start with the Oracle Server(oracle入门)by小雨
    搜索、关键字截图留念,“万能数据库查询分析器”作为关键字在百度和谷歌上的海量搜索结果by小雨
  • 原文地址:https://www.cnblogs.com/wangtianpeng/p/11995927.html
Copyright © 2011-2022 走看看