首先我们将listview简单实现,有图形,有文字:效果如图


之前我们完成了一个较为简单的listview视图列表,但是生活中我们往往碰到的

是更为复杂列表,有图像有评分标准,不如我们来试一试手,做一个琳琅满目的美团美食列表,在看的口水涟涟份上我们来实现它。

首先我们定义模板,也是第一次在安卓中接触模板概念,在layout里定义模板文件,data_list.xml

  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2. android:id="@+id/LinearLayout1"
  3. android:layout_width="match_parent"
  4. android:layout_height="match_parent"
  5. android:orientation="horizontal" >
  6. <ImageView
  7. android:id="@+id/pic"
  8. android:padding="3px"
  9. android:layout_width="wrap_content"
  10. android:layout_height="wrap_content"
  11. />
  12. <LinearLayout
  13. android:layout_width="200px"
  14. android:layout_height="wrap_content"
  15. android:gravity="left"
  16. android:orientation="vertical" >
  17. <TextView
  18. android:id="@+id/title"
  19. android:padding="3px"
  20. android:textSize="26px"
  21. android:layout_width="wrap_content"
  22. android:layout_height="wrap_content"
  23. android:text="TextView" />
  24. <TextView
  25. android:id="@+id/author"
  26. android:padding="3px"
  27. android:textSize="15px"
  28. android:layout_width="wrap_content"
  29. android:layout_height="wrap_content"
  30. android:text="TextView" />
  31. </LinearLayout>
  32. <LinearLayout
  33. android:layout_width="match_parent"
  34. android:layout_height="wrap_content"
  35. android:orientation="vertical" >
  36. <ImageView
  37. android:id="@+id/score"
  38. android:padding="5px"
  39. android:layout_width="wrap_content"
  40. android:layout_height="wrap_content"
  41. />
  42. <TextView
  43. android:id="@+id/type"
  44. android:padding="5px"
  45. android:layout_width="wrap_content"
  46. android:layout_height="wrap_content"
  47. android:text="TextView" />
  48. </LinearLayout>
  49. </LinearLayout>

也就是这样:

然后在main.xml布局文件定义:

  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2. xmlns:tools="http://schemas.android.com/tools"
  3. android:id="@+id/LinearLayout1"
  4. android:layout_width="fill_parent"
  5. android:layout_height="fill_parent"
  6. android:orientation="vertical"
  7. android:paddingBottom="@dimen/activity_vertical_margin"
  8. android:paddingLeft="@dimen/activity_horizontal_margin"
  9. android:paddingRight="@dimen/activity_horizontal_margin"
  10. android:paddingTop="@dimen/activity_vertical_margin"
  11. tools:context=".MainActivity" >
  12. <TextView
  13. android:id="@+id/textView1"
  14. android:textSize="50px"
  15. android:gravity="center_horizontal"
  16. android:layout_width="wrap_content"
  17. android:layout_height="wrap_content"
  18. android:text="美食列表" />
  19. <ListView   //这里之后我们会把模板嵌入进去
  20. android:id="@+id/datalist"
  21. android:layout_width="fill_parent"
  22. android:layout_height="wrap_content" >
  23. </ListView>
  24. </LinearLayout>

以上的listview只是一个名字定义,我们建立好模板就可以代入进去,如何代入还是用Simpleadapter适配器,这部分实现要在MainActivity.java里实现,首先我们准备好几张美食图片,以及打分。

然后在代码里实现:

  1. public class MainActivity extends Activity {
  2. private int[] pic=new int[]{R.drawable.a1,R.drawable.a2,R.drawable.a3,R.drawable.a4
  3. ,R.drawable.a5,R.drawable.a6};
  4. private String data[][]=new String[][]{{"来御来三汁焖锅","烧烤烤肉"},{"SPK思必客麻辣香锅","烧烤烤肉"},
  5. {"冰果彩虹","蛋糕甜点"},{"德克士","小吃快餐"},{"老北京炸酱面","小吃快餐"},{"铁板传奇","其他美食"}};
  6. private List<Map<String,String>> list=new ArrayList<Map<String,String>>();
  7. private ListView datalist;
  8. private SimpleAdapter simpleadapter=null;
  9. protected void onCreate(Bundle savedInstanceState) {
  10. super.onCreate(savedInstanceState);
  11. setContentView(R.layout.activity_main);
  12. this.datalist=(ListView)super.findViewById(R.id.datalist);
  13. for(int i=0;i<data.length;i++){
  14. Map map=new HashMap<String,String>();
  15. map.put("pic", String.valueOf(this.pic[i]));
  16. map.put("title", this.data[i][0]);
  17. map.put("author",this.data[i][1]);
  18. map.put("score",String.valueOf(R.drawable.star) );
  19. map.put("type", "岳麓区天马");
  20. this.list.add(map);
  21. }
  22. this.simpleadapter=new SimpleAdapter(this,this.list,R.layout.data_list,
  23. new String[]{"pic","title","author","score","type"}
  24. ,new int[]{R.id.pic,R.id.title,R.id.author,R.id.score,R.id.type});
  25. this.datalist.setAdapter(this.simpleadapter);
  26. }
  27. @Override
  28. public boolean onCreateOptionsMenu(Menu menu) {
  29. // Inflate the menu; this adds items to the action bar if it is present.
  30. getMenuInflater().inflate(R.menu.main, menu);
  31. return true;
  32. }
  33. }

