惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

T
The Exploit Database - CXSecurity.com
V
Vulnerabilities – Threatpost
Google DeepMind News
Google DeepMind News
Attack and Defense Labs
Attack and Defense Labs
Webroot Blog
Webroot Blog
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
TaoSecurity Blog
TaoSecurity Blog
I
Intezer
Application and Cybersecurity Blog
Application and Cybersecurity Blog
N
News | PayPal Newsroom
S
Security Affairs
T
Tor Project blog
P
Proofpoint News Feed
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
S
Security @ Cisco Blogs
H
Heimdal Security Blog
Hacker News: Ask HN
Hacker News: Ask HN
Help Net Security
Help Net Security
U
Unit 42
云风的 BLOG
云风的 BLOG
The Hacker News
The Hacker News
Cisco Talos Blog
Cisco Talos Blog
量子位
F
Full Disclosure
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
博客园 - 叶小钗
有赞技术团队
有赞技术团队
T
Troy Hunt's Blog
P
Privacy & Cybersecurity Law Blog
Forbes - Security
Forbes - Security
人人都是产品经理
人人都是产品经理
L
Lohrmann on Cybersecurity
Apple Machine Learning Research
Apple Machine Learning Research
Microsoft Security Blog
Microsoft Security Blog
博客园 - Franky
腾讯CDC
AI
AI
Last Week in AI
Last Week in AI
Latest news
Latest news
Google Online Security Blog
Google Online Security Blog
N
Netflix TechBlog - Medium
Engineering at Meta
Engineering at Meta
GbyAI
GbyAI
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
IT之家
IT之家
Martin Fowler
Martin Fowler
Blog — PlanetScale
Blog — PlanetScale
V2EX - 技术
V2EX - 技术
酷 壳 – CoolShell
酷 壳 – CoolShell

博客园 - 独孤雁

在win2008中安装vs2005 【转】NPOI 单元格级别应用 [转]google hosts 2015 [转] C#反射设置属性值和获取属性值 [转]Visual Studio技巧之打造拥有自己标识的代码模板 【转】mysql如何跟踪执行的sql语句 [转]VS2005/2008过期之后简单实用的升级方法 【转】在web 项目使用了ReportViewer时出错 使用NPOI导出excel .NET中TextBox控件设置ReadOnly=true后台取不到值的解决方法 window.location 对象所包含的属性 【原】提交按钮被隐藏,回车一样提交表单 【转】Eclipse安装SVN插件 安卓App程序访问网络 需要配置权限(java.net.SocketException: Permission denied) 【转】Android模拟器怎么配置网络连通 【转】Adobe Dreamweaver CS5序列号 【转】HTML特殊符号对照表 【转】ScriptX打印问题 Android SDK Manager无法更新的解决
[转]listview学习
独孤雁 · 2013-05-24 · via 博客园 - 独孤雁

Posted on 2013-05-24 16:57  独孤雁  阅读(226)  评论()    收藏  举报

转子:http://www.apkbus.com/forum.php?mod=viewthread&tid=94324

1、 ListView单行显示(simple_list_item_1)代码:

  1. public class myListItem1 extends Activity{
  2. @Override
  3. public void onCreate(Bundle savedInstanceState){
  4. super.onCreate(savedInstanceState);

  5. ListView listView = new ListView(this);

  6. List<Map<String, String>> mList = new ArrayList<Map<String, String>>();
  7. for(int i=0; i<10; i++){
  8. Map<String, String> map = new HashMap<String, String>();
  9. map.put("TITLE", "Test Title");
  10. map.put("CONTENT", "Test Content");
  11. mList.add(map);
  12. }

  13. SimpleAdapter adapter = new SimpleAdapter(this,
  14. mList,
  15. android.R.layout.simple_list_item_1,         // List 显示一行item1
  16. new String[]{ "CONTENT" },         // "TITLE",
  17. new int[]{ android.R.id.text1 }
  18. );

  19. listView.setAdapter(adapter);
  20. setContentView(listView);
  21. }
  22. }

复制代码

效果:


2、 ListView双行显示(simple_list_item_2)代码:

  1. public class myListItem2 extends Activity{
  2. @Override
  3. public void onCreate(Bundle savedInstanceState){
  4. super.onCreate(savedInstanceState);

  5. ListView listView = new ListView(this);

  6. List<Map<String, String>> mList = new ArrayList<Map<String, String>>();
  7. for(int i=0; i<10; i++){
  8. Map<String, String> map = new HashMap<String, String>();
  9. map.put("TITLE", "Test Title");
  10. map.put("CONTENT", "Test Content");
  11. mList.add(map);
  12. }

  13. SimpleAdapter adapter = new SimpleAdapter(this,
  14. mList,
  15. android.R.layout.simple_list_item_2,         // List 显示两行item1、item2
  16. new String[]{ "TITLE", "CONTENT" },
  17. new int[]{ android.R.id.text1, android.R.id.text2 }
  18. );

  19. listView.setAdapter(adapter);
  20. setContentView(listView);
  21. }
  22. }

