简单介绍一下功能:简单记事本只能添加文字内容,首页用ListView显示所有保存的事项和保存的时间,添加页面添加内容,点击首页的ListView可以查看内容,删除内容。
先看一下运行效果:
先建立数据库:
package com.engineer.shizhibin.note;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
public class NoteDb 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 NoteDb(Context context) {
super(context, "notes", null,1);
}
@Override
public void onCreate(SQLiteDatabase db) {
String sql ="create table "+TABLE_NAME+" ( "+ID+" integer primary key AUTOINCREMENT, "+CONTENT
+" TEXT NOT NULL, "+TIME+" TEXT NOT NULL )";
db.execSQL(sql);
}
@Override
public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {
}
}
然后是MainActivity:
有显示目录的ListView,和添加按钮。他们都要设置点击监听事件。点击添加 跳转到添加文字的activity 点击ListView跳转到Show Activity。
onResume() 在 Activity 从 Pause 状态转换到 Active 状态时被调用。在这个方法中访问SQLite数据库,通过Adapter将条目的内容和添加时间显示在ListView中。看代码:
package com.engineer.shizhibin.note;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;
public class MainActivity extends AppCompatActivity {
private Button mButton;
private ListView mList;
private Intent mIntent;
private MyAdapter mAdapter;
private NoteDb mNotedb;
private Cursor cursor;
private SQLiteDatabase dbreader;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mList = (ListView) this.findViewById(R.id.list);
mNotedb = new NoteDb(this);
dbreader = mNotedb.getReadableDatabase();
mList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView> adapterView, View view, int i, long l) {
cursor.moveToPosition(i);
Intent intent = new Intent(MainActivity.this,ShowContent.class);
intent.putExtra(NoteDb.ID,cursor.getInt(cursor.getColumnIndex(NoteDb.ID)));
intent.putExtra(NoteDb.CONTENT,cursor.getString(cursor.getColumnIndex(NoteDb.CONTENT)));
intent.putExtra(NoteDb.TIME,cursor.getString(cursor.getColumnIndex(NoteDb.TIME)));
startActivity(intent);
}
});
}
public void add(View v) {
mIntent = new Intent(MainActivity.this,AddContent.class);
startActivity(mIntent);
}
public void selectDb() {
cursor = dbreader.query
(NoteDb.TABLE_NAME,null,null,null,null,null,null);
mAdapter = new MyAdapter(this,cursor);
mList.setAdapter(mAdapter);
}
@Override
protected void onResume() {
super.onResume();
selectDb();
}
}
为了方便程序扩展,以后有可能添加图片、视频内容 我用了自定义的Adapter 继承自BadeAdapter:
package com.engineer.shizhibin.note;
import android.content.Context;
import android.database.Cursor;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;
public class MyAdapter extends BaseAdapter {
private Context mContext;
private Cursor mCursor;
private LinearLayout mLayout;
public MyAdapter(Context mContext,Cursor mCursor) {
this.mContext = mContext;
this.mCursor = mCursor;
}
@Override
public int getCount() {
return mCursor.getCount();
}
@Override
public Object getItem(int position) {
return mCursor.getPosition();
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View view, ViewGroup viewGroup) {
LayoutInflater inflater = LayoutInflater.from(mContext);
mLayout = (LinearLayout) inflater.inflate(R.layout.item,null);
TextView content = (TextView) mLayout.findViewById(R.id.list_content);
TextView time = (TextView) mLayout.findViewById(R.id.list_time);
mCursor.moveToPosition(position);
String dbcontext = mCursor.getString(mCursor.getColumnIndex("content"));
String dbtime = mCursor.getString(mCursor.getColumnIndex("time"));
content.setText(dbcontext);
time.setText(dbtime);
return mLayout;
}
}
添加条目,向数据中添加数据用ContentValues以键值对的方式 非常方便:
package com.engineer.shizhibin.note;
import android.content.ContentValues;
import android.database.sqlite.SQLiteDatabase;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import java.text.SimpleDateFormat;
import java.util.Date;
public class AddContent extends AppCompatActivity {
private EditText mEt;
private NoteDb mDb;
private SQLiteDatabase mSqldb;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add_content);
mEt = (EditText) this.findViewById(R.id.text);
mDb = new NoteDb(this);
mSqldb = mDb.getWritableDatabase();
}
public void save(View v) {
DbAdd();
finish();
}
public void cancle(View v) {
mEt.setText("");
finish();
}
public void DbAdd() {
ContentValues cv = new ContentValues();
cv.put(NoteDb.CONTENT,mEt.getText().toString());
cv.put(NoteDb.TIME,getTime());
mSqldb.insert(NoteDb.TABLE_NAME,null,cv);
}
public String getTime() {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");
Date date = new Date();
String str = sdf.format(date);
return str;
}
}
查看条目:
package com.engineer.shizhibin.note;
import android.database.sqlite.SQLiteDatabase;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
public class ShowContent extends AppCompatActivity {
private TextView mTextview;
private TextView time;
private NoteDb mDb;
private SQLiteDatabase mSql;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_show_content);
mTextview = (TextView)this.findViewById(R.id.showtext);
time = (TextView)this.findViewById(R.id.showtime);
mDb = new NoteDb(this);
mSql = mDb.getWritableDatabase();
mTextview.setText(getIntent().getStringExtra(NoteDb.CONTENT));
time.setText(getIntent().getStringExtra(NoteDb.TIME));
}
public void delete(View v) {
int id = getIntent().getIntExtra(NoteDb.ID,0);
mSql.delete(NoteDb.TABLE_NAME," _id = " + id,null);
finish();
}
public void goBack(View v) {
finish();
}
}
MainActivity布局:
添加Activity布局:
查看页布局:
ListView布局