ListView items using a Layout defined in XML

To customize the View that an item in a ListView uses (we might want a list item to be organized in some view hierarchy) , an easy approach is to implement a ListActivity, then extend BaseAdapter and call setListAdapter() on an instance of it. BaseAdapter only has a few methods to implement (the most importantly of which is getView(), since this is the method that actually constructs the View for the list item). While there are many examples online of doing this by defining the View programmatically, it took me a long time to discover how to use a Layout defined in res/layouts using XML.

For reference, the solution is to use a LayoutInflater.  Here’s an example


@Override
 public View getView(int position, View convertView, ViewGroup parent) {

//here I have stored some strings that I have stored in a SomeObject object, to populate the TextViews defined in my XML layout
//the following methods fetch those values
SomeObject mObject = getItem(position);
String some_string1 = mObject.getSomeString1();
String some_string2 = mObject.getSomeString2();
String some_string3 = mObject.getSomeString3();

//the important step is to get a handle on the LayoutInflater here
LayoutInflater mInflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);

//lets us use the XML layout by "inflating" it
 convertView = mInflater.inflate(R.layout.some_layout, parent, false);

//sets the text of the TextViews in the layout
 ((TextView)convertView.findViewById(R.id.some_textview)).setText(some_string1);
 ((TextView)convertView.findViewById(R.id.some_textview2)).setText(some_string2);
 ((TextView)convertView.findViewById(R.id.some_textview3).setText(some_string3);

 return convertView;
 }

And there we go! Our ListActivity can now make use of our XML layout to construct the Views of the list items.

Posted in Android | Tagged , , , , | Leave a comment

The Singleton Design Pattern

Often in object-oriented programming, we run into situations where we need to have a single instance of an object that can be globally accessed from the whole application. For example, if we needed to use an HTTP client for our whole application, the most efficient approach would be to use a single one maintaining session information. In such cases, the singleton pattern is extremely useful.

In particular to Android, the Singleton can be referenced by all classes in your application, but one must be careful to tear-down the singleton when its no longer needed (for example when the application calls onDestroy()) because the Singleton’s life cycle is not managed by the system.

Why not just make a class with all methods being static? It turns out that this is bad for modularity (since we are forced to use THAT particular class in our code). Also since access is global, we must also worry about concurrency (that is, if the singleton is created at the same time from two independent threads, resulting in the creation of multiple singletons).

Let’s see how we can implement this in Java.

public class Singleton {
   private final Singleton instance = new Singleton();

   private Singleton(){
      //... implementation goes here
      }

   public Singleton getInstance(){
      return this.instance;
      }
}

This is thread-safe because the singleton is created as soon as the class is initialized, so we don’t need to worry that two different threads may try to create it. There is another approach, which allows the singleton to be loaded only when needed.


public class Singelton {

private Singleton(){

//... implementation goes here

}

private static class SingletonHolder {

   private final Singleton instance = new Singleton();

}

public static Singleton getInstance(){

   return SingletonHolder.instance;
}

}

This is also thread-safe because the Singleton (which is final) is created the very first time getInstance() is called. Note in both examples that the constructors are private, to prevent other classes from accessing them.

Posted in Programming | Leave a comment

First post

Hey all. This is where I’m going to post about all the interesting things I come across. We learn more by explaining to others, so hopefully this will further my own understanding too. Enjoy!

Posted in Reflection | 1 Comment