zoukankan      html  css  js  c++  java
  • ArrayAdapter、SimpleAdapter简单用法

    1. 使用流程

    2. ArrayAdapter

    new ArrayAdapter<?>(context, textViewResourceId, objects)
    

      context:上下文

      textViewResourceId:列表项的布局文件id

      objects:数据源(数组或集合)

    public class MainActivity extends Activity {
    
    private ListView myListView;
    private ArrayAdapter<String>arrayAdapter;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    
        //新建数据源
        String[] data = {"初探ListView","初探ListView","初探ListView","初探ListView"};
        //新建适配器并绑定数据源
        adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, data);
        //设置适配器
        myListView = (ListView) findViewById(R.id.myListView);
        myListView.setAdapter(arrayAdapter);
    }
    

    }

    3. SimpleAdapter

    new SimpleAdapter(context, data, resource, from, to)
    

      context:上下文

      data:List> data,数据源必须是一个泛型为Map<String, ?>的集合。其中,集合中的每一个Map对应ListView中的一项。

      resource:列表项的布局文件id

      from:Map中的键名

      to:列表项的布局文件中的组件id

    public class MainActivity extends Activity {
    
        private ListView myListView;
        private SimpleAdapter simpleAdapter;
        private List<Map<String, Object>> data;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            //新建数据源
            data = new ArrayList<Map<String, Object>>();
            for (int i = 0; i < 20; i++) {
                Map<String, Object>map = new HashMap<String, Object>();
                map.put("img", R.mipmap.ic_launcher);
                map.put("text", "初识simpleAdapter");
                data.add(map);
            }
            //新建适配器并绑定数据源
            simpleAdapter = new SimpleAdapter(this, getData(), R.layout.item, new String[]{"img", "text"}, new int[]{R.id.img, R.id.text});
            //设置适配器
            myListView = (ListView) findViewById(R.id.myListView);
            myListView.setAdapter(simpleAdapter);
            
        }
    }
    
  • 相关阅读:
    [bzoj 4553][Tjoi2016&Heoi2016]序列
    [bzoj 5143][Ynoi 2018]五彩斑斓的世界
    [bzoj 4939][Ynoi 2016]掉进兔子洞
    luogu_P3674 小清新人渣的本愿
    [bzoj 2809][Apio2012]dispatching
    [bzoj 3110][zjoi 2013]K大数查询
    Entity Framework技巧系列之九
    Entity Framework技巧系列之八
    Entity Framework技巧系列之七
    Entity Framework技巧系列之六
  • 原文地址:https://www.cnblogs.com/joahyau/p/6538883.html
Copyright © 2011-2022 走看看