Instead of doing something like:
Double Hi = Double.valueOf( (EditText) findViewById(R.id.editText1)).getText().toString())
I used to do the standard casting like the answer by Oli but got tired of handling errors and such. So I wrote a whole class of Casting here is an example of what I did with doubles.
public class Cast {
/**
* Base number cast, return the value or default
* @param object - generic Object
* @param defaultValue - default value to give if Object is null
* @return - returns type Number
*/
private static Number castImpl(Object object, Number defaultValue) {
return (object!=null && object instanceof Number) ? (Number)object : defaultValue;
}
/**
* Base double cast, return the value or default
* @param object - generic Object
* @param defaultValue - default value to give if Object is null
* @return - returns type double
*/
public static double cast(Object object, double defaultValue) {
return castImpl(object, defaultValue).doubleValue();
}
}
This will allow you use a default value too, here is how yo use it.
Cast.cast("3", 1.0);
I've even done this for Arrays too to convert from int to float arrays...