Use a database to implement storage of data.
The following is a small example, write a diary.
The effect is as follows:
When no data is displayed in the ListView, we need to tell the user that there is no data.
There are two methods:
1.
Activity inherits Listactivity, which is written in the layout file as follows:
<textview android:id= "@id/android:empty" android:layout_width= "wrap_content" android:layout_ height= "Wrap_content" android:text= "You lazy ah, have not started to write a diary"/> <listview <span style= "White-space: Pre "></span> android:layout_width=" wrap_content " android:layout_height=" Wrap_content "
2.
<span style= "White-space:pre" ></span>if (Adapter.isempty () &&textview==null) {TextView = new TextView (Mainactivity.this); Textview.settext ("The host is lazy, nothing Left"); Textview.setvisibility (View.gone); ((ViewGroup) listview.getparent ()). AddView (TextView); Listview.setemptyview (TextView);} Else{listview.setadapter (Simplecursoradapter);}
Use of the database (Android itself comes with SQLite, and if you want to use a different database, you have to use Web Server):
Inherit Sqliteopenhelper.
Public DBHelper (context context)//database name, database version public void OnCreate (Sqlitedatabase db)////CREATE database public void Onupgrade ( Sqlitedatabase db, int oldversion, int newversion)//Update database
The specific code is as follows:
Cn.edu.bzu.diary.activity
Mainactivity.java
public class Mainactivity extends Activity {listview listview; Cursor diaries;//cursor TextView TextView = null, @Overrideprotected void OnCreate (Bundle savedinstancestate) { Super.oncreate (savedinstancestate); Setcontentview (r.layout.activity_main); listview = (ListView) Findviewbyid ( R.id.listview); Refreshlist (); This.registerforcontextmenu (listview);} /** * Refresh Adapter */public void Refreshlist () {Diarydao Diarydao = new Diarydao (this);d iaries = Diarydao.getalldairies (); Simplecursoradapter simplecursoradapter = new Simplecursoradapter (Mainactivity.this, R.layout.item, diaries, new String[] {"title", "Created"}, new int[] {r.id.title, r.id.created}); if (Simplecursoradapter.isempty () && Textview==null) {TextView = new TextView (mainactivity.this); Textview.settext ("The host is lazy, nothing remains"); textview.setvisibility (View.gone); ((ViewGroup) listview.getparent ()). AddView (TextView); Listview.setemptyview (TextView);} Else{listview.setadapter (Simplecursoradapter);}} @Overridepublic void Oncreatecontextmenu (contextmeNu menu, View v,contextmenuinfo menuinfo) {menu.setheadertitle ("operation"), Menu.add (0, 1, menu.none, "edit"), Menu.add (0, 2, menu . NONE, "delete"); Super.oncreatecontextmenu (menu, V, menuinfo);} @Overridepublic boolean oncontextitemselected (MenuItem Item) {//Oncontextitemselectedadaptercontextmenuinfo Menuinfo = (adaptercontextmenuinfo) item.getmenuinfo (); switch (Item.getitemid ()) {case 1:intent Intent = new Intent (); Bundle bundle = new bundle (); Bundle.putint ("id", (int) menuinfo.id); Intent.putextras (bundle); Intent.setclass ( Mainactivity.this, Diaryeditacitivity.class); startactivity (intent); break;case 2:diarydao dao = new Diarydao ( Mainactivity.this);d ao.delete ((int) menuinfo.id);//menuinfo.id is consistent with the database ID refreshlist (); break;} return super.oncontextitemselected (item);} /** * in exit Journal Add, use the Lifecycle Refresh adapter */@Overrideprotected void Onresume () {super.onresume (); Refreshlist ();} @Overridepublic boolean Oncreateoptionsmenu (Menu menu) {getmenuinflater (). Inflate (R.menu.main, menu); return true;} /** * Go to "Journal add page" via Menu */@OverRidepublic boolean onoptionsitemselected (MenuItem item) {switch (Item.getitemid ()) {case R.id.save:intent Intent = new in Tent (); Intent.setclass (Mainactivity.this, Diaryaddacitivity.class); startactivity (intent); break;} return super.onoptionsitemselected (item);}}
Diaryaddacitivity.java
public class Diaryaddacitivity extends Activity {Diarydao Diarydao = new Diarydao (diaryaddacitivity.this); Button button; EditText edittext,edittext2; @Overrideprotected void OnCreate (Bundle savedinstancestate) {super.oncreate ( Savedinstancestate); Setcontentview (r.layout.diaryadd_acitivity); editText = (EditText) Findviewbyid (r.id.et_ DIARYADD);//Title EDITTEXT2 = (EditText) Findviewbyid (r.id.et02_diaryadd);//Content button = (button) Findviewbyid (r.id.but);} @Overridepublic boolean Oncreateoptionsmenu (Menu menu) {//Inflate the menu; This adds items to the action bar if it is PR Esent.getmenuinflater (). Inflate (r.menu.diary_add_acitivity, menu); return true;} public void Click (View view) {String title = Edittext.gettext (). toString (); String content = Edittext2.gettext (). toString ();D iary Diary = new Diary (title, Content, Tool.datechange (new Date ()));D Iarydao Diarydao = new Diarydao (this);d Iarydao.save (diary); Finish ();}}
Diaryeditacitivity.java
public class Diaryeditacitivity extends Activity {Diarydao Diarydao = new Diarydao (diaryeditacitivity.this); Button button; EditText edittext,edittext2;int ID; @Overrideprotected void onCreate (Bundle savedinstancestate) {super.oncreate ( Savedinstancestate); Setcontentview (r.layout.diaryadd_acitivity); editText = (EditText) Findviewbyid (r.id.et_ DIARYADD);//Title EDITTEXT2 = (EditText) Findviewbyid (r.id.et02_diaryadd);//Content button = (button) Findviewbyid (r.id.but); Bundle bundle = This.getintent (). Getextras (); if (bundle!=null) {id = bundle.getint ("id");D iary Diary = Diarydao.getdiarybyid (ID); Edittext.settext (Diary.gettitle ()); Edittext2.settext (Diary.getcontent ());}} @Overridepublic boolean Oncreateoptionsmenu (Menu menu) {//Inflate the menu; This adds items to the action bar if it is PR Esent.getmenuinflater (). Inflate (r.menu.diary_add_acitivity, menu); return true;} public void Click (View view) {String title = Edittext.gettext (). toString (); String content = Edittext2.gettext (). toString ();D iary Diary = new Diary (title, Content, Tool.datechange (new Date ()));d Iary.setid (ID);D Iarydao diarydao = new Diarydao (this); Diarydao.update (diary); Finish ();}}
Cn.edu.bzu.diary.daoDiarydao.java
public class Diarydao {private DBHelper dbhelper;private sqlitedatabase sqlitedatabase;public Diarydao (context context) {dbhelper = new DBHelper (context);} /** * Add Journal * @param diary */public void Save (Diary diary) {String sql = "INSERT into diary (title,content,created) VALUES ( ?,?,?)"; Sqlitedatabase = Dbhelper.getwritabledatabase (); Sqlitedatabase.execsql (Sql,new string[] {diary.getTitle (), Diary.getcontent (), Diary.getdatetime ()}); /** * Delete Journal by ID * * @param ID * Journal ID number */public void delete (Integer id) {sqlitedatabase = dbhelper.getwritabled Atabase ();//Get the same DB instance Sqlitedatabase.execsql ("Delete from diary where _id=?", new object[] {ID});} /** * Update Journal * * @param diary */public void Update (Diary diary) {Sqlitedatabase = Dbhelper.getwritabledatabase ();//Get the same DB instance Sqlitedatabase.execsql ("Update diary set title=?,content=?,created=?") Where _id=? ", new object[] {diary.gettitle (), Diary.getcontent (), Diary.getdatetime (), Diary.getid ()});} /** * Returns a cursor, or you can use the comment section to return a list collection (using the SimpLeadapter), where cursors are used, mainly because the ID in the * adaptercontextmenuinfo is consistent with the database * @return cursor */public cursor getalldairies () {//diar Y diary = null;//list<diary> diaries = new arraylist<diary> (); sqlitedatabase = Dbhelper.getreadabledatabase (); cursor cursor = sqlitedatabase.rawquery ("Select * from Diary", NULL);/* * while (Cursor.movetonext ()) {String title = * CU Rsor.getstring (Cursor.getcolumnindex ("title")); String content = * Cursor.getstring (cursor.getcolumnindex ("content")); String created = * Cursor.getstring (Cursor.getcolumnindex ("created")); Diary = new * Diary (title, content, created); Diaries.add (diary); } */return cursor; /** * Gets the total number of records */public Long Count () {Long Count = 0;sqlitedatabase = Dbhelper.getreadabledatabase (); cursor cursor = sqlitedatabase.rawquery ("SELECT count (*) from diary", null); Cursor.movetofirst (); count = Cursor.getlong (0); return count;} Public Diary Getdiarybyid (int id) {sqlitedatabase = Dbhelper.getwritabledatabase ();D iary Diary = null; cursor cursor = SqlitEdatabase.rawquery ("Select * from Diary where _id=?", new string[] {id + "}"); if (Cursor.movetofirst ()) {String title = Cursor.getstring (Cursor.getcolumnindex ("title")); String content = cursor.getstring (Cursor.getcolumnindex ("content")); String created = cursor.getstring (Cursor.getcolumnindex ("created"));d iary = new Diary (title, content, created);} return diary;}}
Cn.edu.bzu.diary.dbDBHelper.java
public class DBHelper extends sqliteopenhelper{public static final String database_name= "diary.db";p ublic static final int VERSION = 1; Database name, database version public DBHelper (context context) {Super (context, database_name, NULL, version);//TODO auto-generated constructor stub}//Create DATABASE, field: ID (primary key), title,content,created; @Overridepublic void OnCreate (Sqlitedatabase db) {//TODO Auto-generated method Stubdb.execsql ("CREATE Table Diary (_id integer primary key autoincrement,title varchar (20), Content varchar (+), created) "); Update for database @overridepublic void Onupgrade (sqlitedatabase db, int oldversion, int newversion) {//TODO auto-generated Metho D stub}}
Cn.edu.bzu.diary.entityDiary.java
public class Diary {private Integer id;private string title;private string Content;private string Datetime;public Diary () { }public Diary (String title,string content,string datetime) {this.title = Title;this.content = Content;this.datetime = datetime;} Public Integer GetId () {return ID;} public void SetId (Integer id) {this.id = ID;} Public String GetTitle () {return title;} public void Settitle (String title) {this.title = title;} Public String getcontent () {return content;} public void SetContent (String content) {this.content = content;} Public String GetDateTime () {return datetime;} public void SetDateTime (String datetime) {this.datetime = datetime;} @Overridepublic String toString () {return "Diary [title=" + title + ", content=" + content + ", datetime=" + datetime + "]" ;}}
Cn.edu.bzu.diary.toolsTool.java
public class Tool {/** * time display style * @param date * @return */public static String datechange (date date) {SimpleDateFormat Simp Ledateformat = new SimpleDateFormat ("yyyy mm DD day hh: mm: ss sec"); return Simpledateformat.format (date);}}
Code
Android Development uses SQLite to write diaries