Android needs to know small details

Source: Internet
Author: User

1. layout file naming cannot contain uppercase letters, special characters. myimageview.xml error.

2. Meaning of the Simpleadapter adapter parameter:

3. GridView

Three important attributes of the GridView:

4. Global access to Context tips

In the development of Android, a lot of places have to use the context of this class object, such as: the need to pop up a toast, the need to start the event, the need to send the broadcast, the operation of the database when needed, the use of notifications when needed.

If it is in activity, then it is easy to get the context object, because the activity itself inherits the context, and it is available directly. But for more complex logic, the code is not in the activity, so getting the context is not that easy.

Here's a tip to tell you:

Android provides a application class that automatically initializes the class whenever the application starts. And we can customize a application class to make it easier to manage some of the global state information in the program, such as the global context.

1, create a new class MyApplication inherit application.

Package com.example.networktest;

Public class MyApplication extends application {

private static context context;

@Override

Public void OnCreate () {

context = Getapplicationcontext ();

}

Public static Context GetContext () {

return context;

}

}

2, you can specify it under the <application> tab of the Androidmanifest.xml file, as shown in the code below:

  1. <manifest xmlns:android="http://schemas.android.com/apk/res/android"
  2. package="Com.example.networktest"
  3. android:versioncode="1"
  4. Android:versionname="1.0" >
  5. ......
  6. <application
  7. Android:name="Com.example.networktest.MyApplication"
  8. ...... >
  9. ......
  10. </Application>
  11. </manifest>

Note: Be sure to add the full package name when specifying MyApplication, or the system will not be able to find this class.

So that we have implemented a global access to the context of the mechanism, after no matter you want to be anywhere in the project

Using Context, you just need to call Myapplication.getcontext ().

5.Handler relationship with Looper, message android asynchronous load frame: Handler+looper+message

Summary: Handler is responsible for sending messages, Looper is responsible for receiving handler sent messages, and directly to the message back to handler himself.

MessageQueue is a container for storing messages.

6. The transfer between the main thread and the sub-thread needs to use the Setmessage in handler

If you want to update the UI thread in a child thread, you can use Handler.post (runnable);

Activity classes and non-activity classes, such as adapter Baseadapter classes, can be passed activity.this by means of a transitive construction method; Such as:

Mimgadapter = new Imageadapter (this, Mimgs,
Mcurrentdir.getabsolutepath ());//absolute path to the current folder

Public Imageadapter (context context, list<string> Mdatas, String dirpath) {
This.mdirpath = dirpath;//Parent Path
This.mimgpaths = mdatas;//All pictures
Minflater = Layoutinflater.from (context);
This.context = context;
Mselectedimg.clear ();
}

Android needs to know small details

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.