Saturday, August 9, 2014

Task Manager App using SQLiteDatabase

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Task Details" />

    <EditText
        android:id="@+id/task"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ems="10"
        android:hint="Enter the Task" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Due Date"
        android:inputType="date"/>

    <EditText
        android:id="@+id/duedate"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ems="10"
        android:hint="Enter the Date"/>
      <!--   android:inputType="date" -->


    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <Button
            android:id="@+id/add"
            style="?android:attr/buttonStyleSmall"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Add" />

        <Button
            android:id="@+id/view"
            style="?android:attr/buttonStyleSmall"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="View" />
       
       

    </LinearLayout>
   
   
    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Enter the Row ID" />

   
    <EditText
        android:id="@+id/etrowid"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ems="10"
        android:hint="Enter the Row ID"
        android:inputType="number"/>
   
        <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >


        <Button
            android:id="@+id/getinfo"
            style="?android:attr/buttonStyleSmall"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Get Info" />
       
       
        <Button
            android:id="@+id/update"
            style="?android:attr/buttonStyleSmall"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Modify" />
       
       
        <Button
            android:id="@+id/delete"
            style="?android:attr/buttonStyleSmall"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Delete" />
    </LinearLayout>
   

</LinearLayout>

----------------------------------------------------------------------------------------------------------
<?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" >

    <TableLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" >

        <TableRow>

            <TextView
                android:id="@+id/task"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="Task"
                android:ems="10" >
            </TextView>
  <TextView
                android:id="@+id/date1"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="Date"
                android:ems="10" >

 
            </TextView>
           
        </TableRow>

     
   
    </TableLayout>
  <TextView
                android:id="@+id/getinfo"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:text="get the stored info."
                android:ems="10" >

           
            </TextView>
</LinearLayout>

----------------------------------------------------------------------------------------------------------
package com.ramki.taskmanager;

import android.app.Activity;
import android.app.Dialog;
import android.content.Intent;
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 AddTask extends Activity implements OnClickListener {

Button bAdd, bView, bgetInfo, bUpdate, bDelete;
EditText etTask, etDate, etRowid;

@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.addtask);

bAdd = (Button) findViewById(R.id.add);
bView = (Button) findViewById(R.id.view);

etTask = (EditText) findViewById(R.id.task);
etDate = (EditText) findViewById(R.id.duedate);

bgetInfo = (Button) findViewById(R.id.getinfo);
bUpdate = (Button) findViewById(R.id.update);
bDelete = (Button) findViewById(R.id.delete);

etRowid = (EditText) findViewById(R.id.etrowid);

bAdd.setOnClickListener(this);
bView.setOnClickListener(this);

bgetInfo.setOnClickListener(this);
bUpdate.setOnClickListener(this);
bDelete.setOnClickListener(this);
}

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.add:

boolean didItWork = true;

try {

String task = etTask.getText().toString();
String duedate = etDate.getText().toString();

DBAdapter entry = new DBAdapter(AddTask.this);

entry.open();
entry.createTask(task, duedate);
entry.close();
} catch (Exception e) {
didItWork = false;
String error = e.toString();
Dialog d = new Dialog(this);
d.setTitle("Error Message");
TextView tv = new TextView(this);
tv.setText(error);
d.setContentView(tv);
d.show();

} finally {
if (didItWork) {
Dialog d = new Dialog(this);
d.setTitle("Message");
TextView tv = new TextView(this);
tv.setText("Successfully Updated");
// tv.setTextAlignment(ALIGN_CENTER);
d.setContentView(tv);
d.show();
}
}

break;
case R.id.view:
Intent intent = new Intent("com.ramki.taskmanager.LISTTASK");
startActivity(intent);
break;

case R.id.getinfo:
try {

String s = etRowid.getText().toString();
long l = Long.parseLong(s);
DBAdapter db = new DBAdapter(this);
db.open();

// db.getInfo();
String getTask = db.getTask(l);
String getDate = db.getDate(l);

db.close();

etTask.setText(getTask);
etDate.setText(getDate);
} catch (Exception e) {

String error = e.toString();
Dialog d = new Dialog(this);
d.setTitle("Error Message");
TextView tv = new TextView(this);
tv.setText(error);
d.setContentView(tv);
d.show();

}
break;

case R.id.update:
try {
String uRow = etRowid.getText().toString();
long lRow = Long.parseLong(uRow);

String utask = etTask.getText().toString();
String uduedate = etDate.getText().toString();

DBAdapter dUpdate = new DBAdapter(this);
dUpdate.open();

dUpdate.UpdateEntry(lRow, utask, uduedate);
//

dUpdate.close();

} catch (Exception e) {

String error = e.toString();
Dialog d = new Dialog(this);
d.setTitle("Error Message");
TextView tv = new TextView(this);
tv.setText(error);
d.setContentView(tv);
d.show();

}

