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

推荐订阅源

Security Archives - TechRepublic
Security Archives - TechRepublic
Project Zero
Project Zero
K
Kaspersky official blog
G
Google Developers Blog
T
Threat Research - Cisco Blogs
T
The Blog of Author Tim Ferriss
Cyberwarzone
Cyberwarzone
Y
Y Combinator Blog
Recorded Future
Recorded Future
Blog — PlanetScale
Blog — PlanetScale
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
Cisco Talos Blog
Cisco Talos Blog
Latest news
Latest news
Microsoft Security Blog
Microsoft Security Blog
H
Help Net Security
S
Schneier on Security
P
Palo Alto Networks Blog
H
Hacker News: Front Page
N
News and Events Feed by Topic
N
Netflix TechBlog - Medium
博客园 - Franky
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
SecWiki News
SecWiki News
Cloudbric
Cloudbric
TaoSecurity Blog
TaoSecurity Blog
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
The Hacker News
The Hacker News
C
Check Point Blog
L
LangChain Blog
腾讯CDC
小众软件
小众软件
T
Tenable Blog
Google DeepMind News
Google DeepMind News
GbyAI
GbyAI
L
LINUX DO - 最新话题
A
About on SuperTechFans
Google Online Security Blog
Google Online Security Blog
C
Cisco Blogs
Recent Announcements
Recent Announcements
Hacker News: Ask HN
Hacker News: Ask HN
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
Vercel News
Vercel News
雷峰网
雷峰网
美团技术团队
D
DataBreaches.Net
Martin Fowler
Martin Fowler
Help Net Security
Help Net Security
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
F
Full Disclosure
博客园_首页

博客园 - 独孤雁

在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>

复制代码

效果: