Android Program take notes(Add & List only)
ListNotes Activity
package com.ramki.takenotes;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class ListNotes extends Activity implements OnClickListener {
Button bAdd, bDelete, bUpdate;
EditText etNotes;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.listnotes);
TextView tv = (TextView) findViewById(R.id.list);
etNotes = (EditText) findViewById(R.id.etnotes);
bAdd = (Button) findViewById(R.id.badd);
bAdd.setOnClickListener(this);
DBAdapter ListRec = new DBAdapter(this);
ListRec.open();
String getInfo = ListRec.getInfo();
ListRec.close();
tv.setText(getInfo);
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.badd:
String notes2 = etNotes.getText().toString();
DBAdapter WriteDB = new DBAdapter(ListNotes.this);
WriteDB.open();
WriteDB.writeInfo(notes2);
WriteDB.close();
break;
}
}
}
DBAdapter
package com.ramki.takenotes;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
public class DBAdapter {
public static final String ROWID = "rowid";
public static final String FIELDNOTES = "notes";
private static final String DBNAME = "takenotes1.db";
private static final String DBTABLE = "takenotes";
private static final int DBVERSION = 1;
private final Context ourContext;
private SQLiteDatabase ourDB;
private DBHelper ourHelper;
private class DBHelper extends SQLiteOpenHelper{
public DBHelper(Context context) {
// TODO Auto-generated constructor stub
super(context, DBNAME, null, DBVERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
db.execSQL("CREATE TABLE " + DBTABLE + " (" +
ROWID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
FIELDNOTES + " TEXT NOT NULL);"
);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
db.execSQL("DROP TABLE IF NOT EXIST " + DBTABLE);
onCreate(db);
}
}
public DBAdapter(Context c)
{
ourContext = c;
}
public DBAdapter open() throws SQLException
{
ourHelper = new DBHelper(ourContext);
ourDB = ourHelper.getWritableDatabase();
return this;
}
public void close(){
ourHelper.close();
}
//INSERT THE RECORDS INTO TABLE
public long writeInfo(String notes2)
{
// TODO Auto-generated method stub
ContentValues cv = new ContentValues();
cv.put(FIELDNOTES, notes2);
return ourDB.insert(DBTABLE, null, cv);
}
//LIST THE RECORDS FROM TABLE
public String getInfo() {
// TODO Auto-generated method stub
String[] columns = new String[]{ROWID, FIELDNOTES};
Cursor c = ourDB.query(DBTABLE, columns, null,null, null, null,null);
String results = "";
int iRow = c.getColumnIndex(ROWID);
int iNotes = c.getColumnIndex(FIELDNOTES);
for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext()){
results = results + c.getString(iRow) + " " +c.getString(iNotes) + "\n";
}
return results;
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical" >
<EditText
android:id="@+id/etnotes"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:text="Enter the Notes" >
<requestFocus />
</EditText>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<Button
android:id="@+id/badd"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Add" />
<Button
android:id="@+id/bdelete"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Delete" />
<Button
android:id="@+id/bupdate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Update" />
</LinearLayout>
<TableLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<TableRow>
<TextView
android:id="@+id/notes"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="List Notes from DB"
android:textAppearance="?android:attr/textAppearanceMedium" />
</TableRow>
</TableLayout>
<TextView
android:id="@+id/list"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Record not found"
android:textAppearance="?android:attr/textAppearanceMedium" />
</LinearLayout>
ListNotes Activity
package com.ramki.takenotes;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class ListNotes extends Activity implements OnClickListener {
Button bAdd, bDelete, bUpdate;
EditText etNotes;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.listnotes);
TextView tv = (TextView) findViewById(R.id.list);
etNotes = (EditText) findViewById(R.id.etnotes);
bAdd = (Button) findViewById(R.id.badd);
bAdd.setOnClickListener(this);
DBAdapter ListRec = new DBAdapter(this);
ListRec.open();
String getInfo = ListRec.getInfo();
ListRec.close();
tv.setText(getInfo);
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.badd:
String notes2 = etNotes.getText().toString();
DBAdapter WriteDB = new DBAdapter(ListNotes.this);
WriteDB.open();
WriteDB.writeInfo(notes2);
WriteDB.close();
break;
}
}
}
package com.ramki.takenotes;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
public class DBAdapter {
public static final String ROWID = "rowid";
public static final String FIELDNOTES = "notes";
private static final String DBNAME = "takenotes1.db";
private static final String DBTABLE = "takenotes";
private static final int DBVERSION = 1;
private final Context ourContext;
private SQLiteDatabase ourDB;
private DBHelper ourHelper;
private class DBHelper extends SQLiteOpenHelper{
public DBHelper(Context context) {
// TODO Auto-generated constructor stub
super(context, DBNAME, null, DBVERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
db.execSQL("CREATE TABLE " + DBTABLE + " (" +
ROWID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
FIELDNOTES + " TEXT NOT NULL);"
);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
db.execSQL("DROP TABLE IF NOT EXIST " + DBTABLE);
onCreate(db);
}
}
public DBAdapter(Context c)
{
ourContext = c;
}
public DBAdapter open() throws SQLException
{
ourHelper = new DBHelper(ourContext);
ourDB = ourHelper.getWritableDatabase();
return this;
}
public void close(){
ourHelper.close();
}
//INSERT THE RECORDS INTO TABLE
public long writeInfo(String notes2)
{
// TODO Auto-generated method stub
ContentValues cv = new ContentValues();
cv.put(FIELDNOTES, notes2);
return ourDB.insert(DBTABLE, null, cv);
}
//LIST THE RECORDS FROM TABLE
public String getInfo() {
// TODO Auto-generated method stub
String[] columns = new String[]{ROWID, FIELDNOTES};
Cursor c = ourDB.query(DBTABLE, columns, null,null, null, null,null);
String results = "";
int iRow = c.getColumnIndex(ROWID);
int iNotes = c.getColumnIndex(FIELDNOTES);
for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext()){
results = results + c.getString(iRow) + " " +c.getString(iNotes) + "\n";
}
return results;
}
}
XML Layout
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical" >
<EditText
android:id="@+id/etnotes"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:text="Enter the Notes" >
<requestFocus />
</EditText>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<Button
android:id="@+id/badd"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Add" />
<Button
android:id="@+id/bdelete"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Delete" />
<Button
android:id="@+id/bupdate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Update" />
</LinearLayout>
<TableLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<TableRow>
<TextView
android:id="@+id/notes"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="List Notes from DB"
android:textAppearance="?android:attr/textAppearanceMedium" />
</TableRow>
</TableLayout>
<TextView
android:id="@+id/list"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Record not found"
android:textAppearance="?android:attr/textAppearanceMedium" />
</LinearLayout>


