利用安卓知识写一个简单的记事本 实现添加文本,修改文本,显示文本添加的时间,以及长按文本时能够删除文本。我使用用的是安卓自带的数据库SQLite。
首先创建一个工具类
public class DBUtils1 {
public static final String DATABASE_NAME="Notepad";//数据库名字
public static final String DATABASE_TABLE="Note";//表的名字
public static final int DATABASE_VERION=1;//数据库版本号
public static final String NOTEPAD_ID="id";
public static final String NOTEPAD_CONTENT="content";
public static final String NOTEPAD_TIME="notetime";
public static final String getNotepadTime(){//获取当前时间
SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");
Date date=new Date(System.currentTimeMillis());
return simpleDateFormat.format(date);
}
}
创建数据库
public class SQLiteHelper extends SQLiteOpenHelper {
private SQLiteDatabase sqLiteDatabase;
//创建数据库
public SQLiteHelper(Context context){
super(context, DBUtils1.DATABASE_NAME, null, DBUtils1.DATABASE_VERION);
sqLiteDatabase = this.getWritableDatabase();
}
//创建表
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("create table "+DBUtils1.DATABASE_TABLE+"("+DBUtils1.NOTEPAD_ID+
" integer primary key autoincrement,"+ DBUtils1.NOTEPAD_CONTENT +
" text," + DBUtils1.NOTEPAD_TIME+ " text)");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {}
//添加数据
public boolean insertData(String userContent,String userTime){
ContentValues contentValues=new ContentValues();
contentValues.put(DBUtils1.NOTEPAD_CONTENT,userContent);
contentValues.put(DBUtils1.NOTEPAD_TIME,userTime);
return
sqLiteDatabase.insert(DBUtils1.DATABASE_TABLE,null,contentValues)>0;
}
//删除数据
public boolean deleteData(String id){
String sql=DBUtils1.NOTEPAD_ID+"=?";
String[] contentValuesArray=new String[]{String.valueOf(id)};
return
sqLiteDatabase.delete(DBUtils1.DATABASE_TABLE,sql,contentValuesArray)>0;
}
//修改数据
public boolean updateData(String id,String content,String userYear){
ContentValues contentValues=new ContentValues();
contentValues.put(DBUtils1.NOTEPAD_CONTENT,content);
contentValues.put(DBUtils1.NOTEPAD_TIME,userYear);
String sql=DBUtils1.NOTEPAD_ID+"=?";
String[] strings=new String[]{id};
return
sqLiteDatabase.update(DBUtils1.DATABASE_TABLE,contentValues,sql,strings)>0;
}
//查询数据
public List<NotepadBean> query(){
List<NotepadBean> list=new ArrayList<NotepadBean>();
Cursor cursor=sqLiteDatabase.query(DBUtils1.DATABASE_TABLE,null,null,null,
null,null,DBUtils1.NOTEPAD_ID+" desc");
if (cursor!=null){
while (cursor.moveToNext()){
NotepadBean noteInfo=new NotepadBean();
String id = String.valueOf(cursor.getInt
(cursor.getColumnIndex(DBUtils1.NOTEPAD_ID)));
String content = cursor.getString(cursor.getColumnIndex
(DBUtils1.NOTEPAD_CONTENT));
String time = cursor.getString(cursor.getColumnIndex
(DBUtils1.NOTEPAD_TIME));
noteInfo.setId(id);
noteInfo.setText(content);
noteInfo.setTime(time);
list.add(noteInfo);
}
cursor.close();
}
return list;
}
}
因为用到listview所以要创建构造器和适配器
public class NotepadAdapter extends BaseAdapter {
private LayoutInflater layoutInflater;
private List<NotepadBean>list;
public NotepadAdapter(Context context, List<NotepadBean> list) {
this.layoutInflater = LayoutInflater.from(context);
this.list = list;
}
@Override
public int getCount() {
return list==null?0 : list.size();
}
@Override
public Object getItem(int position) {
return list.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder viewHolder;
if ( convertView==null ){
convertView=layoutInflater.inflate(R.layout.activity_list_notepad,null);
viewHolder=new ViewHolder(convertView);
convertView.setTag(viewHolder);
}else {
viewHolder =(ViewHolder) convertView.getTag();
}
NotepadBean noteInfo= (NotepadBean) getItem(position);
viewHolder.time.setText(noteInfo.getTime());
viewHolder.text.setText(noteInfo.getText());
return convertView;
}
class ViewHolder{
TextView text;
TextView time;
public ViewHolder(View view){
text = (TextView) view.findViewById(R.id.item_text);
time = (TextView) view.findViewById(R.id.item_time);
}
}
}
public class NotepadBean {
private String time;
private String text;
private String id;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getTime() {
return time;
}
public void setTime(String time) {
this.time = time;
}
public String getText() {
return text;
}
public void setText(String text) {
this.text = text;
}
public NotepadBean(String time, String text) {
this.time = time;
this.text = text;
}
public NotepadBean() {
}
}
接下来就是显示界面和添加界面的代码
public class NotepadActivity extends Activity {
ListView listView;
NotepadAdapter adapter;
List<NotepadBean>list;
SQLiteHelper sqLiteHelper;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_notepad);
listView= (ListView) findViewById(R.id.list);
ImageView add= (ImageView) findViewById(R.id.add);
add.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent=new Intent(NotepadActivity.this,
RecordActivity.class);
startActivityForResult(intent,1);
}
});
initData();
}
private void initData() {
sqLiteHelper=new SQLiteHelper(this);
showQueryData();
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
NotepadBean notepadBean=list.get(position);
Intent intent=new Intent(NotepadActivity.this,RecordActivity.class);
intent.putExtra("id",notepadBean.getId());
intent.putExtra("time",notepadBean.getTime());
intent.putExtra("content",notepadBean.getText());
NotepadActivity.this.startActivityForResult(intent, 1);
}
});
listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> parent, View view, final int position, long id) {
AlertDialog dialog;
AlertDialog.Builder builder=new AlertDialog.Builder(NotepadActivity.this)
.setMessage("是否删除此事件")
.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
NotepadBean notepadBean=list.get(position);
if (sqLiteHelper.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();
return true;
}
});
}
private void showQueryData(){
if(list!=null){
list.clear();
}
list=sqLiteHelper.query();
adapter = new NotepadAdapter(this, list);
listView.setAdapter(adapter);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if ( requestCode==1&&resultCode==2 ){
showQueryData();
}
}
}
public class RecordActivity extends Activity implements View.OnClickListener{
ImageView note_back;
TextView note_time;
EditText content;
ImageView delete;
ImageView note_save;
SQLiteHelper sqLiteHelper;
TextView notename;
String id;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.notepad);
note_back= (ImageView) findViewById(R.id.note_back);
note_time= (TextView) findViewById(R.id.time);
content= (EditText) findViewById(R.id.content);
delete= (ImageView) findViewById(R.id.remove);
note_save= (ImageView) findViewById(R.id.keep);
note_time= (TextView) findViewById(R.id.time);
notename= (TextView) findViewById(R.id.note_name);
note_save.setOnClickListener(this);
note_back.setOnClickListener(this);
delete.setOnClickListener(this);
initData();
}
private void initData() {
sqLiteHelper=new SQLiteHelper(this);
notename.setText("添加记录");
Intent intent=getIntent();
if ( intent != null ){
id =intent.getStringExtra("id");
if ( id !=null ){
notename.setText("修改记录");
content.setText(intent.getStringExtra("content"));
note_time.setText(intent.getStringExtra("time"));
note_time.setVisibility(View.VISIBLE);
}
}
}
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.note_back:
finish();
break;
case R.id.remove:
content.setText("");
break;
case R.id.keep:
String noteContent=content.getText().toString().trim();
if ( id!=null ){
if (noteContent.length()>0){
if ( sqLiteHelper.updateData(id,noteContent, DBUtils1.getNotepadTime()) ){
showToast("修改成功");
setResult(2);
finish();
}else {
showToast("修改失败");
}
}else {
showToast("不能为空");
}
}else {
if ( noteContent.length()>0 ){
if ( sqLiteHelper.insertData(noteContent, DBUtils1.getNotepadTime()) ){
showToast("保存成功");
setResult(2);
finish();
}else {
showToast("保存失败");
}
}else {
showToast("不能为空");
}
}
break;
}
}
public void showToast(String message){
Toast.makeText(RecordActivity.this,message,Toast.LENGTH_SHORT).show();
}
}
布局比较简单 我就不展示出来了 (修改界面和增加界面的布局用的是同一个界面 只是文本框显示的内容不一样 代码在RecordActivity 中有体现出来)
源码下载地址
https://download.youkuaiyun.com/download/Linlee11/86791113
这篇博客介绍如何利用Android知识和SQLite数据库创建一个具备添加、修改、显示时间戳及长按删除功能的记事本应用。文章涵盖工具类、数据库创建、ListView的使用,以及UI界面的实现。
7116

被折叠的 条评论
为什么被折叠?



