Udacity-android Study Notes: lesson 2, udacityandroid

Source: Internet
Author: User
Tags addall

Udacity-android Study Notes: lesson 2, udacityandroid
Udacity android lesson 2 Study Notes

Prepared by: Taobao stores/titer1/Archimedes
Source: https://code.csdn.net/titer1
Contact: September 1307316
Statement: This article uses the following agreement for authorization: Free Reprint-non commercial-Non derivative-keep the signature | Creative Commons BY-NC-ND 3.0, reprint please indicate the author and the source.
Tips: https://code.csdn.net/titer1/pat_aha/blob/master/Markdown/android/

Here we start from 2 to 17, so far our demo can display the List, but the network communication code is placed in the main thread, so we need to improve it.

Refresh... async is not optional

When the activity component is terminated by something, such as rotating the screen. Data transmission will be affected

Todo activity declaration cycle better way

  • Not use refresh button
  • Not effect by the activity lifecycle
Menu + buttons

In principle

The code is embodied. In short, the item content under Menu. xml is updated.

After that, consider the refresh button and change the location to layout-related xml.
After running the demo, the button is not displayed. The code for creating the button is not updated yet.


If the button is not displayed, check the api shown in the figure.


The above code initializes the refresh button and button response

Not perfect. Continue to improve

Run the above Code directly, crash


The reason is that the permission problem (no permission to apply for the internet), catherine is very interesting about making errors, and also gives us how to use logcat to diagnose app errors. It is really a best practice!

Permission mechanism sandbox

Solution: currently, only the required permissions are requested.

The requested permissions are inevitable. You can check whether there are any alternative solutions.
-Use intent to start an application (such as a camera)
-Data sharing methods: for example, using the intent and content provider Architecture Application sandbox

Internet permission Control

Permissions that can be dynamically denied by users.

In short, I want users to decide whether to grant certain permissions,
In androidManifest. xml

android.permission.INTERNET

After obtaining INTERNET permissions,
Catherine then explained the call of JSON data at the code level, and used loagcat in the command line to see the JSON data that has not been parsed. In fact, this is a demo of a debug method.

So far, our demo can successfully receive data.

Custom parameters (ZIP code)
  • The previous request string is String concatenation.
  • The current request string is built by uribuilder
  • Detection method, logcat
Fetchweathertask with Parameters

Let's take a look at the true face of urlbuilder, and there are debugging interfaces.

Json parsing json attributes
  • Json fromater Reader
    Summary of requirements
  • Max tempature?
    Interestingly, the udacity Website still has online java json exercises.


Analysis of a typical tree data structure

!! Jason parsing

After understanding the basics, we will have three functions to process json

Helper method
-Formate date
-Fromae the max/min temperature
-Transfer the JSON string to array we need (the parsing method of the previous section is used here)

Almost all methods are called in the doInBackground of FetchWeatherTask.

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 parent class

In my eyes, arrayAdapter serves as a bridge between the foreground process (asyncTask) and background communication.
Analysis of arrayAdapter

Data sharing policy
-Change the previous private arrayAdapter to a global variable.

If arrayAdpater Initialization is incorrect, an unsupported operation exception will occur.

The preceding 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

Update listview's two policies-lower version (
Structural chart memories

Process of view update caused by data update

If adapter. nofitfyDatasetChanged () is automatically called, let's look at the truth.

Source code

You need to view arrayadpater. java under the framework

  • NofitfyDatasetChanged
  • The addAll method also calls nofitfyDatasetChanged
  • In terms of efficiency, addAll only calls nofitfyDatasetChanged once for a group of data, so it is more efficient.
This is actually the design pattern of the legendary observer.
Tips: android frame code is a treasure chest! To some extent, understanding of the android framework determines the level of android programmers.
Take a screenshot

You can use the built-in functions of android debug monitor to help developers identify the progress.

Now, the refresh function is complete,
Let's take a photo of catherine, happy programmer.

Catherine is going to have a sleep. See lesson3.

Others

Video repetition before, 50, and 51.
So you can take a nap.

Todo

Chapter 2 after all the code is run, I will update some sections

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.