本设计完成后,能够实现以下功能:
(1)设计实现“添加新记事”,“编辑内容”,“保存和删除当前记事”这三个主要的功能模块。
(2)分析并解决实现中的若干技术问题,像组件的选择以及保存方式的选择。
(3)进行测试并分析结果。
具体过程如下:
组件设计:
本项目涉及到以下组件:
本次设计的基于Android简易记事本APP系统,方便用户随时随地记录,操作简单,更大的发挥了用户的自定义功能。
程序设计:
(1)主函数设计:
package com.example.michaelxuzhi.notebook2;
import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;
//import java.util.Timer;
public class MainActivity extends Activity implements View.OnClickListener {
private ListView lv;
private SQLiteDatabase dbReader;
private Cursor cursor;
private NotesDB notesDB;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
}
private void initView() {
Button addbtn = (Button) findViewById(R.id.main_add);
lv = (ListView) findViewById(R.id.main_list);
addbtn.setOnClickListener(this);
notesDB = new NotesDB(this);
dbReader = notesDB.getReadableDatabase();
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
cursor.moveToPosition(position);
Intent i = new Intent(MainActivity.this, SelectAct.class);
i.putExtra(NotesDB.ID, cursor.getInt(cursor.getColumnIndex(NotesDB.ID)));
i.putExtra(notesDB.CONTENT, cursor.getString(cursor.getColumnIndex(NotesDB.CONTENT)));
i.putExtra(notesDB.TIME, cursor.getString(cursor.getColumnIndex(NotesDB.TIME)));
startActivity(i);
}
});
}
@Override
public void onClick(View v) {
Intent i = new Intent(this, Addcontent.class);
startActivity(i);
}
private void selectDB() {
cursor = dbReader.query(NotesDB.TABLE_NAME, null, null, null, null, null, null);
MyAdapter adapter = new MyAdapter(this, cursor);
lv.setAdapter(adapter);
}
@Override
protected void onResume() {
super.onResume();
selectDB();
}
}
(2)添加内容程序设计:
public class Addcontent extends Activity implements View.OnClickListener {
private EditText ettext;
private SQLiteDatabase dbWriter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.add_list);
Button savebtn = (Button) findViewById(R.id.add_save);
Button deletebtn = (Button) findViewById(R.id.add_delete);
ettext = (EditText) findViewById(R.id.add_etv);
savebtn.setOnClickListener(this);
deletebtn.setOnClickListener(this);
NotesDB notesDB = new NotesDB(this);
dbWriter = notesDB.getWritableDatabase();
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.add_save:
addDB();
finish();
break;
case R.id.add_delete:
finish();
break;
}
}
private void addDB() {
ContentValues cv = new ContentValues();
cv.put(NotesDB.CONTENT, ettext.getText().toString());
cv.put(NotesDB.TIME, getTime());
dbWriter.insert(NotesDB.TABLE_NAME, null, cv);
}
public String getTime() {
SimpleDateFormat format = new SimpleDateFormat("yyy年MM月dd日 HH:mm:ss");
Date curDate = new Date();
String str = format.format(curDate);
return str;
}
}
(3)项目驱动程序设计:
public class MyAdapter extends BaseAdapter {
private Context context;
private Cursor cursor;
public MyAdapter(MainActivity context, Cursor cursor){
this.context = context;
this.cursor = cursor;
}
@Override
public int getCount() {
return cursor.getCount();
}
@Override
public Object getItem(int position) {
return cursor.getPosition();
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = LayoutInflater.from(context);
LinearLayout layout = (LinearLayout) inflater.inflate(R.layout.list, null);
TextView contenttv = (TextView) layout.findViewById(R.id.list_content);
TextView timetv = (TextView) layout.findViewById(R.id.list_time);
cursor.moveToPosition(position);
String content = cursor.getString(cursor.getColumnIndex("content"));
String time = cursor.getString(cursor.getColumnIndex("time"));
contenttv.setText(content);
timetv.setText(time);
return layout;
}
}
(4)选择功能程序设计:
public class SelectAct extends Activity implements View.OnClickListener {
private Button s_delete, s_back;
private TextView s_tv;
private NotesDB notesDB;
private SQLiteDatabase dbWriter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.select);
s_back = (Button) findViewById(R.id.s_back);
s_delete = (Button) findViewById(R.id.s_delete);
s_tv = (TextView) findViewById(R.id.s_tv);
notesDB = new NotesDB(this);
dbWriter = notesDB.getWritableDatabase();
s_back.setOnClickListener(this);
s_delete.setOnClickListener(this);
s_tv.setText(getIntent().getStringExtra(NotesDB.CONTENT));
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.s_delete:
deleteDate();
(5)笔记功能程序设计:
public class NotesDB extends SQLiteOpenHelper {
public static final String TABLE_NAME = "notes";
public static final String CONTENT = "content";
public static final String ID = "_id";
public static final String TIME = "time";
public NotesDB(Context context) {
super(context, "notes", null, 1);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("create table " + TABLE_NAME + " (" + ID + " integer primary key autoincrement,"
+ CONTENT + " text not null," + TIME + " text not null)");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
}