对于数据的管理肯定就是最最基本的增删改查,这是毋庸置疑的。初学者在五天内能够将这些操作做到滚瓜烂熟就已经非常不错了。剩下的就交个Intent组件进行通讯的跳转和连接。
因为效果简单,我就不封装数据库操作方法,只贴主要代码,源码请看附件。
db = myhelper.getWritableDatabase();
values = new ContentValues();
values.put("name",name);
values.put("phone",phone);
db.insert("listphone",null,values);
Toast.makeText(this,"信息添加成功",Toast.LENGTH_SHORT).show();
db.close();
db = myhelper.getReadableDatabase();
values = new ContentValues();
if (TextUtils.isEmpty(names.getText())){
Toast.makeText(this,"所需要修改的姓名不能为空!!",Toast.LENGTH_SHORT).show();
break;
} else if (update_id == null){
values.put("phone" , phone = phones.getText().toString());
db.update("listphone",values,"name=?",new String[]{names.getText().toString()});
Toast.makeText(this,"信息修改成功!!",Toast.LENGTH_SHORT).show();
} else {
values.put("name" , phone = names.getText().toString());
values.put("phone" , phone = phones.getText().toString());
db.update("listphone",values,"_id=?",new String[]{update_id});
Toast.makeText(this,"信息修改成功!!",Toast.LENGTH_SHORT).show();
}
db.close();
db = myhelper.getReadableDatabase();
if (TextUtils.isEmpty(names.getText())){
Toast.makeText(this,"请输入需要删除的联系人!!",Toast.LENGTH_SHORT).show();
break;
}
db.delete("listphone", "name=?",new String[]{names.getText().toString()});
Toast.makeText(this,"信息删除成功!!",Toast.LENGTH_SHORT).show();
db.close();
db = myhelper.getReadableDatabase();
Cursor cursor = db.query("listphone",null,null,null,null,null,null);
cursor.moveToFirst();
data = new ArrayList<>();
next = new ArrayList<>();
ids = new ArrayList<>();
if (cursor.getCount() == 0){
Toast.makeText(this,"通讯录为空!!",Toast.LENGTH_SHORT).show();
} else {
ids.add(cursor.getString(0));
data.add(cursor.getString(1));
next.add(cursor.getString(2));
} while (cursor.moveToNext()){
ids.add(cursor.getString(0));
data.add(cursor.getString(1));
next.add(cursor.getString(2));
}
ArrayAdapter<String> adapter = new ArrayAdapter<>(this,R.layout.phine_list,R.id.li,data);
ArrayAdapter<String> adapter1 = new ArrayAdapter<>(this,R.layout.phine_list,R.id.li,next);
list.setAdapter(adapter);
list1.setAdapter(adapter1);
cursor.close();
db.close();]
数据操作也是增删改查,但是更多的则是一些需求的完成。
比如:
1、字体加粗、字体放大/放小、斜体等基本字体操作
2、删除,总不能点进去删,需要局外遍历删除,是长按还是滑动?
3、搜索,模糊搜索,搜索的字段有哪些?
数据库操作在通讯录里已经写过同样的操作,这里就不重复了,就写点其他功能。
AlertDialog dialog;
AlertDialog.Builder builder = new AlertDialog.Builder( NotepadActivity.this)
.setMessage("是否删除此事件?")
.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Notepbook notepadBean = list.get(position);
if(mSQLiteHelper.deleteData(notepadBean.getId())){
list.remove(position);
adapter.notifyDataSetChanged();
Toast.makeText(NotepadActivity.this,"删除成功",
Toast.LENGTH_SHORT).show();
}
}
})
.setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
dialog = builder.create();
dialog.show();
case R.id.bin_bold: // 加粗
content.setTypeface(Typeface.defaultFromStyle(Typeface.BOLD));
break;
case R.id.bin_italic: // 斜体
content.setTypeface(Typeface.defaultFromStyle(Typeface.ITALIC));
break;
case R.id.bin_a: // 变小
fend -= 5;
content.setTextSize(fend);
break;
case R.id.bin_A: // 变大
fend += 5;
content.setTextSize(fend);
break;
计算器就当是给初学者练习的项目
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#666"
tools:context=".MainActivity">
<EditText
android:id="@+id/main_01"
android:layout_width="match_parent"
android:layout_height="120dp"
android:textColor="#fff"
android:gravity="center_vertical|right"
android:background="#F0F0F0"/>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/main_01"
android:layout_marginTop="20dp">
<Button
android:id="@+id/text_ac"
style="@style/textStyle1"
android:layout_width="70dp"
android:layout_height="70dp"
android:background="@drawable/shape_w"
android:text="AC"
android:textColor="#000"/>
<Button
android:id="@+id/user_1"
style="@style/textStyle1"
android:layout_width="70dp"
android:layout_height="70dp"
android:layout_toRightOf="@id/text_ac"
android:background="@drawable/shape_w"
android:text="±"
android:textColor="#000" />
<Button
android:id="@+id/user_bai"
style="@style/textStyle1"
android:layout_width="70dp"
android:layout_height="70dp"
android:layout_toRightOf="@id/user_1"
android:background="@drawable/shape_w"
android:text="%"
android:textColor="#000" />
<Button
android:id="@+id/text_chu"
style="@style/textStyle2"
android:layout_width="70dp"
android:layout_height="70dp"
android:layout_toRightOf="@id/user_bai"
android:background="@drawable/shape_o"
android:text="÷" />
<Button
android:id="@+id/text_01"
style="@style/textStyle1"
android:layout_width="70dp"
android:layout_height="70dp"
android:layout_below="@id/text_ac"
android:background="@drawable/shape_b"
android:text="1" />
<Button
android:id="@+id/text_02"
style="@style/textStyle1"
android:layout_width="70dp"
android:layout_height="70dp"
android:layout_below="@id/text_ac"
android:layout_toRightOf="@id/text_01"
android:background="@drawable/shape_b"
android:text="2" />
<Button
android:id="@+id/text_03"
style="@style/textStyle1"
android:layout_width="70dp"
android:layout_height="70dp"
android:layout_below="@id/text_ac"
android:layout_toRightOf="@id/text_02"
android:background="@drawable/shape_b"
android:text="3" />
<Button
android:id="@+id/text_jia"
style="@style/textStyle2"
android:layout_width="70dp"
android:layout_height="70dp"
android:layout_below="@id/text_chu"
android:layout_toRightOf="@id/user_bai"
android:background="@drawable/shape_o"
android:text="+" />
<Button
android:id="@+id/text_04"
style="@style/textStyle1"
android:layout_width="70dp"
android:layout_height="70dp"
android:layout_below="@id/text_01"
android:background="@drawable/shape_b"
android:text="4" />
<Button
android:id="@+id/text_05"
style="@style/textStyle1"
android:layout_width="70dp"
android:layout_height="70dp"
android:layout_below="@id/text_01"
android:layout_toRightOf="@id/text_04"
android:background="@drawable/shape_b"
android:text="5" />
<Button
android:id="@+id/text_06"
style="@style/textStyle1"
android:layout_width="70dp"
android:layout_height="70dp"
android:layout_below="@id/text_01"
android:layout_toRightOf="@id/text_05"
android:background="@drawable/shape_b"
android:text="6" />
<Button
android:id="@+id/text_jiang"
style="@style/textStyle2"
android:layout_width="70dp"
android:layout_height="70dp"
android:layout_below="@id/text_jia"
android:layout_toRightOf="@id/user_bai"
android:background="@drawable/shape_o"
android:text="-" />
<Button
android:id="@+id/text_07"
style="@style/textStyle1"
android:layout_width="70dp"
android:layout_height="70dp"
android:layout_below="@id/text_04"
android:background="@drawable/shape_b"
android:text="7" />
<Button
android:id="@+id/text_08"
style="@style/textStyle1"
android:layout_width="70dp"
android:layout_height="70dp"
android:layout_below="@id/text_04"
android:layout_toRightOf="@id/text_07"
android:background="@drawable/shape_b"
android:text="8" />
<Button
android:id="@+id/text_09"
style="@style/textStyle1"
android:layout_width="70dp"
android:layout_height="70dp"
android:layout_below="@id/text_04"
android:layout_toRightOf="@id/text_08"
android:background="@drawable/shape_b"
android:text="9" />
<Button
android:id="@+id/text_cheng"
style="@style/textStyle2"
android:layout_width="70dp"
android:layout_height="70dp"
android:layout_below="@id/text_jiang"
android:layout_toRightOf="@id/user_bai"
android:background="@drawable/shape_o"
android:text="×" />
<Button
android:id="@+id/text_00"
style="@style/textStyle1"
android:layout_width="160dp"
android:layout_height="80dp"
android:layout_below="@id/text_07"
android:background="@drawable/shape"
android:text="0" />
<Button
android:id="@+id/text_dian"
style="@style/textStyle1"
android:layout_width="70dp"
android:layout_height="70dp"
android:layout_below="@id/text_07"
android:layout_toRightOf="@id/text_00"
android:background="@drawable/shape_b"
android:text="." />
<Button
android:id="@+id/text_deng"
style="@style/textStyle2"
android:layout_width="70dp"
android:layout_height="70dp"
android:layout_below="@id/text_cheng"
android:layout_toRightOf="@id/user_bai"
android:background="@drawable/shape_o"
android:text="=" />
RelativeLayout>
RelativeLayout>
gitee下载地址:
1、通讯录:https://gitee.com/xu-pq/android-demo/tree/master/Android_Sqlite
2、记事本:https://gitee.com/xu-pq/android-demo/tree/master/Android_Notebook2
3、计算器:https://gitee.com/xu-pq/android-demo/tree/master/diyic