Udacity Android Lesson 2 Learning Notes
/titer1/archimedes of dry Goods shop chores
Source: Https://code.csdn.net/titer1
Contact: 13,073,161,968
Disclaimer: This document is licensed under the following protocols: Free reprint-Non-commercial-non-derivative-retention Attribution | Creative Commons by-nc-nd 3.0, reproduced please specify the author and source.
Tips:https://code.csdn.net/titer1/pat_aha/blob/master/markdown/android/
Here we start from 2-17, so that our demo can display list, but the network communication code in the main thread, so we need to improve him.
Refresh.. Async is not optional
When the activity component is terminated by something, such as rotating the screen. Data transfer will be affected
TODO activity declaration cycle better
- Not use refresh button
- Not effect by the activity lifecycle
Menu +buttons
In principle is
The code, in short, is to update the item content under Menu.xml
After that, consider the refresh button, where the modified location is the layout-related XML.
After running the demo, the button is not displayed and the code that created the button is not currently updated
If the button is not displayed, please give the API in the check graph
The relevant code for initializing the Refresh button and the button response is given above
Not perfect, continue to improve
Run the above code directly, crash
The reason is the permissions issue (no Internet access), Catherine very interesting manufacturing errors, but also to us how to use Logcat to diagnose the error of the app, really give people to fish!
Permission mechanism sandbox
Solution: Currently, only the required permissions are requested
Inevitably ask for high permissions and can think of no alternative
-Start the application with intent (e.g. camera)
-Ways to share data: for example, using the intent and content provider schema application sandbox
Internet rights control
The user can dynamically deny permission to the given.
In short, I want the user to decide whether or not to give some kind of permission,
Then I need to give the Androidmanifest.xml
android.permission.INTERNET
After you get Internet permissions,
Catherine then at the code level to explain the JSON data calls, and at the command line using LOAGCAT to see the JSON data is not parsed, in fact, this is a demonstration of the Debug method
At this point, our demo can successfully receive the data.
Custom parameters (Zip code)
- The previous request string is a concatenation of strings
- Now the request string is UriBuilder build
- Detection method, Logcat
Fetchweathertask with parameters
Look down at UrlBuilder's true face and debug interface.
JSON parsing JSON properties
- JSON Fromater Reader
Simply organize your needs
- Max Tempature?
Interesting to find, Udacity Web site even have the online Java JSON practice
Analysis of a typical tree-like data structure
!! Jason Parsing
After understanding the basics, we will have 3 functions to process the JSON
Helper method
-Formate Date
-Fromae The max/min temperature
-Transfer the JSON string to array we need (here we use the parsing method of the last section)
Almost all of the methods are called in the Fetchweathertask Doinbackground
When the function is correct, we will get the following output
Update the Adpater + UI
This is the 2-42 lesson and 2-43 ' s content.
Hotkey is "Ctrl+o", it can give method of the parent class
我眼中的arrayAdapter就是前台进程(asyncTask)和后台通讯的桥梁
Analysis Arrayadapter
Policies for data sharing
-Change the previous private arrayadapter to a global variable
If the initialization arrayadpater is incorrect, there will be unsupported operation exception
The above description is updated after the actual code is run
Update OnPostExecute
Next we will update OnPostExecute
, here we will see the update of the ListView
更新listview的两种策略- 低版本(- 高版本(>=honeyComb),使用addall
Schema diagram recall
Process of data update causing view update
Say Adapter.nofitfydatasetchanged () is automatically called and we look at the truth.
In layman's source code
You need to see the Arrayadpater.java under the framework
- The Add method, called the nofitfydatasetchanged
- AddAll method, also called the nofitfydatasetchanged
- In terms of efficiency, AddAll only calls once nofitfydatasetchanged for a set of data, making it more efficient
这其实就是传说中的观察者设计模式
tips:androidframecode是个宝箱!某种程度上说,对androidframework的了解程度决定android程序员的水平
Take a screenshot
You can use the features of Android Debug Monitor to help developers to mark progress
At this point, the Refresh function is completed,
Or give Catherine a picture, good fun program
Catherine ready for a big sleep, lesson3 see you.
Other
Video of 47,48,49,50,51 11 before the repetition.
So you can take a nap.
Todo
Chapter II after all the code runs, I'll update some chapters
Udacity's Android Learning Note: Lesson 2