activity_main.xml
<?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:background="@drawable/bg" androID:paddingBottom="16dp" androID:paddingleft="16dp" androID:paddingRight="16dp" androID:paddingtop="16dp" tools:context=".MainActivity"> <linearLayout androID:ID="@+ID/ll_name" androID:layout_wIDth="match_parent" androID:layout_height="wrap_content" androID:layout_above="@+ID/ll_phone" androID:layout_alignleft="@+ID/ll_btn" androID:layout_alignStart="@+ID/ll_btn"> <TextVIEw androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content" androID:textSize="18sp" androID:text="姓 名 :" /> <EditText androID:ID="@+ID/et_name" androID:layout_wIDth="match_parent" androID:layout_height="wrap_content" androID:textSize="16sp" androID:hint="请输入姓名" /> </linearLayout> <linearLayout androID:layout_wIDth="match_parent" androID:layout_height="wrap_content" androID:ID="@+ID/ll_phone" androID:layout_marginBottom="10dp" androID:layout_above="@+ID/ll_btn" androID:layout_alignleft="@+ID/ll_name" androID:layout_alignStart="@+ID/ll_name"> <TextVIEw androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content" androID:textSize="18sp" androID:text="电 话 :" /> <EditText androID:ID="@+ID/et_phone" androID:layout_wIDth="match_parent" androID:layout_height="wrap_content" androID:textSize="16sp" androID:hint="请输入手机号码" /> </linearLayout> <linearLayout androID:layout_wIDth="match_parent" androID:layout_height="wrap_content" androID:ID="@+ID/ll_btn" androID:layout_centerVertical="true" > <button androID:ID="@+ID/btn_add" androID:layout_wIDth="0dp" androID:layout_height="wrap_content" androID:layout_weight="1" androID:textSize="18sp" androID:background="#B9B9FF" androID:layout_marginRight="2dp" androID:text="添加" /> <button androID:ID="@+ID/btn_query" androID:layout_wIDth="0dp" androID:layout_height="wrap_content" androID:layout_weight="1" androID:textSize="18sp" androID:background="#DCB5FF" androID:layout_marginRight="2dp" androID:text="查询" /> <button androID:ID="@+ID/btn_update" androID:layout_wIDth="0dp" androID:layout_height="wrap_content" androID:layout_weight="1" androID:textSize="18sp" androID:background="#E6CAFF" androID:layout_marginRight="2dp" androID:text="修改" /> <button androID:ID="@+ID/btn_delete" androID:layout_wIDth="0dp" androID:layout_height="wrap_content" androID:layout_weight="1" androID:textSize="18sp" androID:background="#ACD6FF" androID:text="删除" /> </linearLayout> <TextVIEw androID:ID="@+ID/tv_show" androID:layout_wIDth="match_parent" androID:layout_height="wrap_content" androID:layout_margintop="25dp" androID:layout_below="@+ID/ll_btn" androID:textSize="20sp" /></relativeLayout>
MyHelper.java
package cn.itcast.directory;import androID.content.Context;import androID.database.sqlite.sqliteDatabase;import androID.database.sqlite.sqliteOpenHelper;import androIDx.annotation.NonNull;import androIDx.annotation.Nullable;public 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) { }}
MainActivity.java
package cn.itcast.directory;import androIDx.appcompat.app.AppCompatActivity;import androID.content.ContentValues;import androID.database.Cursor;import androID.database.sqlite.sqliteDatabase;import androID.os.Bundle;import androID.vIEw.VIEw;import androID.Widget.button;import androID.Widget.EditText;import androID.Widget.TextVIEw;import androID.Widget.Toast;import static cn.itcast.directory.R.*;public class MainActivity extends AppCompatActivity implements VIEw.OnClickListener { private MyHelper myHelper; private EditText mEtname; private EditText mEtPhone; private TextVIEw mTvShow; private button mBtnAdd; private button mBtnquery; private button mBtnUpdate; private button mBtnDelete; @OverrIDe protected voID onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentVIEw(layout.activity_main); myHelper = new MyHelper(this); init(); //初始化控件 } private voID init() { mEtname = (EditText) findVIEwByID(ID.et_name); mEtPhone = (EditText) findVIEwByID(ID.et_phone); mTvShow = (TextVIEw) findVIEwByID(ID.tv_show); mBtnAdd = (button) findVIEwByID(ID.btn_add); mBtnquery = (button) findVIEwByID(ID.btn_query); mBtnUpdate = (button) findVIEwByID(ID.btn_update); mBtnDelete = (button) findVIEwByID(ID.btn_delete); mBtnAdd.setonClickListener(this); mBtnquery.setonClickListener(this); mBtnUpdate.setonClickListener(this); mBtnDelete.setonClickListener(this); } @OverrIDe public voID onClick(VIEw v) { String name; String phone; sqliteDatabase db; ContentValues values; switch (v.getID()) { case R.ID.btn_add: //添加数据 name = mEtname.getText().toString(); phone = mEtPhone.getText().toString(); db = myHelper.getWritableDatabase();//获取可读写sqliteDatabse对象 values = new ContentValues(); // 创建ContentValues对象 values.put("name", name); // 将数据添加到ContentValues对象 values.put("phone", phone); db.insert("information", null, values); Toast.makeText(this, "信息已添加", Toast.LENGTH_SHORT).show(); db.close(); break; case 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("\n" + "name : " + cursor.getString(1) + " ;Tel : " + cursor.getString(2)); } cursor.close(); db.close(); break; case ID.btn_update: //更新数据 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 ID.btn_delete: //删除数据 db = myHelper.getWritableDatabase(); db.delete("information", null, null); Toast.makeText(this, "信息已删除", Toast.LENGTH_SHORT).show(); mTvShow.setText(""); db.close(); break; } }}
总结 以上是内存溢出为你收集整理的安卓绿豆通讯录全部内容,希望文章能够帮你解决安卓绿豆通讯录所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)