• 第十个作业 简易通讯录


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

     
  • 相关阅读:
    将aaaa替换成aaba 分类: python 小练习 2013-10-28 18:28 246人阅读 评论(0) 收藏
    使用生成器返回fibs列表 分类: python Module python基础学习 2013-10-28 18:19 283人阅读 评论(0) 收藏
    python中的生成器(generator) 分类: python Module python基础学习 2013-10-28 17:41 310人阅读 评论(0) 收藏
    遇到的问题总结 分类: 问题总结 2013-10-28 17:21 263人阅读 评论(0) 收藏
    win7 下安装ipython 分类: python基础学习 software 2013-10-19 12:23 1383人阅读 评论(0) 收藏
    获取函数中的参数 分类: 正则表达式 2013-10-16 15:14 221人阅读 评论(0) 收藏
    使用termcolor模块 分类: python Module 2013-10-12 14:06 459人阅读 评论(0) 收藏
    Mysql启动失败
    java-抽象类
    java三大特性--多态(1)
  • 原文地址:https://www.cnblogs.com/wangtianpeng/p/11995927.html
Copyright © 2020-2023  润新知