Hi all, in this post we will make the note taking app made in previous post better .
We will make the app to store multiple notes and shall display them in our main screen.
At the very first we will change the app icon. For this put an icon file inres/drawable-* folders ( one for each size) and change the string in AndroidManifest.xml as below.
android:icon=”@drawable/note256″
Here the notes256 is file name of icon I put in drawable-* folders.
Notice the new icon above.
Now to store multiple notes we will need database. We will follow the pattern championed by masters and create the notes class to store well, notes
and NotesDatabase to create, store and retrieve notes.
Here is the notes class
public class Notes {
int _id;
String _note;
public Notes(){
}
public Notes(int id, String note){
this._id = id;
this._note = note;
}
public Notes(String note){
this._note = note;
}
public int getID(){
return this._id;
}
public void setID(int id){
this._id = id;
}
public String getNote(){
return this._note;
}
public void setNote(String note){
this._note = note;
}
}
Code is self explanatory it just store notes and id for database operations and onscreen UI.
Now here is the NotesDatabase.java
We will understand the code by step by step
private static final int DATABASE_VERSION = 1;
private static final String DATABASE_NAME = "notesDatabase";
private static final String TABLE_NOTES = "notes";
private static final String KEY_ID = "id";
private static final String KEY_NOTE = "note";
These fields define the database name, table name and table key name.
String CREATE_NOTES_TABLE= "CREATE TABLE " + TABLE_NOTES+ "(" + KEY_ID + " INTEGER PRIMARY KEY," + KEY_NOTE + " TEXT" + ")"; db.execSQL(CREATE_NOTES_TABLE);
Above statements in onCreate method prepares the create query and when executed by db.execSQL creates the table.
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(KEY_NOTE, notes.getNote());
db.insert(TABLE_NOTES, null, values);
db.close();
Above ones in addNote takes a Notes object and store its values in database.
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.query(TABLE_NOTES, new String[] { KEY_ID,
KEY_NOTE }, KEY_ID + "=?",
new String[] { String.valueOf(id) }, null, null, null, null);
if (cursor != null)
cursor.moveToFirst();
Notes notes = new Notes(Integer.parseInt(cursor.getString(0)),
cursor.getString(1));
return notes;
List<Notes> notesList = new ArrayList<Notes>();
String selectQuery = "SELECT * FROM " + TABLE_NOTES;
SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);
if (cursor.moveToFirst()) {
do {
Notes notes = new Notes();
notes.setID(Integer.parseInt(cursor.getString(0)));
notes.setNote(cursor.getString(1));
notesList.add(notes);
} while (cursor.moveToNext());
}
return notesList;
Above listed ones get all notes from database.
There are more methods but we use only the above listed code.
In EditNoteActivity.java change the onClickSave method to be like this
public void onClickSave(View theButton) { EditText text = (EditText) findViewById(R.id.editText1); NotesDatabase db =new NotesDatabase(this); db.addNote(new Notes(text.getText().toString())); finish(); }
This will save the note to database.
Now change the onCreate method of MyActivity to be like this.
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
NotesDatabase db =new NotesDatabase(this);
List<Notes> notes = db.getAllNotes();
for ( Notes nt: notes) {
final LayoutInflater inflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
TextView b = (TextView) inflater.inflate(R.layout.textviews,null);
b.setText(nt.getNote());
LinearLayout lLayout = (LinearLayout)findViewById(R.id.layout1);
lLayout.addView(b);
}
}
This requires some explanation as this dynamically adds the TextView defined in textviews.xml file and adds them to main activity.
final LayoutInflater inflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER
Above code creates a inflater object whose inflate method gets the text view.
TextView b = (TextView) inflater.inflate(R.layout.textviews,null);
This gets the TextView from below textview.xml
<?xml version="1.0" encoding="utf-8"?> <TextView xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/textview1" android:text="Press to close" android:layout_width="wrap_content" android:layout_height="wrap_content"/>
addView adds the TextView to the layout dynamically.
LinearLayout lLayout = (LinearLayout)findViewById(R.id.layout1);
Now build and run the app
Even at this stage there is lot to be desired. We will keep improving in next post.
Thanks to Sai Geetha for inflator trick, and Ravi Tamada, Lars Vogel for database pattern.

