teaching machines

leggitns Homework 2: When Click Listeners Strike Back (TaskMan)

October 19, 2011 by . Filed under cs491 mobile, fall 2011, postmortems.

As you may gather from the title, click listeners were acting up in my project.

 

The intent of this project was to create a very simple Task Tracker that would allow users to use a list view to see a list of tasks, click a task to get deeper details and to edit the task, and provide a checkbox, that while not having any real functionality, allows users to mark of tasks they consider complete.  Because my previous assignment also used databases and a list view I went with a somewhat simple route this application.

And then it all went horribly wrong.

After completing a majority of my coding I went to run through the program and ran into an bug.  The click listener failed to function.  Clicking my “add new task” tab simply did nothing.  It just sat there.  I honestly don’t know what went wrong here.

 

This is the main activity for the project, as you should note, it is (very) similar to how the Rattler application was set up.

 

package edu.uwec.cs.leggitns;

import android.app.AlertDialog;
import android.app.ListActivity;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends ListActivity implements OnItemClickListener {

    private static int REQUEST_CHANGE = 123;

    private TaskDatabase db;
    private ArrayAdapter<Task> adapter;
    private Task updatedTask;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        db = ((TaskManApplication) getApplication()).getTaskDatabase();

        updatedTask = null;
        adapter = new TaskAdapter(this);
        adapter.add(new Task("Add new task", null));
        Task[] tasks = db.getTasks();
        for (Task task : tasks) {
            adapter.add(task);
        }

        setListAdapter(adapter);
        getListView().setOnItemClickListener(this);
    }

    

    public void onItemClick(AdapterView<?> parent, View view, int position,
            long id) {
        Toast.makeText(this, position + "", Toast.LENGTH_SHORT).show();

        if (position == 0) {
            addNewTask();
        } else {
            Task updatedTask = adapter.getItem(position);
            Intent intent = new Intent(this, TaskActivity.class);
            intent.putExtra("taskId", updatedTask.getId());
            startActivityForResult(intent, REQUEST_CHANGE);
        }
    }
    

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == REQUEST_CHANGE) {
            Task newVersion = db.getTask(updatedTask.getId());
            updatedTask.setTitle(newVersion.getTitle());
            updatedTask.setDetails(newVersion.getDetails());
            adapter.notifyDataSetChanged();
        }
    }

    private void addNewTask() {
        AlertDialog.Builder dialog = new AlertDialog.Builder(this);
        dialog.setTitle("New Task");
        final EditText titleBox = new EditText(this);
        titleBox.setHint("title");
        dialog.setView(titleBox);
        final EditText detailsBox = new EditText(this);
        detailsBox.setHint("add details here");
        dialog.setView(detailsBox);
        dialog.setPositiveButton("Create",
                new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                        Task task = new Task(titleBox.getText().toString(),
                                detailsBox.getText().toString());
                        long id = db.insert(task);
                        task.setId(id);
                        adapter.add(task);
                    }
                });
        dialog.show();

    }
}

 

Unfortunately because I can’t quite figure out why the click listener is acting up, I’m left with a nearly completed project that simply doesn’t work.  In addition to the main activity, I have a simple activity used for viewing the task, and an activity for editing the task (once again modeled after the editing in the Rattler app).  Because I got a late start on this project (I really should get on these much sooner) I didn’t implement such things like deleting a task, creating multiple task lists (ie. completed list and incomplete list), among other features.  And simply for the sake of showing that I did indeed have code structured for other things I’ll include a few code snippets after this paragraph.

 

package edu.uwec.cs.leggitns;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class TaskActivity extends Activity{
    
    private Task task;
    private TaskDatabase db;
    
    @Override
    public void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.task);
        
        long id = getIntent().getLongExtra("taskId", -1);
        
        db = ((TaskManApplication) getApplication()).getTaskDatabase();
        task = db.getTask(id);
        
        TextView titleText = (TextView) findViewById(R.id.taskTitle);
        titleText.setText(task.getTitle());
        
        TextView detailsText = (TextView) findViewById(R.id.taskDetails);
        detailsText.setText(task.getDetails());
        
        Button editButton = (Button) findViewById(R.id.editButton);
        editButton.setOnClickListener(new OnClickListener(){
            public void onClick(View v) {
                Intent intent = new Intent(v.getContext(), EditActivity.class);
                intent.putExtra("taskId", task.getId());
            }

            
        });
    }
    
    
}
package edu.uwec.cs.leggitns;

