Arrayadapter, Simpleadapter, baseadapter are often used in Android programming. Among them, Arrayadapter, Simpleadapter is simpler, the custom effect is few, but baseadapter use is very common.
1Asynchttpclient.get (Constants.healthy_news_list,NewAsynchttpresponsehandler () {2 @Override3 Public voidOnsuccess (intI, header[] headers,byte[] bytes) {4 Try {5String str =NewString (Bytes, "gb2312");6Document document =jsoup.parse (str);7Element BODY =document.body ();8Elements tdwidth = Body.select ("td[width=500]");9Elements tdheight = Tdwidth.select ("td[height=40]");TenElements links = tdheight.select ("a"); One for(Element link:links) { AString title =Link.text (); -String url = link.attr ("href"); -Healthy =NewHealthy (); the Healthy.settitle (title); - healthy.seturl (URL); - Healthylist.add (healthy); - } +Healthyadapter Healthyadapter =NewHealthyadapter (healthyactivity. This, healthylist); - Pulltorefreshlistview.setadapter (healthyadapter); +Pulltorefreshlistview.setonitemclicklistener (NewAdapterview.onitemclicklistener () { A @Override at Public voidOnitemclick (adapterview<?> Parent, view view,intPositionLongID) { -String title = Healthylist.get (intID). GetTitle (); -String URL = healthylist.get ((intID). GETURL (); -Intent Intent =NewIntent (healthyactivity. This, Healthydetailactivity.class); -Intent.putextra ("url", URL); - startactivity (intent); in } - }); toElements Link_more = Body.select ("a[href^=http://health.enorth.com.cn/system/more/]"); +Textview.settext (link_more.attr ("href")); -}Catch(IOException e) { theLOG.D ("File Read exception:", E.getmessage ()); * } $ Panax Notoginseng } - the @Override + Public voidOnFailure (intI, header[] headers,byte[] bytes, throwable throwable) { AToast.maketext (healthyactivity. This, "Please check your network connection", Toast.length_short). Show (); theLOG.D ("Asynchttpclient fail:", Throwable.getmessage ()); + } -});
This code uses the Get () method of the asynhttpclient to get the byte stream of a page. Asynchttpclient I accidentally imported android-async-http-x.x.x.jar when I imported the jar package of other open source projects, according to one of its name I guess it is used for multithreading to initiate HTTP requests, after reading the relevant blog online, in fact, is Httpclie NT and new runnable in one package, so that it can be directly in the main thread, but compared with asynctask, the disadvantage is that there is no control over the network connection process of OnPreExecute (). For the return byte stream garbled problem, read the source code after reading the Web page to find that it is gb2312, which is a kind of commonly used in the mainland of Chinese character coding, it is said to display about 98% of Chinese characters. The setting character is encoded as String str = new string (bytes, "gb2312"). Web page code is HTML language (Hypertext Markup Language), in my opinion is actually like XML is a markup language, where Java 5 provides a tool class Htmlparse used to parse HTML text, but it is rarely updated and relatively complex, so the use of very little. Jsoup is a very popular HTML parsing framework on the web, which is used very much in web crawlers. The approximate use is to use a variety of tags to parse the text. The bottom-level should be the use of the compiler principle of the state machine some knowledge, compiling the principle of my study is not good, here also say nothing. This morning encountered a big problem is to add (healthy) to healthylist, I used the log will parse the title and URL to print out, after the addition will healthy print out, after the For loop, I manually printed the healthy in turn, and it was very strange that the last time I printed it was all the same value, which puzzled me for a long time. Later, inspired by my classmates, I found that each cycle would have to be a new healthy object that would solve the problem. Before I was not in the loop every time the new one healthy object is because I think this will save memory, the result is to incur such a problem, in fact, in-depth analysis of the object in Java and the value of the transfer and reference (address) is not clear caused by the delivery. Then I read this blog, which is very detailed. http://6924918.blog.51cto.com/6914918/1283761 can be summarized as two points, when the basic type is passed as a parameter, it is a copy of the transfer value, no matter how you change the copy, the original value is not changed. Object in Java as a parameterWhen a number is passed, a copy of the object's address in memory is copied to the parameter. One of the Mldnjava videos is dedicated to this explanation, but it seems that I have forgotten. So when I don't have a new object in the For loop, he stores a copy of the address of the healthy object in healthylist every time the loop prints the correct content, but when it executes to the last loop, the object in the address is the last, And the address of this content has been copied n times, and are stored in the healthylist, so finally I do manual printing when the last time to join the content, and when I print helathylist, the printed address is the same, But I didn't think of it at the time, and I was thinking that every time new object was too much memory, it seemed that the basics of Java were really important.
Record problems with your Android learning process