简略引见一下性能:简略记事本只能减少文字内容,首页用ListView显示全副保存的事项和保存的工夫,减少页面减少内容,点击首页的ListView可以查看内容,无锡外卖平台,校园点餐平台开发,删除内容。html
先看一下运转效果:
java
先创建数据库:android
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:sql
有显示目录的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:app
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; } }