import android.app.Application;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class TaskDatabase {
    
    private static String DB_NAME = "tasks.db";
    private static int DB_VERSION = 234;
    private static String TABLE_TASKS = "tasks";
    private static String COLUMN_TASKS_ID = "_id";
    private static String COLUMN_TASKS_TITLE = "title";
    private static String COLUMN_TASKS_DETAILS = "details";
    
    private SQLiteDatabase db;
    
    /**
     * Private constructor used because of singleton pattern
     * @param context
     */
    private TaskDatabase(Context context) {
        OpenHelper helper = new OpenHelper(context, DB_NAME, DB_VERSION);
        db = helper.getWritableDatabase();
    }
    
    
     private static TaskDatabase soleInstance = null;  
    
     /**
      * Public method to return the single instance of the database, creates an instance if one does not exist currently
      * @param app
      * @return TaskDatabase
      */
     public static TaskDatabase getDatabase(Application app) {
        if (soleInstance == null) {
          soleInstance = new TaskDatabase(app);
        }
        return soleInstance;
      }
      
     /**
      * Method to pull a single Task from the database
      * @param id
      * @return Task
      */
      public Task getTask(long id){
          String[] columns = {
                  COLUMN_TASKS_ID,
                  COLUMN_TASKS_TITLE,
                  COLUMN_TASKS_DETAILS          
          };
          Cursor cursor = db.query(TABLE_TASKS, columns, COLUMN_TASKS_ID + " = ?", new String[] {"" + id}, null, null, null);
          
          Task task = null;
          if(cursor.moveToNext()){
              task = new Task(cursor.getLong(0), cursor.getString(1), cursor.getString(2));
          }
          
          return task;
      }
      
      /**
       * Method to pull out all the tasks in the database
       * @return Task[]
       */
      public Task[] getTasks(){
          String[] columns = {
                  COLUMN_TASKS_ID,
                  COLUMN_TASKS_TITLE,
                  COLUMN_TASKS_DETAILS          
          };
          Cursor cursor = db.query(TABLE_TASKS, columns, null, null, null, null, null);
          
          Task[] tasks = new Task[cursor.getCount()];
          int i = 0;
          while(cursor.moveToNext()){
              tasks[i] = new Task(cursor.getLong(0), cursor.getString(1), cursor.getString(2));
              i++;
          }
          
          return tasks;
      }
    
      /**
       * Method to insert a new task into the database
       * @param task
       * @return
       */
      public long insert(Task task) {
            ContentValues map = new ContentValues();
            map.put(COLUMN_TASKS_TITLE, task.getTitle());
            map.put(COLUMN_TASKS_DETAILS, task.getDetails());
            return db.insert(TABLE_TASKS, null, map);
          }
          
      /**
       * Method to delete a task from the database
       * @param id
       */
      public void delete(long id) {
            db.delete(TABLE_TASKS, COLUMN_TASKS_ID + " = ?", new String[]{id + ""});
          }
          
      /**
       * Method to update a task in the database
       * @param task
       * @return
       */
      public long update(Task task) {
            ContentValues map = new ContentValues();
            map.put(COLUMN_TASKS_TITLE, task.getTitle());
            map.put(COLUMN_TASKS_DETAILS, task.getDetails());
            return db.update(TABLE_TASKS, map, COLUMN_TASKS_ID + " = ?", new String[]{"" + task.getId()});
          }
    
      
      private class OpenHelper extends SQLiteOpenHelper {
          public OpenHelper(Context context,
                          String name,
                          int version) {
          super(context, name, null, version);
        }
        
        @Override
        public void onCreate(SQLiteDatabase db) {
            String q = "CREATE TABLE " + TABLE_TASKS + "(" +
                     "  " + COLUMN_TASKS_ID + " INTEGER PRIMARY KEY AUTOINCREMENT," +
                     "  " + COLUMN_TASKS_TITLE + " TEXT NOT NULL," +
                     "  " + COLUMN_TASKS_DETAILS + " TEXT NOT NULL" +
                     ")";
          db.execSQL(q);
        }

        @Override
        public void onUpgrade(SQLiteDatabase db,
                              int oldVersion,
                              int newVersion) {
          
            db.execSQL("DROP TABLE IF EXISTS " + TABLE_TASKS);
               onCreate(db);
        }
      }
}