Friday, 29 June 2012

Where should new items appear in a listview?

Today I got a bug logged from QA which got me thinking a little bit.  To start with, the bug was about where the new items were appearing after adding them to the list. I have a listview where I the user add, edit and order items, however, whenever a new item is added, it appears at the top of the list. This is one of those issues you throw to the Design Analyst but then I decided to think through it though there was a potential risk of the designer saying “No this is not how it should be..”.

Back to my Issue; What is the best practice while adding items to a listview, should the item appear at the bottom or at the top of the a list?
I think the answer to this highly depends on the several things, One of the things is the sorting order. If the list has an existing sort, then it should be maintained; example if an associated timestamp (assuming that's the order) is chronological, then the new item will appear at the button and vice versa for the reverse chronological ordering. So if there is a sorting order then insert according to the sorting rules. In situations where the user has scrolled to a point that the first/last items cannot be viewed then auto-scroll to the new item and put the new item into context. The same should apply if the list is paged.

If you consider a scenario where the sort is not applied, then you might want to check what the other parts of the application do. This will allow you to maintain conformity within the application,  in addition, the user can intuitively tell where to find a new entry because of the consistency all through. This how I fixed my bug.

Some schools of thought would argue that adding the item at the top would be a good idea. It gives the user an immediate feedback that the item has been added to the list and reduces on the challenge of keeping focus at the bottom of the list.

Monday, 25 June 2012

How to Send an extended ArrayList through an intent


In this article am going to look at how you can pass a custom list from one activity to another.  This being one of the things I had to deal with in my not complete android application, well, I thought it was worthwhile blogging about it.

Let’s first look at what my code supposedly looks like before we jump into what is happening. 

I have Customer class:

import android.os.Bundle;
import android.os.Parcel;
import android.os.Parcelable;


class Customer implements Parcelable {
       
        private int iProperty1;
        private String sProperty2;
       
        public Customer(){
              super();
        }
       
        public Customer(Parcel in){
              super();
              iProperty1 =in.readInt();
              sProperty2=in.readString();
        }
       
          public int Property1() {return iProperty1;}
          public void Property1(int value) {this.iProperty1 = value;}
         
          public String Property2() {return sProperty2;}
          public void Property2(String value) {this.sProperty2 = value;}
         
          @Override
          public void writeToParcel(Parcel dest, int flags) {
            dest.writeInt(iProperty1);
            dest.writeString(sProperty2);
                 
          } 
         
          @Override
          public int describeContents() {
          return 0;
          }
         
          public static final Parcelable.Creator<Customer> CREATOR= new Parcelable.Creator<Customer>() {
            public Customer createFromParcel(Parcel in) {
                  return new Customer(in);
            }

            public Customer[] newArray(int size) {
                  return new Customer[size];
            }
          };
         
         
  }


A customer List,

import android.content.Context;
import java.sql.Array;
import java.util.ArrayList;


class CustomerList extends ArrayList<Customer>{
       private Context context;
       private CustomerDataSource oDataSrc;

       
       public CustomerList(){
             super();
       }
       public CustomerList(Context context){
             super();
             this.context=context;
             oDataSrc = new CustomerDataSource(context);
       }
       
       public void Save(){
            try{
            for(int iItem=0; iItem<this.size(); iItem++ )
            {
                  Customer oCustomer=this.get(iItem);
                  if (oCustomer.IsDirty()){
                        oDataSrc.Save(oCustomer);
                  }
            }
            }
            catch(Exception e){
                  Log.e("ERROR", e.toString());
                  e.printStackTrace();
            }
       }
       public void Load(){
             try{
                  this.addAll(oDataSrc.GetCustomers());
             }
             catch(Exception e){
                        Log.e("ERROR", e.toString());
                        e.printStackTrace();
             }
       }

       public Boolean RemoveCustomer(Customer oCustomer){
             try{
                  oDataSrc.Delete(oCustomer);              
                  this.remove(oCustomer);
                  return true;
             }
             catch(Exception e){
                        Log.e("ERROR", e.toString());
                        e.printStackTrace();
                        return false;
             }
       }
}

Lets begin with the customer class, I know you have realized that it has implemented Parcelable interface. Parcelable is an extremely efficient, low level Protocol which serializes your objects into byte stream. Two methods have also been overridden for similar purpose;

  • writeParcelable(Parcelable, int) -

This method flattens the object data to the Parcel, allowing that object to be reconstructed from the appropriate class loader when later reading. Each element in the object should be parceled individually as I have done with the two class properties.

  • describeContents() -This defines the kind of object you are going to parcel.

To reconstruct the object I have defined a constructor, which will read data from the parcel and map it back to the object.
  public Customer(Parcel in){



I also have an class "class CustomerList" which extends an ArrayList. This basically loads all the customers data from the repository and saves back in. This ArrayList object will be used to sustain the customers data within different activities which was also a bit of  a pain. So how do I do this?



CustomerList oCustomerList = new CustomerList(this);

oCustomerList.load(); // Load all the customers

Intent intent  =  new Intent(this, CustomersActivity.class); //Declare an Intent
intent.putParcelableArrayListExtra("oCustomerList", oCustomerList); //send the Customer list to


startActivityForResult(intent, EditREQUEST_CODE); 


Reconstructing  the list, was tricky because after the object was flattened I could not directly parse it back to a type  CustomerList therefore I had to manuver.

Declare my custome arraylist to persist the original customer list ;

CustomerList oCustomerList = new CustomerList();

The declare another Arraylist since the intent will only return a type arrayList.

ArrayList<Customer> oArraylist = extras.getParcelableArrayList("oCustomerList");

//populate the customer list with the data from arraylist

oCustomerList.addAll(oArraylist);

//Clean up
oArraylist.clear;
oArraylist=null;


Wednesday, 6 June 2012

Custom action management in VS2010 setup project


Apart from developing, some of the other things I do are to create builds for the development team. This involves picking latest code changes from the source safe, compiling them into executable form and eventually building it into a distributable package.  Of course, before releasing the distributable I do test it on a clean machine; by clean I mean a machine that has had no developer or any of the development tools installed.  The testing process will involves deploying on different operating system i.e XP and windows7 including mimicking GPO and Image deployment.

One of the things I have realised over the time I have been a Build master, is that this process is very much ignored until the end of the project.  Discussions relating to extra customizing and shipping usually arise at this time; of course most of the time it’s usually too late to start purchasing new tools, train on them and eventually use them.

This leaves us with a problem on how you can make do with the limited tools and time to customize an installation if such a scenario arises.

Visual studio is one of the kits almost every developer has installed in their machines, it comes with a project setup tool that can be used to do quick and easy install though it is limited in scope. The main drawbacks include:

  •  Having a limited number of dialogs to choose from, which can easily run out on a relatively simple install, example, say that you have a need for a further set of textboxes for a piece of information that contextually belongs on its own and does not look correct if tagged onto one of the other dialogs.
  • The information you can enter is limited. Have you ever needed a dialog with 5 radio buttons? What about a pick-list of options?
  • Build Automation; e.g. you cannot put your project and the VS Setup Project on a build machine and have them built automatically.
  • There is no way to designate a custom action to run with elevated privileges


Just to mention a few…

Developing a setup:

In this post I’ll show you how to create a custom action and to use it in a setup deployment project using C# and Visual Studio 2010. For our sample, we will have windows form application and a deployment project which is ready to build an MSI that will deploy the windows form project to program files.

To start will we will add an extra dialog in our setup then include a custom action to edit the Application Config file (*.exe.config ) with the data collected from the textbox in the dialog during installation.


Here is what my solution looks like:



I have added an Installer class to my Form Project. Do this by right clicking on the solution>Select Add> select Class. From the Add New Item dialogbox select Installer Class.

Here is my app.config file


<?xml version="1.0" encoding="utf-8" ?>
<configuration>
       <appSettings>
              <add key="ConnectionString" value="" />
       </appSettings>
</configuration>





I want to edit the value of the’ ConnectionString’ key during installation.