zoukankan      html  css  js  c++  java
  • listview的几种简单用法

    第一种(最简单) 使用数组创建listview

    只能实现简单的数组列表
    需要一个listview控件,只有设置id以后才会显示完整,最重要的是设置这个属性
    android:entries="@array/array"
    由这个属性可知需要一个资源文件array.xml
    <resources>
        <array name="array">
            <item>计算机科学与技术</item>
            <item>计算机科学与技术</item>
            <item>计算机科学与技术</item>
            <item>计算机科学与技术</item>
            <item>计算机科学与技术</item>
            <item>计算机科学与技术</item>
        </array>
    </resources>

    最后在启动页面中声明listview变量,并且赋值id

    第二种 使用arrayadapter创建listview

    需要一个设置好id的listview控件 不需要上面的那个属性
    需要定义一个资源数组作为要显示的东西
    String[] array1 = {"张三","李四","网二"};
    最重要的就是定义一个适配器
    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.select_dialog_item,array1);
    listView.setAdapter(adapter);
    这样就ok了
    第三种 使用simpleAdapter创建listview 最常用的 图文结合


    需要一个设置好id的listview控件
    需要构建一个布局文件 如何显示就在此布局中设置
    <ImageView
            android:id="@+id/iv"
            android:layout_width="50dp"
            android:layout_height="50dp"
            android:src="@mipmap/ic_launcher"/>
    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical">
    
        <TextView
            android:textSize="12dp"
            android:id="@+id/tv"
            android:layout_marginLeft="12dp"
            android:layout_marginTop="15dp"
            android:text="nsuifhiu modgfdo"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
        <TextView
            android:id="@+id/tv_er"
            android:textSize="8dp"
            android:layout_marginLeft="12dp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="哈哈"/>

    重要步骤 使用HashMap

    HashMap<String,Object>t2= new HashMap<>();
    //String代表可以传入文本文件 Object代表可传入图片
    t2.put("text","张三" );
    //代表一个媒介 后面的代码需要使用这个 就代表要传入text这一类的值
    t2.put("tv","一个人");
    t2.put("icon",R.drawable.fabu);
    重要步骤 使用ArrayList

    ArrayList<Map<String,Object>> list = new ArrayList<>();
    list.add(t1);
    list.add(t2);
    list.add(t3);
    list.add(t4);
    list.add(t5);


    最重要的一步 设置simpleadapter
    SimpleAdapter sa = new SimpleAdapter(this,list,R.layout.layout,new String[]{"text","icon","tv"},new int[]{R.id.tv,R.id.iv,R.id.tv_er});
    listView.setAdapter(sa);



    成功无捷径,学习有方法!
  • 相关阅读:
    为什么不应该使用ZooKeeper做服务发现
    Python 练习 人事管理
    Python 爬取 妹子图(技术是无罪的)
    Python 爬取 书籍
    Python 爬取 豆瓣
    Python 爬取 房天下
    Python 爬取 煎蛋
    Python 爬取 拉钩
    Python 爬取 猫眼
    家具专区网
  • 原文地址:https://www.cnblogs.com/ithuangqing/p/12113722.html
Copyright © 2011-2022 走看看