ListView SimpleAdapter

£可爱£侵袭症+ 提交于 2020-02-10 04:49:19
public class MainActivity extends Activity {
    private ListView lv;
    private String[] names;
    private String[] ages;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        names = getResources().getStringArray(R.array.name);
        ages = getResources().getStringArray(R.array.age);

        lv = (ListView) findViewById(R.id.lv);
        ArrayList<Map<String, Object>> listItems = new ArrayList<Map<String, Object>>();
        for (int i = 0; i < names.length; i++) {
            Map<String, Object> map = new HashMap<String, Object>();
            map.put("name", names[i]);
            map.put("age", ages[i]);
            listItems.add(map);
        }
        SimpleAdapter adapter = new SimpleAdapter(this, listItems,
                R.layout.list_item, new String[] { "name", "age" }, new int[] {
                        R.id.tv1, R.id.tv2 });
        lv.setAdapter(adapter);

    }

}

 

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!