Hi All,
I m a novice in Google Android.. Just started with couple of tutorials
present in Google Android Documentation page.
I have modified the NotePad application present there. I have four
textfields namely, employeeName,employeeAge,
employeeQualification, employeeDept.
I could insert a new record and delete an existing record, but i m
facing problem in editing an existing record. When i click a record it
says, the Application EmployeeInformation has stopped unexpectedly.
Here is my EmployeeInfo class
package com.dreamapp.employeeinfo.activity;

import android.app.ListActivity;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;

import com.dreamapp.employeeinfo.util.EmployeeDAO;

public class EmployeeInfo extends ListActivity {
       private static final int ACTIVITY_CREATE =0;
       private static final int ACTIVITY_EDIT =1;

       private static final int INSERT_ID = Menu.FIRST;
       private static final int DELETE_ID = Menu.FIRST+1;

       private EmployeeDAO employee;
       private Cursor mCursor;

       private static final String TAG="EmployeeInfo";
   /** Called when the activity is first created. */
   @Override
   public void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       setContentView(R.layout.employee_list);
       employee = new EmployeeDAO(this);
       employee.open();
       fillData();
   }

   public void fillData(){
       mCursor = employee.fetchAllEmployeeInformation();
       startManagingCursor(mCursor);

       String[] from = new String[] {EmployeeDAO.EMP_NAME};

       int[] to = new int[] {R.id.text1};
       SimpleCursorAdapter employeeList =
               new SimpleCursorAdapter(this, R.layout.employee_row,
mCursor,from, to);
       setListAdapter(employeeList);
   }


   public boolean onCreateOptionsMenu(Menu menu){
       super.onCreateOptionsMenu(menu);
       menu.add(0, INSERT_ID, 0, R.string.menu_insert);
       menu.add(0, DELETE_ID, 0, R.string.menu_delete);
       return true;
   }

   public boolean onMenuItemSelected(int featureId, MenuItem item){
       switch(item.getItemId()){
       case INSERT_ID:
               insertEmployeeInformation();
               return true;
       case DELETE_ID:
               employee.deleteEmployeenformation(getListView().
                                getSelectedItemId());
               fillData();
               return true;
       }
       return super.onMenuItemSelected(featureId, item);
   }

   public void insertEmployeeInformation(){
       Intent i = new Intent(this,EmployeeEdit.class);
       startActivityForResult(i, ACTIVITY_CREATE);
   }

   protected void onListItemClick(ListView l, View v, int position,
long id){
       super.onListItemClick(l, v, position, id);
       Cursor c = mCursor;
       Log.w(TAG, "Position in the List "+position);
       c.moveToPosition(position);
       Intent i = new Intent(this,EmployeeEdit.class);
       i.putExtra(EmployeeDAO.KEY_ROW_ID, id);
       i.putExtra(EmployeeDAO.EMP_NAME, c.getString(
 
c.getColumnIndexOrThrow(EmployeeDAO.EMP_NAME)));
       i.putExtra(EmployeeDAO.EMP_AGE, id);
       i.putExtra(EmployeeDAO.EMP_QUALIFICATION, c.getInt(
 
c.getColumnIndexOrThrow(EmployeeDAO.EMP_QUALIFICATION)));
       i.putExtra(EmployeeDAO.EMP_QUALIFICATION, c.getString(
 
c.getColumnIndexOrThrow(EmployeeDAO.EMP_QUALIFICATION)));
       i.putExtra(EmployeeDAO.EMP_DEPT, c.getString(
 
c.getColumnIndexOrThrow(EmployeeDAO.EMP_DEPT)));
       startActivityForResult(i, ACTIVITY_EDIT);
   }

   protected void onActivityResult(int requestCode, int resultCode,
                          Intent intent){
       super.onActivityResult(requestCode, resultCode, intent);
       Bundle extras = intent.getExtras();
       switch(requestCode){
       case ACTIVITY_CREATE:
               String employeeName =
extras.getString(EmployeeDAO.EMP_NAME);
               int employeeAge = extras.getInt(EmployeeDAO.EMP_NAME);
               String employeeQualification =
extras.getString(EmployeeDAO.EMP_QUALIFICATION);
               String employeeDept =
extras.getString(EmployeeDAO.EMP_DEPT);
               employee.insertEmployeeInformation(employeeName,
employeeAge,
employeeQualification, employeeDept);
               fillData();
               break;
       case ACTIVITY_EDIT:
               Long rowId = extras.getLong(EmployeeDAO.KEY_ROW_ID);
               if(rowId != null){
                       String updateEmployeeName =
extras.getString(EmployeeDAO.EMP_NAME);
                       int updateEmployeeAge =
extras.getInt(EmployeeDAO.EMP_NAME);
                       String updateEmployeeQualification =
extras.getString(EmployeeDAO.EMP_QUALIFICATION);
                       String updateEmployeeDept =
extras.getString(EmployeeDAO.EMP_DEPT);
                       employee.updateEmployeeInformation(rowId,
updateEmployeeName,
                                        updateEmployeeAge,
updateEmployeeQualification,
updateEmployeeDept);
                       fillData();
                       break;
               }
       }

   }
}