复制代码

效果:


3、 ListView自定义显示代码:

  1. public class MyList extends Activity {
  2. @Override
  3. public void onCreate(Bundle savedInstanceState) {
  4. super.onCreate(savedInstanceState);
  5. setContentView(R.layout.mylist);

  6. List<Map<String, Object>> mList = new ArrayList<Map<String, Object>>();
  7. for (int i = 0; i < 10; i++) {
  8. Map<String, Object> map = new HashMap<String, Object>();
  9. map.put("PIC", R.drawable.pic);         // 加载图片资源
  10. map.put("TITLE", "Test Title");
  11. map.put("CONTENT", "Test Content");
  12. mList.add(map);
  13. }

  14. SimpleAdapter adapter = new SimpleAdapter(this,
  15. mList,
  16. R.layout.listitem,         // 自定义布局格式
  17. new String[] { "PIC", "TITLE", "CONTENT" },
  18. new int[] { R.id.listitem_pic, R.id.listitem_title, R.id.listitem_content }
  19. );

  20. ListView listView = (ListView) findViewById(R.id.list);
  21. listView.setAdapter(adapter);
  22. }
  23. }

复制代码

自定义的 listitem.xml

  1. <?xml version="1.0" encoding="utf-8"?>

  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="fill_parent"
  4. android:layout_height="?android:attr/listPreferredItemHeight">

  5. <ImageView android:id="@+id/listitem_pic"
  6. android:layout_width="wrap_content"
  7. android:layout_height="fill_parent"
  8. android:layout_alignParentTop="true"
  9. android:layout_alignParentBottom="true"
  10. android:adjustViewBounds="true"
  11. android:padding="10dip" />

  12. <TextView android:id="@+id/listitem_title"
  13. android:layout_width="wrap_content"
  14. android:layout_height="wrap_content"
  15. android:layout_toRightOf="@id/listitem_pic"
  16. android:layout_alignParentRight="true"
  17. android:layout_alignParentTop="true"
  18. android:layout_alignWithParentIfMissing="true"
  19. android:gravity="center_vertical"
  20. android:textSize="22sp" />

  21. <TextView android:id="@+id/listitem_content"
  22. android:layout_width="fill_parent"
  23. android:layout_height="wrap_content"
  24. android:layout_toRightOf="@id/listitem_pic"
  25. android:layout_alignParentBottom="true"
  26. android:layout_alignParentRight="true"
  27. android:layout_below="@id/listitem_title"
  28. android:singleLine="true"
  29. android:ellipsize="marquee"
  30. android:textSize="14sp" />
  31. </RelativeLayout>

复制代码

效果:


4、 GridView自定义显示代码:

  1. public class MyGrid extends Activity {
  2. @Override
  3. public void onCreate(Bundle savedInstanceState) {
  4. super.onCreate(savedInstanceState);
  5. setContentView(R.layout.mygrid);

  6. List<Map<String, Object>> mList = new ArrayList<Map<String, Object>>();
  7. for (int i = 0; i < 10; i++) {
  8. Map<String, Object> map = new HashMap<String, Object>();
  9. map.put("PIC", R.drawable.pic);         // 加载图片资源
  10. map.put("TITLE", "Test Title");
  11. mList.add(map);
  12. }
  13. SimpleAdapter adapter = new SimpleAdapter(this,
  14. mList, R.layout.griditem,         // 自定义布局格式
  15. new String[] { "PIC", "TITLE" },
  16. new int[] { R.id.griditem_pic, R.id.griditem_title, }
  17. );

  18. GridView gridView = (GridView) findViewById(R.id.grid);
  19. gridView.setAdapter(adapter);
  20. }
  21. }

复制代码

自定义的 gridview.xml

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_height="fill_parent"
  4. android:layout_width="fill_parent"
  5. android:orientation="vertical">

  6. <ImageView android:id="@+id/griditem_pic"
  7. android:layout_width="wrap_content"
  8. android:layout_height="wrap_content"
  9. android:layout_gravity="center_horizontal">
  10. </ImageView>

  11. <TextView android:id="@+id/griditem_title"
  12. android:layout_width="wrap_content"
  13. android:layout_height="wrap_content"
  14. android:layout_gravity="center_horizontal">
  15. </TextView>
  16. </LinearLayout>

复制代码

效果: