开发者

Android Development general question

I'm learning how to write Android apps and am having the same issues I've had with learning languages in general - data types are impossible to deal with!

Why is it so hard to convert data from one type to another?? Why does everything use complex, custom data sets? The issue I'm trying to figure out right now is a good example: Android Location Services API. When I finally get the location data from Android its in this special Location type that doesn't convert to a basic string at all. Using the built in types like whatever.getLatitude() doesn't work either. Again, why is it so hard for API/OS to provide a simple conversion routine??

Example apps tend to never help, even if I get one compiled it 开发者_如何学Pythonstill crashes. As a beginner I don't care about making a button work or anything, I want to see location data in a text box - that would be a better intro example app then hello,world.

If anyone would like to give me pointers on how to deal with different data types in Android that will be helpful, but honestly I posted this to hear reasons why data conversion have to be such a headache.

Thanks all!


Because Java is Object Oriented Language, and Android API is based on OOP principles. And it's not problem - it's provide benefits to you. For example, your Location can provide too much information and methods on them. Look at the API http://developer.android.com/reference/android/location/Location.html API provides access to attitude, latitude, longitude, etc. You can try to call toString() methods for debug or logging purposes if you want to look stringified presentation for all these fields. But it's not good way for any other purposes.


Whenever I am consuming data that is in a format I'm not a big fan of or contains more information than I need or the information needs to be "translated" in a more suitable form for my purpose, I create my own object model and a translation layer. For example, when I consume a webservice that gives me json or xml data that I need to parse. I just create a package (or namespace if I'm in the .Net world) called model and one called translators. The model objects are the data exactly as my app wants it. The translator classes take in the data and return my object. The same would work for object-to-object translation and not just raw data like json and xml. It takes very little time whatsoever, and when you write yourself helpers (for example, I have a JSONHelper class I created in Java for when I'm working with Android), you just copy it over from project to project making your workflow even faster.


This is basic Java/OOP but this is also a question about mapping data-values. For example, you cannot simply attempt to calculate a value of two numbers that are not two numbers.

"6" (a string) does not equal the number value of 6 unless you tell the class to recognize that.

using bastardized Java/C#

String x = new String ("6");
int xi = 6;

return !x.equals( xi)// would actually fail 

instead

return xi == Integer.parseInt(x);

So each part of the app and class needs to be able to understand what you are passing as parameters. Get that book Tim recommends or look up some of the various Java tutorials on the Oracle website (or on the web).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