如同上节课一样,一一匹配,首先将图片之类的统统存入数组,数组统统存入map健和value值,然后将map存入list队列里面,然后加入到适配器里面进行分封装,然后ListView就坐享其成得到这个适配器。

然后我们的美团列表就冰果诞生了:


 

Android之listview运用(美团美食列表)的更多相关文章

  1. Android开发 ListView(垂直滚动列表项视图)的简单使用

    效果图: 使用方法: 1.在布局文件中加入ListView控件: <?xml version="1.0" encoding="utf-8"?> &l ...

  2. Android使用listView,BaseAdapter实现列表页

    参考: 1.讲解很详细: blog.csdn.net/psuaije/article/details/7447391 总结: 代码:

  3. 我的Android进阶之旅------>Android二级ListView列表的实现

    实现如下图所示的二级列表效果 首先是在布局文件中,布局两个ListView,代码如下: <LinearLayout xmlns:android="http://schemas.andr ...

  4. Android一个ListView列表之中插入两种不同的数据

    http://www.cnblogs.com/roucheng/ Android一个ListView列表之中插入两种不同的数据 代码如下: public class ViewHolder{ Butto ...

  5. Android通过LIstView显示文件列表

    [绥江一百]http://www.sj100.net                                                  欢迎,进入绥江一百感谢点击[我的小网站,请大家多 ...

  6. 如何在Android的ListView中构建CheckBox和RadioButton列表(支持单选和多选的投票项目示例)

    引言 我们在android的APP开发中有时候会碰到提供一个选项列表供用户选择的需求,如在投票类型的项目中,我们提供一些主题给用户选择,每个主题有若干选项,用户对这些主题的选项进行选择,然后提交. 本 ...

  7. Android 自定义 ListView 上下拉动“刷新最新”和“加载更多”歌曲列表

    本文内容 环境 测试数据 项目结构 演示 参考资料 本文演示,上拉刷新最新的歌曲列表,和下拉加载更多的歌曲列表.所谓"刷新最新"和"加载更多"是指日期.演示代码 ...

  8. Android 自定义 ListView 显示网络上 JSON 格式歌曲列表

    本文内容 环境 项目结构 演示自定义 ListView 显示网络上 JSON 歌曲列表 参考资料 本文最开始看的是一个国人翻译的文章,没有源代码可下载,根据文中提供的代码片段,自己新建的项目(比较可恶 ...

  9. Android 使用ListView显示信息列表

    课程目标1.理解ListView的基础使用2.学会熟练运用两种适配器(ArrayAdapter.SimpleAdapter)3.学会熟练运用两种监听器(OnScrollListener.OnItemC ...

随机推荐

  1. Python连接MySQL的实例代码

    Python连接MySQL的实例代码   MySQLdb下载地址:http://sourceforge.net/projects/mysql-python/ 下载解压缩后放到%Python_HOME% ...

  2. jstl函数的使用

    1.fn:contains()和fn:containsIgnoreCase() fn:contains()函数用于确定一个字符串是否包含指定的子串. fn:containsIgnoreCase()函数 ...

  3. Unity3d 屏幕截图。并保存。iOS

    - ( void ) imageSaved: ( UIImage *) image didFinishSavingWithError:( NSError *)error contextInfo: ( ...

  4. android布局 - fill_parent/match_paren/wrap_content的区别

    三个属性都用来适应视图的水平或垂直大小,一个以视图的内容或尺寸为基础的布局比精确地指定视图范围更加方便. 1)fill_parent 设置一个构件的布局为fill_parent将强制性地使构件扩展,以 ...

  5. Linux中使用sed命令或awk命令修改常规配置文件

    一.方案: Linux中使用sed命令或awk命令修改常规配置文件 二.步骤: 1.假设有一个a.txt,内容如下: #!/bin/bash aa= bbb= ccc= #ddd= 2.如果想要把里面 ...

  6. Tomcat访问日志浅析 (转)

    来自:http://blog.chinaunix.net/uid-20691565-id-3938220.html Tomcat的访问日志是靠org.apache.catalina.valves.Ac ...

  7. Codeforces Round #310 (Div. 1) B. Case of Fugitive(set二分)

    B. Case of Fugitive time limit per test 3 seconds memory limit per test 256 megabytes input standard ...

  8. ios app性能分析

    苹果app的流畅性一般比安卓的要好的多.应该是和苹果系统的设计理念同样,早期的iphone4曾经是绝对单任务,仅仅能做一件事情,尽管添加了后台能够.音乐播放,定位等有限的服务.可是大多数普通应用切换到 ...

  9. jvm内存模型及分配

    1.什么是jvm?(1)jvm是一种用于计算设备的规范,它是一个虚构出来的机器,是通过在实际的计算机上仿真模拟各种功能实现的.(2)jvm包含一套字节码指令集,一组寄存器,一个栈,一个垃圾回收堆和一个 ...

  10. Java定时器Timer的使用详解

     转载请注明原文地址:http://www.cnblogs.com/ygj0930/p/6374714.html 定时器在Web开发中使用得不是很多.这里主要列举一下使用定时器的步骤,方便日后使用时查 ...