Android开发简单记事本程序(附源码)
简单介绍⼀下功能:简单记事本只能添加⽂字内容,⾸页⽤ListView显⽰所有保存的事项和保存的时间,添加页⾯添加内容,点击⾸页的ListView可以查看内容,删除内容。
先看⼀下运⾏效果:
先建⽴数据库:
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 )";
}
@Override
public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {
简易安卓app开发}
}
然后是MainActivity:
有显⽰⽬录的ListView,和添加按钮。他们都要设置点击监听事件。点击添加 跳转到添加⽂字的activity 点击ListView跳转到Show
Activity。
onResume() 在 Activity 从 Pause 状态转换到 Active 状态时被调⽤。在这个⽅法中访问SQLite数据库,通过Adapter将条⽬的内容和添加时间显⽰在ListView中。看代
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) {
setContentView(R.layout.activity_main);
mList = (ListView) this.findViewById(R.id.list);
mNotedb = new NoteDb(this);
dbreader = ReadableDatabase();
mList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
Intent intent = new Intent(MainActivity.this,ShowContent.class);
intent.putExtra(NoteDb.ColumnIndex(NoteDb.ID)));
intent.putExtra(NoteDb.ColumnIndex(NoteDb.CONTENT)));
intent.putExtra(NoteDb.ColumnIndex(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() {
selectDb();
}
}
为了⽅便程序扩展,以后有可能添加图⽚、视频内容 我⽤了⾃定义的Adapter 继承⾃BadeAdapter:

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。