and My EmployeeEdit class

package com.dreamapp.employeeinfo.activity;


import com.dreamapp.employeeinfo.util.EmployeeDAO;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.util.Log;

public class EmployeeEdit extends Activity {

       public EditText mEmployeeName;
       public EditText mEmployeeAge;
       public EditText mEmployeeQualification;
       public EditText mEmployeeDept;
       public Long mRowId;

       public static final String TAG = "EmployeeEdit";

       protected void onCreate(Bundle savedInstanceState){
               super.onCreate(savedInstanceState);
               setContentView(R.layout.employee_edit);
               Log.w(TAG, "OnCreate Method in EmployeeEdit Class");
               mEmployeeName = (EditText) findViewById(R.id.name);
               mEmployeeAge = (EditText) findViewById(R.id.age);
               mEmployeeQualification = (EditText)
findViewById(R.id.qualification);
               mEmployeeDept = (EditText) findViewById(R.id.dept);

               Button confirmButton = (Button)
findViewById(R.id.confirm);
               mRowId = null;
               Bundle extras = getIntent().getExtras();
               if(extras !=null){
                       String name =
extras.getString(EmployeeDAO.EMP_NAME);
                       int age = extras.getInt(EmployeeDAO.EMP_AGE);
                       String qualification =
extras.getString(EmployeeDAO.EMP_QUALIFICATION);
                       String dept =
extras.getString(EmployeeDAO.EMP_DEPT);
                       mEmployeeName.setText(name);
                       mEmployeeAge.setText(age);
                       mEmployeeQualification.setText(qualification);
                       mEmployeeDept.setText(dept);
                       Log.w(TAG, "Preparing the View");
               }

               confirmButton.setOnClickListener(new
View.OnClickListener(){

                       public void onClick(View v) {
                               Log.w(TAG, "OnClick Button Listener");
                               Bundle bundle = new Bundle();
                               bundle.putString(EmployeeDAO.EMP_NAME,
mEmployeeName.getText().toString());
               bundle.putString(EmployeeDAO.EMP_AGE,
mEmployeeAge.getText().toString());
               bundle.putString(EmployeeDAO.EMP_QUALIFICATION,
mEmployeeQualification.getText().toString());
               bundle.putString(EmployeeDAO.EMP_DEPT,
mEmployeeDept.getText().toString());
               Log.w(TAG, "Values Present in Bundle"+bundle);
               if (mRowId != null) {
                   bundle.putLong(EmployeeDAO.KEY_ROW_ID, mRowId);
               }
               Intent mIntent = new Intent();
               mIntent.putExtras(bundle);
               setResult(RESULT_OK, mIntent);
               finish();

                       }

               } );
       }

}


and finally my EmployeeDAO class

/*
 * This class Primarily takes care of Inserting, Updating and
Selecting
 * and Deleting an Employee information. Basic CRUD operations are
 * covered here
 * @author Sabari
 */

package com.dreamapp.employeeinfo.util;

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;
import android.util.Log;

public class EmployeeDAO {

       public static final String EMP_NAME = "employeeName";
       public static final String EMP_AGE = "employeeAge";
       public static final String EMP_QUALIFICATION =
"employeeQualification";
       public static final String EMP_DEPT = "employeeDept";
       public static final String KEY_ROW_ID = "_id";

