几种适配器   ArrayAdapter、SimpleAdapter...

14.03.05


ArrayAdapter

      局限性:行布局只能是TextView

ArrayAdapter adapter=new ArrayAdapter(this,R.layout.item_list,strlist);
listview.setAdapter(adapter);


ListActivity

       默认布局listview   继承ListActivity

       显示布局setListadapternew ArrayAdapter());

public class ListViewActivity extends ListActivity {
    String[] strlist=new String[]{"aaa","bbb","ccc","ddd","eee","fff","ggg","hhh"};
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
//      setContentView(R.layout.activity_list_view);
                
        setListAdapter(new ArrayAdapter<String>(this, R.layout.text_list,strlist));
}


基本适配器SimpleAdapter

      参数from:存放HashMapkey

listview = (ListView) findViewById(R.id.listView1);
        ArrayList<HashMap<String,String>> data=new ArrayList<HashMap<String,String>>();
        for (int i = 0; i < 10; i++) {
            HashMap<String, String> hasData=new HashMap<String, String>();
            hasData.put("name", "Amo");
            hasData.put("age", "20");
            data.add(hasData);
        }
        String[] from=new String[]{"name","age"};
        int[] to=new int[]{R.id.textView1,R.id.textView2};
        SimpleAdapter adapter=new SimpleAdapter(this, data, R.layout.item_list, from, to);
        listview.setAdapter(adapter);


你可能感兴趣的:(适配器,局限性)