Android之多线程之HandlerThread

[来源] 达内    [编辑] 达内   [时间]2012-09-18

其实在android的开发中体现了不少java的知识

其实在android的开发中体现了不少java的知识。

< h2 style="margin: 15px auto 2px; padding: 0px; font-size: 1.5em; font-weight: bold; display: block; color: rgb(0, 0, 0); font-family: 微软雅黑; font-style: normal; font-variant: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); ">1.for循环的使用情景 < p style="margin: 5px auto; padding: 0px; text-indent: 0px; color: rgb(0, 0, 0); font-family: 微软雅黑; font-size: 14px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: -webkit-auto; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); "> 在填充ListView上都是与适配器绑定的,我们看一下适配器的继承结构图。

< p style="margin: 5px auto; padding: 0px; text-indent: 0px; color: rgb(0, 0, 0); font-family: 微软雅黑; font-size: 14px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: -webkit-auto; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); ">

< p style="margin: 5px auto; padding: 0px; text-indent: 0px; color: rgb(0, 0, 0); font-family: 微软雅黑; font-size: 14px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: -webkit-auto; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); "> 详情参考Devin Zhang Android之Adapter用法总结这边文章。

< p style="margin: 5px auto; padding: 0px; text-indent: 0px; color: rgb(0, 0, 0); font-family: 微软雅黑; font-size: 14px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: -webkit-auto; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); ">用的比较多自然就属SimpleAdapter咯,使用简洁,可自定义界面。

< p style="margin: 5px auto; padding: 0px; text-indent: 0px; color: rgb(0, 0, 0); font-family: 微软雅黑; font-size: 14px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: -webkit-auto; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); ">看下这个适配器是如何填充的。

< div style="margin: 5px 0px; padding: 5px; background-color: rgb(245, 245, 245); font-family: 'Courier New'; font-size: 12px; border: 1px solid rgb(204, 204, 204); overflow: auto; color: rgb(0, 0, 0); font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; " class="cnblogs_code">
SimpleAdapter getAdapter(int[] res){
//填充SimpleAdapter

                 List
<Map<String,Object>> list = new
 ArrayList<Map<String,Object>>();              

for(int
 i=0;i<res.length;i++){//
典型for循环


            Map<String,Object> map = new
 HashMap<String,Object> ();             map.put(
"title", "标题"+i);             map.put(

"context", "内容"+i);             map.put(
"img", res[i]);             list.add(map);         }         String[] from 
= new

 String[]{"title","context","img"};         
int

[] to = new int
[]{};         SimpleAdapter adapter = 
new

 SimpleAdapter(this
,list,android.R.layout.simple_list_item_1,from,to);         return
 adapter;     }
< p style="margin: 5px auto; padding: 0px; text-indent: 0px; color: rgb(0, 0, 0); font-family: 微软雅黑; font-size: 14px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: -webkit-auto; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); "> 

< h2 style="margin: 15px auto 2px; padding: 0px; font-size: 1.5em; font-weight: bold; display: block; color: rgb(0, 0, 0); font-family: 微软雅黑; font-style: normal; font-variant: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); ">2.while循环的使用情景 < p style="margin: 5px auto; padding: 0px; text-indent: 0px; color: rgb(0, 0, 0); font-family: 微软雅黑; font-size: 14px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: -webkit-auto; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); ">在读取网络上的内容,不得不用到stream IO流。

< div style="margin: 5px 0px; padding: 5px; background-color: rgb(245, 245, 245); font-family: 'Courier New'; font-size: 12px; border: 1px solid rgb(204, 204, 204); overflow: auto; color: rgb(0, 0, 0); font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; " class="cnblogs_code">
void
 readNet(URL url){//
读取网络


        
        try
 {             HttpURLConnection conn = (HttpURLConnection)url.openConnection();             InputStream is 
= conn.getInputStream();             BufferedReader br 
= new

 BufferedReader(new
 InputStreamReader(is));             String line 

= null
;             StringBuilder sb 

= new
 StringBuilder();             

while((line = br.readLine())!=
null

){//典型while循环

                sb.append(line);             }             Log.e(
"内容", sb.toString());         } 
catch

 (IOException e) {             
//

 TODO Auto-generated catch block

            e.printStackTrace();         }              }
< p style="margin: 5px auto; padding: 0px; text-indent: 0px; color: rgb(0, 0, 0); font-family: 微软雅黑; font-size: 14px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: -webkit-auto; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); "> 

< h2 style="margin: 15px auto 2px; padding: 0px; font-size: 1.5em; font-weight: bold; display: block; color: rgb(0, 0, 0); font-family: 微软雅黑; font-style: normal; font-variant: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); ">3.do while循环的使用情景 < p style="margin: 5px auto; padding: 0px; text-indent: 0px; color: rgb(0, 0, 0); font-family: 微软雅黑; font-size: 14px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: -webkit-auto; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); ">遍历游标通常的用法。

< p style="margin: 5px auto; padding: 0px; text-indent: 0px; color: rgb(0, 0, 0); font-family: 微软雅黑; font-size: 14px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: -webkit-auto; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); "> 

< div style="margin: 5px 0px; padding: 5px; background-color: rgb(245, 245, 245); font-family: 'Courier New'; font-size: 12px; border: 1px solid rgb(204, 204, 204); overflow: auto; color: rgb(0, 0, 0); font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; " class="cnblogs_code">
 void printCursor(Cursor cursor){
//遍历游标

        
        if(cursor!=null
&&cursor.moveToFirst()){                          
do{//


典型do while循环

                for(int
 i=0;i<cursor.getColumnCount();i++){                     Log.e(
"第"+i+"列", cursor.getString(i));                 }                              }
while

(cursor.moveToNext());                      }              }

资源下载