界面:

添加信息:

 

 查询信息:

 

 

 

 

按名字删除信息:

 

 

查看删除后:

 

 

点击修改按钮:

 

 

修改后:

 

 

 MainActivity.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("\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;
        }
    }
}

activity_main.xml代码:

 

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

 

版权声明:本文为XiaoPoHai-11原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://www.cnblogs.com/XiaoPoHai-11/p/11782498.html