IdleSun's Weblog

August 27, 2011

How to make a TextView scrollable with a Scrollbar

Filed under: Android,Java,UI,XML — idlesun @ 8:08 am
Tags: , , , ,

 

This post has been moved to my new blog.

 

Advertisements

July 29, 2011

Android Parcelable Example 3 – Subclass, Inheritance

Filed under: Android,Java — idlesun @ 7:04 am
Tags: , , , , ,

 

This post has been moved to my new blog.

 

July 15, 2011

Android Parcelable Example 2 – Sub-object and List

Filed under: Android,Java — idlesun @ 7:48 pm
Tags: , , , , ,

 

This post has been moved to my new blog.

 

July 1, 2011

Android Parcelable Example 1 – boolean and enum

Filed under: Android,Java,Uncategorized — idlesun @ 6:44 pm
Tags: , , ,

 

This post has been moved to my new blog.

 

April 8, 2011

How to make PreferenceActivity use non-default SharedPreferences

Filed under: Android,Java — idlesun @ 5:49 am
Tags: , ,

 

This post has been moved to my new blog.

 

December 12, 2010

Android Handler and Message Tutorial

Filed under: Android,Java — idlesun @ 1:44 am

 

This post has been moved to my new blog.

 

November 18, 2010

How to use an Java enum like an integer (for Android Message.what)

Filed under: Android,Java — idlesun @ 4:52 am

Java enum is powerful. However, it is tricky if you want to use it as integer just like in C/C++. Actually, it is not easy at all because Java enum elements are objects. So it is best to use Java enum as enum. But what about the case you need to pass it back an forth as an integer. One example is when you want to use enum for a ‘what’ of Android Message. Lets say you have defined the enum, WhatAbout, like this:

enum WhatAbout { ART, LIFE, MONEY, WORLD };

And you want to sendMessage to someHandler like this:

someHandler.sendMessage(updater.obtainMessage(WhatAbout.ART.ordinal(), 0, 0));

As you can see enum’s ordinal() method solves the issue. Now look at the receiver side. Here is the code for handleMessge() method of someHander

  public void handleMessage(Message msg) {
    if (msg.what == WhatAbout.ART.ordinal()) {
      // Do something about ART
    } else if (msg.what == WhatAbout.LIFE.ordinal()) {
      // Do something about LIFE
    } else ...
  }

It will work, but looks ugly. Using switch must be better. However, unfortunately, the ordinal() method cannot be a case expression because it is not a constant expression. So here is my trick to get around that restriction and use switch:

  static WhatAbout[] wa = WhatAbout.values();

  public void handleMessage(Message msg) {
    try {
      switch(wa[msg.what]) {
      case ART:
        // Do something about ART
        break;
      case LIFE:
        // Do something about LIFE
        break;
      ...
      }
    } catch (Exception x) {
      // Handle ArrayIndexOutOfBoundsException exception
    }
  }

The point is having an enum value array instantiated and use it in the switch expression.

November 11, 2010

Android SharedPreferences.edit().commit() lies! Not really.

Filed under: Android,Java — idlesun @ 5:37 am

 

This post has been moved to my new blog.

 

January 12, 2010

Gson – Complex Java Object with enum and list to JSON

Filed under: Java,JSON — idlesun @ 6:08 pm

Google GSON is pretty powerful with serializing a complex java object to json in one shot. This example shows the serialization  of a complex object that has a enum type member and nested list of simple objects:

public class TestMain {
	static enum Gender {Male, Female, NotKnown}

	static class Entry {
		public String name = "";
		public String phone = "";
		public Gender gender = Gender.NotKnown;
		public int birthYear;

		Entry(String name, String phone, Gender gender,int birthYear) {
			this.name = name;
			this.phone = phone;
			this.gender = gender;
			this.birthYear = birthYear;
		}
	}
	static class Contacts {
		public String name = "";
		public List entries = new ArrayList();
		Contacts(String name) {
			this.name = name;
		}
	}

	public static void main(String[] args) {
		// build contacts object
		Contacts contacts = new Contacts("My Contacts");
		contacts.entries.add(new Entry("John Doe", "123-234-3456", Gender.Male, 66));
		contacts.entries.add(new Entry("Jane Doe", "234-345-4567", Gender.Female, 67));

		// convert the contacts object to JSON string
		Gson gson = new Gson();
		System.out.println(gson.toJson(contacts));
	}
}

Here is the output:

{ 
  "name":"My Contacts",
  "entries":[
    {"name":"John Doe","phone":"123-234-3456","gender":"Male","birthYear":66},
    {"name":"Jane Doe","phone":"234-345-4567","gender":"Female","birthYear":67}
  ]
}

Nice, huh? Of course, check Gson User Guide for more details.

Blog at WordPress.com.