SimpleAdapter ArrayAdapter用法

        listView = (ListView) findViewById(R.id.list_main);
/*        String[] strings = {"A","A","A","A","A","A","A","A","A"};
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.list_ctn, strings);*/
        List<Map<String, Object>> arrayList = new ArrayList<Map<String, Object>>();
        Map<String, Object> map = null;
        int i = 0;
        while (i<10) {
            map = new HashMap<String, Object>();
            map.put("name", "zh"+i);
            map.put("age", i);
            arrayList.add(map);
            i++;
        }
        String[] from = {"name", "age"};
        int[] to = {R.id.list_ctn_name, R.id.list_ctn_age};
        SimpleAdapter adapter = new SimpleAdapter(this, arrayList, R.layout.list_ctn, from, to);
        listView.setAdapter(adapter);
 
  
<ListView
android:id="@+id/list_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>



<?
xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <TextView android:id="@+id/list_ctn_name" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:text="TextView" /> <TextView android:id="@+id/list_ctn_age" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:text="TextView" /> </LinearLayout>

 

转载于:https://www.cnblogs.com/znsongshu/p/9330072.html

在Android Studio中,ArrayAdapterSimpleAdapter都是常用的适配器类,用于将数据源与UI控件进行绑定。下面我来分别介绍一下这两个适配器类的使用方法。 ### ArrayAdapter ArrayAdapter是一个简单的适配器,它可以将一个数组或List中的数据与ListView等UI控件进行绑定。使用ArrayAdapter时,需要创建一个包含数据的数组或List,并将其传入ArrayAdapter的构造函数中,然后将ArrayAdapter设置到对应的UI控件中即可。 下面是一个简单的使用ArrayAdapter的示例代码: ```java String[] data = {"apple", "banana", "orange", "pear"}; ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, data); ListView listView = (ListView) findViewById(R.id.list_view); listView.setAdapter(adapter); ``` 在上面的代码中,我们创建了一个包含4个字符串的数组data,然后将其传入ArrayAdapter的构造函数中,同时指定了一个简单的ListView的布局文件`android.R.layout.simple_list_item_1`,最后将ArrayAdapter设置到ListView中。 ### SimpleAdapter SimpleAdapter是一个稍微复杂一些的适配器,它可以将一个List中的Map与ListView等UI控件进行绑定。使用SimpleAdapter时,需要创建一个包含多个Map的List,并将其传入SimpleAdapter的构造函数中,然后将SimpleAdapter设置到对应的UI控件中即可。 下面是一个简单的使用SimpleAdapter的示例代码: ```java List<Map<String, Object>> data = new ArrayList<Map<String, Object>>(); Map<String, Object> map1 = new HashMap<String, Object>(); map1.put("name", "apple"); map1.put("image", R.drawable.apple); data.add(map1); Map<String, Object> map2 = new HashMap<String, Object>(); map2.put("name", "banana"); map2.put("image", R.drawable.banana); data.add(map2); Map<String, Object> map3 = new HashMap<String, Object>(); map3.put("name", "orange"); map3.put("image", R.drawable.orange); data.add(map3); Map<String, Object> map4 = new HashMap<String, Object>(); map4.put("name", "pear"); map4.put("image", R.drawable.pear); data.add(map4); SimpleAdapter adapter = new SimpleAdapter(this, data, R.layout.item_simple_adapter, new String[]{"name", "image"}, new int[]{R.id.tv_name, R.id.iv_image}); ListView listView = (ListView) findViewById(R.id.list_view); listView.setAdapter(adapter); ``` 在上面的代码中,我们创建了一个包含4个Map的List,每个Map包含两个键值对(name和image),分别对应水果的名称和图片资源的ID。然后将List传入SimpleAdapter的构造函数中,同时指定了一个ListView的布局文件`R.layout.item_simple_adapter`,以及将name和image映射到ListView的两个控件(tv_name和iv_image)上。 总的来说,ArrayAdapterSimpleAdapter都是非常实用的适配器类,用于将数据源与UI控件进行绑定。在实际开发中,我们可以根据具体的需求来选择使用哪个适配器类。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值