前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Android UI学习之ListView(使用ArrayAdapter和SimpleAdapter)

Android UI学习之ListView(使用ArrayAdapter和SimpleAdapter)

作者头像
DragonKingZhu
发布2022-05-08 15:56:15
4040
发布2022-05-08 15:56:15
举报
文章被收录于专栏:Linux内核深入分析

既然要使用ArrayAdapter,那我们先看看系统中所以的Adapter关系:

在实际中使用频率比较高的有:SimpleAdapter, ArrayAdapter, BaseAdapter

BaseAdapter: 是一个抽象类,实现它要实现比较多的方法,但是灵活的高

ArrayAdapter:支持了泛型操作,比较简单,一般只能显示同类型的数据

SimpleAdapter:有比较好的灵活的,可以定义自己的UI

关于BaseAdapter在上一节已经讲过,这里不在举例说明

我们先使用ArrayAdapter:

代码语言:javascript
复制
public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        ListView listView = (ListView) findViewById(R.id.lv);
        
       listView.setAdapter(new ArrayAdapter<String>(
        		MainActivity.this, 
        		R.layout.item_list, //每个元素的布局文件
        		R.id.tv_name,  //要给布局文件中那个控件设置属性
        		new String[]{"视频", "电影", "无线"}));//给id=tv_name设置属性
       
    }

效果图如下:

可以看到,ArrayAdapter只能设置id相同的属性,别的id的属性是设置不了的。

所以,ArrayAdapter一般用于设置只需要设置一种类型的数据,既方便又简单。

SimpleAdapter学习:

先贴出我们上面的布局文件:

代码语言:javascript
复制
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="horizontal" >
    
    <ImageView 
        android:id="@+id/image_head"
        android:layout_width="50sp"
        android:layout_height="50sp"
        android:src="@drawable/afh"
        />
    
    <LinearLayout 
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"        
        >
    <TextView 
        android:id="@+id/tv_name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="23sp"
        />
     <TextView 
        android:id="@+id/tv_phone"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="15sp"
        android:text="10086"
        />
    </LinearLayout>

</LinearLayout>

使用SimpleAdapter显示:

代码语言:javascript
复制
 //定义一个list
    List<Map<String, Object>> data = new ArrayList<Map<String,Object>>();
    
    //第一个行数据设置
    Map<String, Object> map1 = new HashMap<String, Object>();
    map1.put("image", R.drawable.afh);
    map1.put("name", "定位");
    map1.put("phone", "12345678900000000000");
    data.add(map1);
    
    Map<String, Object> map2 = new HashMap<String, Object>();
    map2.put("image", R.drawable.afj);
    map2.put("name", "音乐");
    map2.put("phone", "99999999999999999999");
    data.add(map2);

    Map<String, Object> map3 = new HashMap<String, Object>();
    map3.put("image", R.drawable.afl);
    map3.put("name", "图片");
    map3.put("phone", "66666666666666666666");
    data.add(map3);

    Map<String, Object> map4 = new HashMap<String, Object>();
    map4.put("image", R.drawable.afn);
    map4.put("name", "链接");
    map4.put("phone", "88888888888888888888");
    data.add(map4);

    listView.setAdapter(new SimpleAdapter(this, data, R.layout.item_list, //每行的布局文件样式
    		new String[]{"image", "name" ,"phone"}, //这里image和资源id的是一一对应的,顺序是对应的
    		new int[]{R.id.image_head, R.id.tv_name, R.id.tv_phone}));

显示效果如下:

可以看到SimpleAdapter作用还是很明显的,同时可以设置image, 和2个textview

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2015-08-06,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档