Summary of knowledge points used in some work

Source: Internet
Author: User

Start writing after entering the companyProgramIs nested in a browser and used as a plug-in. This application has been used for a lot of time. In fact, the application is not too difficult, but its own foundation is too bad. It will not write anything and it will be necessary to check the information, which will delay a lot of time, this also taught me how to copyCodeEndless troubles.

This application mainly includes requests to the server for data, parsing XML, updating the UI in the background, and processing some screen response events, which are all very basic. Many problems have been encountered in the code implementation process. Some problems have not been solved yet, so we need to consider them urgently.

Question 1: Time formatting

The data obtained from the server is in the format of "14:12:36". You need to convert the data to an intuitive "today" format, the main cause of the problem is that you are not familiar with the Java or Android event conversion class. This problem has been solved currently.

Public Static String getlocaltime (context, string time ){
// Retrieve the year, month, and day, and compare the strings.
String str_curtime = Dateformat. Format ( " Yyyy-mm-dd " , New Date (). tostring ();
Int Result = Str_curtime.compareto (time. substring ( 0 , Time. indexof ( " " )));
If (Result > 0 ){
Return Context. getstring (R. String . Haha_yesterday) + Time. substring (time. indexof ( " " ), Time. lastindexof ( " : " ));
} Else If (Result = 0 ){
Return Context. getstring (R. String . Haha_today) + Time. substring (time. indexof ( " " ), Time. lastindexof ( " : " ));
} Else {
Return Time;
}
}

This method is not professional enough, but meets the needs. If there is a better way for netizens to post it, we recommend that you use the android dateformat class instead of Java simpledateformat or others, The conversion efficiency of Android dateformat classes is much higher than that of Java.

Question 2: How to Reduce the layout layers of itemview in listview

I have already written this article. As mentioned in my previous blog, although using the merge label as the top-level label can reduce the level of view, all the attributes set in the merge label will not work, you need to set it in the code.

Question 3: Set the bold format for Chinese Characters

Mpublisher = (Textview) This . Findviewbyid (R. Id. haha_publisher );
/**
* Set the font of Chinese characters to bold. In the XML file, set textstyle = bold to only valid for English characters.
* Set the Chinese font to bold as follows:
* */
Textpaint TP = Mpublisher. getpaint ();
TP. setfakeboldtext ( True );

Question 4: Tips

Use of the settag method of the view. In the adapter, for example, bind the data with the view, for example, to process the onclick and onitemclick events, you sometimes need to obtain various data of the itemview, for example, positon, ID, text, etc. After settag in the getview method, we can use the gettag method to retrieve the information description object from other places and directly use the data in it, it makes the code more clean and tidy.

Question 5: There are several ways to asynchronously load images.

First:

In the getview method of the adapter, determine whether the view has drawabel. If not, send a message and start an asynchronous task to download the image.

Summary: this is not a good practice, because in addition to drawing the view on the current display interface, the listview will continuously call the getview method when sliding up or down to determine whether the drawable exists, resource waste. In addition, many asynchronous tasks may be enabled. Even if a queue is used, the cost of Continuously Determining whether the image has been downloaded is increased.

Second:

Bind a scroll event to the listview. The advantage of this operation is that several views on the current interface are loaded each time without downloading the images of all items. The advantage is that the traffic is reduced, the downside is that when the page is loaded for the first time, the scrolling event will not be triggered, and the first page of images will not be downloaded. I prefer to do this. In this case, the queue should be added.

Mlistview. setonscrolllistener ( New Onscrolllistener (){

@ Override
Public Void Onscrollstatechanged (abslistview view, Int Scrollstate ){
If (Scrollstate = Onscrolllistener. scroll_state_idle ){
Int Count = View. getchildcount ();
If (Count > 0 ){
Arraylist < Integer > List = New Arraylist < Integer > ();
For ( Int Index = 0 ; Index < Count; index ++ ){
View vi = View. getchildat (INDEX );
If (Vi Instanceof Hahaitem ){
Hahaitem item = (Hahaitem) VI;
Hahaiteminfo info = (Hahaiteminfo) item. gettag ();
If (Madapter. containskey (info. usericonurl )) Continue ;
List. Add (item. ID );
}
}
Imageloader Loader = New Imageloader (mhandler, list );
Mxtaskmanager. getinstance (). excutetask (loader );
}
}
}

@ Override
Public VoidOnscroll (abslistview view,IntFirstvisibleitem,
IntVisibleitemcount,IntTotalitemcount ){

}
});

Third: when loading a play page, enable a thread to download all images.

In the asynchronous task of request data, parse the XML file, encapsulate the data, store the data to the collection in the adapter, and store the data in a queue at the same time. This queue cannot be blocked, if it is blocked, asynchronous tasks that request data may be blocked. At the same time, this queue must be thread-safe; otherwise, we have to manually implement synchronization.

Using the concurrentlinkedqueue class, he has implemented synchronization himself.

After adding all the data to the queue, we need to check whether the asynchronous download of the image has been completed. If yes, we need to start a new image download task. If not, it automatically retrieves the data in the queue. We process the data differently, which can reduce the frequency of enabling and disabling threads and improve efficiency.

Private Class Imageloader Extends Mxasynctaskrequest {
Private Concurrent1_queue < String > Queue;

Public Imageloader (handler, Int Position ){
Super (Handler, getpic_taskid );
}
Public Imageloader (handler, concurrent1_queue < String > Queue ){
Super (Handler, getpic_taskid );
This . Queue = Queue;
}
@ Override
Protected Void Dotaskinbackground (){
While ( True ){
If (Queue. isempty ()){
Break ;
}
Log. I (tag, " Dotaskinbackground is working: Size " + Queue. Size ());
String URL = Queue. Poll ();
Log. I (tag, " Dotaskinbackground is working: URL " + URL );
Try {
Drawable DW = Apputils. loaddrawable (URL );
Madapter. mdrawablemap. Put (URL, DW );
} Catch (Ioexception e ){
E. printstacktrace ();
}
}

Sendmessage (msgid );
}
}

Question 6: how to set dip and PX?

In the XML layout file, we can set both PX and dip. The system has converted us, but how can we deal with it in the code, many methods only provide the method for setting PX, such as setpadding, and do not provide the method for setting dip. It is reasonable to say that dip should be converted to PX, but after conversion, on a high-screen mobile phone, a problem occurs, the view becomes larger, and the data in dimen is directly obtained for use. Instead, there will be no problems, which makes me very confused. Does the method provide automatic conversion, this has to be studied.

Question 7: Layout of textview?

If multiple lines of text are displayed in textview, and the text contains numbers and English letters, a typographical problem occurs. How can this problem be solved?

The main classes involved here include paint,

Breaktext method, which can detect the number of texts displayed on a line.

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.