Code Review (2), codereview

Source: Internet
Author: User

Code Review (2), codereview

1. ListView Optimization

BindView function is used to handle data and view Binding operations.

GetItem and getItemId functions are not rewritten as needed, but must be modified according to the actual situation of their own applications. For example, getItemId does not necessarily return position, you can also return the ID of the element at the corresponding position in the list set.

Depending on the actual situation, when the data object to be adapted to the view entry is empty, the view should also be updated if necessary, otherwise, the original data on the attempt entry is displayed to the user instead of the empty data.

Use custom ViewHolder to initialize the control to avoid multiple findViewById

2. UI refresh of controls and desktop plug-ins in the Activity

Currently, operations are too cumbersome and unnecessary. You can monitor network status change events to update the display of the APN and Wi-Fi interfaces, at the same time, you can also obtain the intermediate state, and when the intermediate state is in the control should not be operated

Take refreshing Swicher as an example:

// Defines the public class SwitcherNetworkStatusMonitor extends BroadcastReceiver {@ Overridepublic void onReceive (Context context, Intent intent) {String action = intent. getAction (); if (action. equals (WifiManager. WIFI_STATE_CHANGED_ACTION) {updateSwitchWifiView ();} else if (action. equals (ConnectivityManager. CONNECTIVITY_ACTION) {updateSwitchApnView () ;}}// update the Wifi control function public void updateSwitchWifiV Iew () {if (switchWIFI! = Null) {WifiManager wifiManager = (WifiManager) getSystemService (Context. WIFI_SERVICE); int wifiStatus = wifiManager. getWifiState (); if (wifiStatus = WifiManager. WIFI_STATE_ENABLED) {switchWIFI. setChecked (true);} else if (wifiStatus = WifiManager. WIFI_STATE_DISABLED) {switchWIFI. setChecked (false) ;}}// update the Apn control function public void updateSwitchApnView () {if (switchAPN! = Null) {NetworkManager networkManager = new NetworkManager (getApplicationContext (); if (networkManager. isApnOpened () {switchAPN. setChecked (true);} else {switchAPN. setChecked (false) ;}}// register BroadcastReceiver in the onStart () function and initialize the control @ Overrideprotected void onStart () {super. onStart (); switchAPN = (Switch) findViewById (R. id. apn_switcher); switchWIFI = (Switch) findViewById (R. id. wifi_switcher); mSwitcherNetworkStatusMonitor = new SwitcherNetworkStatusMonitor (); IntentFilter intentFilter = new IntentFilter (); intentFilter. addAction (ConnectivityManager. CONNECTIVITY_ACTION); intentFilter. addAction (WifiManager. response); registerReceiver (mSwitcherNetworkStatusMonitor, intentFilter);} // deregister BroadcastReceiver @ Overrideprotected void onStop () {unregisterReceiver (response); super. onStop ();}

3. understand the meaning of the flags parameter when PendingIntent is created

4. For each class and method, define their roles and division of labor, and place the positions of each variable in the appropriate positions. For example, view variables should not be placed in the broadcast receiver.

5. Internal and external learning

6. Static Functions

Static code block: when the project starts, the project code block will be executed, and the static method will be loaded when the class is loaded, but the static method will be executed only when it is called.

Because the static internal class can only access static members in the external class, and methods such as findViewById are non-static, this class can avoid holding unnecessary objects, long-held resource attempts, such as controls, or even Activity or context environments, will cause these resources to be stuck in the memory for a long time and cannot be released, resulting in Memory leakage.

7. Database Parallel Problems

By extending the Application, you can enable the database when the entire Application starts and shut down the database at the end. The application service runs on the program process. When the program process is terminated, the Service does not exist.

The create ENApplication class inherits the onCreate () and onTerminate () Methods of the Application.

 @Overridepublic void onCreate() {super.onCreate();DatabaseUtil.openDatabase(getApplicationContext());}@Overridepublic void onTerminate() {DatabaseUtil.closeDatabase();super.onTerminate();}

8. when you expand the system view or system service, you should first create your own content for the system during creation, and then execute our operations, we should first destroy our contents and then execute the system destruction operation. For example, when the onCreate () function is used, super. onCreate () is called first to implement its own operations. When onDestroy () is used, the custom content is destroyed before super. onDestroy () is called ()


Code review
Java: two types of writing that do not understand the style

The first condition is clear. Anyone who does not program will understand it.
The second type of efficiency is slightly higher, and it should be less than once in else if.

There are many people in the company. There must be two types of code. However, if there is code review, the first one will be discarded.

So do not hesitate to use the second method with High Efficiency

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.