zoukankan      html  css  js  c++  java
  • android 适配器 ArrayAdapter,SimpleAdapter的学习

      今天认真看了下android适配器,学习了下它的使用方法。

      一,ArrayAdapter

      ArrayAdapter 比较简单,只可以存放一行文本信息。下面是简单的实现

        private ListView listView;
        private ArrayAdapter<String> adapter;
        private ArrayList<String>  list;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_array_adapter);
            listView = (ListView)findViewById(R.id.listView3);
            list = new ArrayList<String>();
            adapter = new ArrayAdapter(this,android.R.layout.simple_list_item_1,getData());
            listView.setAdapter(adapter);
        }
        ArrayList<String> getData(){
            list.add("第一行");
            list.add("第二行");
            list.add("第三行");
            list.add("第四行");
            return list;
        }
    

      1,首先需要定义一个数组的集合,这里用ArrayList<String>类型,通过getData()动态的添加数据,

      2,定义一个ArrayAdapter,参数context   :只需要填入上下文this,第二个参数 resource:是一个布局文件,第三个参数List<T> objects:是一个数据集合。上面代码中的

    android.R.layout.simple_list_item_1参数是一个系统自带的laout文件,通过按键ctrl+B查看源代码如下:
    <TextView xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@android:id/text1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceListItemSmall"
        android:gravity="center_vertical"
        android:paddingStart="?android:attr/listPreferredItemPaddingStart"
        android:paddingEnd="?android:attr/listPreferredItemPaddingEnd"
        android:minHeight="?android:attr/listPreferredItemHeightSmall"
    />
    

      只是一个简单的TextView,垂直居中,当然如果不用系统自带的,用我们自己创建的也可以。

      3,将适配器和控件进行绑定。如果没有这一步,相当于我们创建的控件是不会显示出任何数据的。

    二 SimpleAdapter

      SimpleAdapter 比ArrayAdapter稍微复杂一些,一般用在图片文字混排的数据结构中一个简单例子

     private SimpleAdapter adapter;
        private ListView listView;
        private List<Map<String,Object>> list;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_simple_adapter);
            listView = (ListView)findViewById(R.id.listView2);
            list = new ArrayList<Map<String, Object>>();
            adapter = new SimpleAdapter(this,getData(),R.layout.itemlayout,new String[]{"pic","title","content"},new int[]{R.id.imageView,R.id.textView,R.id.textView2});
            listView.setAdapter(adapter);
        }
        List<Map<String,Object>> getData(){
            Map<String,Object> map = new HashMap<String, Object>();
            map.put("pic",R.drawable.a);
            map.put("title","标题");
            map.put("content","内容");
            list.add(map);
            map = new HashMap<String, Object>();
            map.put("pic",R.drawable.b);
            map.put("title","标题");
            map.put("content","内容");
            list.add(map);
            map = new HashMap<String, Object>();
            map.put("pic",R.drawable.c);
            map.put("title","标题");
            map.put("content","内容");
            list.add(map);
            map = new HashMap<String, Object>();
            map.put("pic",R.drawable.d);
            map.put("title","标题");
            map.put("content","内容");
            list.add(map);
            return list;
        }

    通过ctrl+B 查看SimpleAdapter 定义,其中参数

    List<? extends Map<String, ?>> data是一个List的集合,成员是一个Map,
    resource 同样是一个布局文件,只是这里的布局文件需要我们手动添加,
    from     map中映射到控件ID的字符串  
    to       就是我们定义的控件ID    
     public SimpleAdapter(Context context, List<? extends Map<String, ?>> data,
                int resource, String[] from, int[] to) {

    这里的子布局文件使用自定义itemlayout

    <?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="match_parent">
        <ImageView
            android:layout_width="50dp"
            android:layout_height="60dp"
            android:id="@+id/imageView" />
        <LinearLayout
            android:orientation="vertical"
            android:layout_width="match_parent"
            android:layout_height="match_parent">
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="New Text"
                android:id="@+id/textView" />
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="New Text"
                android:id="@+id/textView2" />
        </LinearLayout>
    </LinearLayout>

    最后一点就是千万不能忘记setAdapter

     

  • 相关阅读:
    iframeWin For Easy UI. 为 Easy UI 扩展的支持IFrame插件
    mac系统及xcode使用的SVN客户端安装升级
    泛型List的一点建议
    redis-setbit理解
    zpkin sql语句
    idea 使用 RunDashboard启动
    (转)Spring事务不生效的原因大解读
    (转)Lock和synchronized比较详解
    springboot整合HttpAsyncClient简单实用
    mysql 数据库表迁移复制
  • 原文地址:https://www.cnblogs.com/techdreaming/p/4926419.html
Copyright © 2011-2022 走看看