       private static final String TAG="EmployeeDAO";
       private DatabaseHelper mDbHelper;
       private SQLiteDatabase mDb;

       /*
        * Variables needed for Database
        *
        */
       private static final String DATABASE_CREATE =
                "create table employeeMaster (_id integer primary key
autoincrement, "
       + "employeeName text not null, employeeAge int not null," +
       "employeeQualification text not null, employeeDept text not
null);";
       private static final String DATABASE_NAME = "data";
   private static final String DATABASE_TABLE = "employeeMaster";
   private static final int DATABASE_VERSION = 2;

   private final Context mCtx;

       private static class DatabaseHelper extends SQLiteOpenHelper{

               DatabaseHelper(Context context){
                       super(context, DATABASE_NAME, null,
DATABASE_VERSION);
               }

               @Override
               public void onCreate(SQLiteDatabase db) {
                       db.execSQL(DATABASE_CREATE);
               }

               @Override
               public void onUpgrade(SQLiteDatabase db, int
oldVersion, int
newVersion) {
                       Log.w(TAG, "Upgrading database from version " +
oldVersion + " to "
                   + newVersion + ", which will destroy all old
data");
                       db.execSQL("DROP TABLE IF EXISTS
employeeMaster");
               }
       }

       public EmployeeDAO(Context ctx){
               this.mCtx = ctx;
       }

       public EmployeeDAO open() throws SQLException{
               mDbHelper = new DatabaseHelper(mCtx);
               mDb = mDbHelper.getWritableDatabase();
               return this;
       }

       public void close(){
               mDbHelper.close();
       }

       public long insertEmployeeInformation(String employeeName, int
employeeAge,
                               String employeeQualification,String
employeeDept){
               ContentValues initiaValues = new ContentValues();
               initiaValues.put(EMP_NAME, employeeName);
               initiaValues.put(EMP_AGE, employeeAge);
               initiaValues.put(EMP_QUALIFICATION,
employeeQualification);
               initiaValues.put(EMP_DEPT, employeeDept);
               Log.w(TAG, " Values Before Inserting an Employee
Information" +
initiaValues);
               return mDb.insert(DATABASE_TABLE, null, initiaValues);
       }

       public boolean deleteEmployeenformation(long rowId){
               Log.w(TAG, "Deleting this Employee Information "+
rowId);
               return mDb.delete(DATABASE_TABLE, KEY_ROW_ID + "=" +
rowId , null) >
0;
       }

       public Cursor fetchAllEmployeeInformation(){
               Log.w(TAG, "Fetching all EmployeeInformation from DB");
               return mDb.query(DATABASE_TABLE, new String[]
{KEY_ROW_ID,EMP_NAME,
                               EMP_AGE,EMP_QUALIFICATION,EMP_DEPT},
null, null, null, null,
                               null);
       }

       public Cursor fetchEmployeeInformation(long rowId){
               Cursor mCursor =
                                       mDb.query(DATABASE_TABLE, new
String[] {KEY_ROW_ID,EMP_NAME,
 
EMP_AGE,EMP_QUALIFICATION,EMP_DEPT}, KEY_ROW_ID + "=" +
                                       rowId,null, null, null, null,
null);
               if(mCursor!= null){
                       mCursor.moveToFirst();
               }
               return mCursor;
       }

       public boolean updateEmployeeInformation(long rowId, String
employeeName,
                       int employeeAge, String employeeQualification,
String employeeDept)
{
               ContentValues updateValues = new ContentValues();
               updateValues.put(EMP_NAME, employeeName);
               updateValues.put(EMP_AGE, employeeAge);
               updateValues.put(EMP_QUALIFICATION,
employeeQualification);
               updateValues.put(EMP_DEPT, employeeDept);
               Log.w(TAG, "Updating this Information "+updateValues);
               return mDb.update(DATABASE_TABLE, updateValues ,
KEY_ROW_ID + "=" +
rowId , null) > 0;

       }

}

and one more thing is i have added many logger statements throughout
my application, but these aren`t getting printed on Anroid Console. Is
there any other technique to print these debuggigng statements.
       Log.w(TAG, "Position in the List "+position);

Any help for the above mentioned issue is highly appreciated...

Thanks in Advance

Thanks
JavaAndroid
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
[EMAIL PROTECTED]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to