ListView SimpleAdapter

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);

    }

}

 

posted @ 2012-12-10 13:04  Mr.haox  阅读(330)  评论(0编辑  收藏  举报