break;

case R.id.delete:
try {
String uRow1 = etRowid.getText().toString();
long lRow1 = Long.parseLong(uRow1);

DBAdapter dDelete = new DBAdapter(this);
dDelete.open();

dDelete.DeleteRow(lRow1);

dDelete.close();
} catch (Exception e) {

String error = e.toString();
Dialog d = new Dialog(this);
d.setTitle("Error Message");
TextView tv = new TextView(this);
tv.setText(error);
d.setContentView(tv);
d.show();

}
break;

}
}

}

----------------------------------------------------------------------------------------------------------
package com.ramki.taskmanager;

import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;

public class ListTask extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.listtask);
TextView tv = (TextView) findViewById(R.id.getinfo);
DBAdapter getInfo = new DBAdapter(this);

getInfo.open();

String getData = getInfo.getData();

getInfo.close();
tv.setText(getData);

}

}

----------------------------------------------------------------------------------------------------------
package com.ramki.taskmanager;

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 TASK = "task";
public static final String DUEDATE = "duedate";

private static final String DBNAME = "taskmanager";
private static final String DBTABLE = "task";
private static final int DBVERSION = 1;

private DBHelper ourHelper;
private SQLiteDatabase ourDB;
private final Context ourContext;



private class DBHelper extends SQLiteOpenHelper{

public DBHelper(Context context) {
super(context, DBNAME, null, DBVERSION);
// TODO Auto-generated constructor stub
}

@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
db.execSQL("CREATE TABLE " + DBTABLE + " (" +

ROWID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
TASK + " TEXT NOT NULL, " +
DUEDATE + " 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();
}

public long createTask(String task2, String duedate2) {
// TODO Auto-generated method stub
ContentValues cv = new ContentValues();
cv.put(TASK, task2);
cv.put(DUEDATE, duedate2);
//ourDB.insert(DBTABLE, null, cv);
return ourDB.insert(DBTABLE, null, cv);


}

public String getData() {
// TODO Auto-generated method stub
String[] columns = new String[]{ROWID, TASK, DUEDATE};
Cursor c = ourDB.query(DBTABLE, columns, null, null, null, null, null);
String result = "";

int iRow = c.getColumnIndex(ROWID);
int iTask = c.getColumnIndex(TASK);
int iDueDate = c.getColumnIndex(DUEDATE);

for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext()){
result = result + c.getString(iRow) + " " + c.getString(iTask) + " " + c.getString(iDueDate) + "\n";
}


return result;
}

public String getTask(long l) throws SQLException{
// TODO Auto-generated method stub
String[] columns = new String[]{ROWID, TASK, DUEDATE};
Cursor c = ourDB.query(DBTABLE, columns, ROWID + "=" + l, null, null, null, null);
if (c != null){
c.moveToFirst();
String task = c.getString(1);
return task;
}
return null;
}


public String getDate(long l) throws SQLException{
// TODO Auto-generated method stub
String[] columns = new String[]{ROWID, TASK, DUEDATE};
Cursor c = ourDB.query(DBTABLE, columns, ROWID + "=" + l, null, null, null, null);
if (c != null){
c.moveToFirst();
String date = c.getString(2);
return date;
}
return null;
}

public void UpdateEntry(long lRow, String utask, String uduedate) throws SQLException{
// TODO Auto-generated method stub
ContentValues cvUpdate = new ContentValues();
cvUpdate.put(TASK, utask);
cvUpdate.put(DUEDATE, uduedate);
//ourDB.insert(DBTABLE, null, cv);
ourDB.update(DBTABLE, cvUpdate, ROWID + "=" + lRow, null);
}

public void DeleteRow(long lRow1) throws SQLException{
// TODO Auto-generated method stub
ourDB.delete(DBTABLE, ROWID + "=" + lRow1, null);

}



















/*private DBHelper ourHelper;
private SQLiteDatabase ourDB;
private final Context ourContext;



private static class DBHelper extends SQLiteOpenHelper{

public DBHelper(Context context) {
super(context, DBNAME, null, DBVERSION);
// TODO Auto-generated constructor stub
}

@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub

}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub

}

}

public DBAdapter(Context c){
ourContext = c;

}

public DBAdapter open(){
ourHelper = new DBHelper(ourContext);
ourDB = ourHelper.getWritableDatabase();
return this;
}
*/






}

----------------------------------------------------------------------------------------------------------


No comments:

Post a Comment