Androidの循环之常用循环积累
2013-05-07 15:04
218 查看
其实在android的开发中体现了不少java的知识。
详情参考Devin Zhang的Android之Adapter用法总结这边文章。
用的比较多自然就属SimpleAdapter咯,使用简洁,可自定义界面。
看下这个适配器是如何填充的。
1.for循环的使用情景
在填充ListView上都是与适配器绑定的,我们看一下适配器的继承结构图。详情参考Devin Zhang的Android之Adapter用法总结这边文章。
用的比较多自然就属SimpleAdapter咯,使用简洁,可自定义界面。
看下这个适配器是如何填充的。
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; }
2.while循环的使用情景
在读取网络上的内容,不得不用到stream IO流。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(); } }
3.do while循环的使用情景
遍历游标通常的用法。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()); } }
相关文章推荐
- Androidの循环之常用循环积累
- Android正则表达式----常用正则积累
- linux常用命令积累 | ubuntu 10.04系统开发Android
- android 常用的帮助类和方法积累一
- Android积累--常用小知识点
- Android——常用代码段积累(一)
- Android——常用代码段积累(一)
- Android常用到得方法积累
- Android常用工具类
- android下的一些常用命令(1)
- Android常用开源工具(2)-Dagger2进阶
- Android 常用控件(四)
- android常用系统bar高度
- android 布局常用混淆属性
- android:viewpager实现图片循环滑动+索引点击事件
- android常用的侧滑面板
- 常用特效积累
- Android系统常用隐藏命令大全
- android常用同步类
- 头部循环滑动广告 Android-ConvenientBanner 使用