为了账号安全,请及时绑定邮箱和手机立即绑定

ListView初试

标签:
Android

历时若干天的框架搭建完成。开始转入内部学习:

ListView之前一直以为他的作用就和布局类似,不过其中只能存放一些类似于char或者int 的纯文本。今天又长姿势了。

ListView是一个列表框架,一般使用的时候需要和适配器相搭配。而常见的适配器有三种:ArrayAdapter,SimpleAdapter和SimpleCursorAdapter。

当然作为小白的我来说这些东西就只能是跟着大神的脚步慢慢爬,看了一下午的listView,大致的用法看明白了。

ArrayAdapter是Androidstudio最基本的,大神告诉我这是安卓自带的(莫非其他两种不是??)。他只能显示纯文本,显示一页的列表。

ArrayAdapter的构造需要三个参数,依次为this,布局文件(注意这里的布局文件描述的是列表的每一行的布局,android.R.layout.simple_list_item_1是系统定义好的布局文件只显示一行文字,数据源(一个List集合)。同时用setAdapter()完成适配的最后工作。

(借用一下百度得到的解释)

SimpleAdapter是档次比较高端的,可以映射imagmentView,TextView,甚至Button;撒一段代码:

public class MyListView3 extends ListActivity {      // private List<String> data = new ArrayList<String>();    @Override    public void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);         SimpleAdapter adapter = new SimpleAdapter(this,getData(),R.layout.vlist,                new String[]{"title","info","img"},                new int[]{R.id.title,R.id.info,R.id.img});        setListAdapter(adapter);    }     private List<Map<String, Object>> getData() {        List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();         Map<String, Object> map = new HashMap<String, Object>();        map.put("title", "G1");        map.put("info", "google 1");        map.put("img", R.drawable.i1);        list.add(map);         map = new HashMap<String, Object>();        map.put("title", "G2");        map.put("info", "google 2");        map.put("img", R.drawable.i2);        list.add(map);         map = new HashMap<String, Object>();        map.put("title", "G3");        map.put("info", "google 3");        map.put("img", R.drawable.i3);        list.add(map);                 return list;    }}<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:orientation="horizontal" android:layout_width="fill_parent"    android:layout_height="fill_parent">      <ImageView android:id="@+id/img"         android:layout_width="wrap_content"        android:layout_height="wrap_content"         android:layout_margin="5px"/>     <LinearLayout android:orientation="vertical"        android:layout_width="wrap_content"         android:layout_height="wrap_content">         <TextView android:id="@+id/title"             android:layout_width="wrap_content"            android:layout_height="wrap_content"             android:textColor="#FFFFFFFF"            android:textSize="22px" />        <TextView android:id="@+id/info"             android:layout_width="wrap_content"            android:layout_height="wrap_content"             android:textColor="#FFFFFFFF"            android:textSize="13px" />     </LinearLayout>  </LinearLayout>我不是代码的创造者,我只是代码的搬运工。。

原文链接:http://www.apkbus.com/blog-879004-62866.html

点击查看更多内容
TA 点赞

若觉得本文不错,就分享一下吧!

评论

作者其他优质文章

正在加载中
  • 推荐
  • 评论
  • 收藏
  • 共同学习,写下你的评论
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦
今天注册有机会得

100积分直接送

付费专栏免费学

大额优惠券免费领

立即参与 放弃机会
意见反馈 帮助中心 APP下载
官方微信

举报

0/150
提交
取消