常用数据适配器ArrayAdapter
public class ArraylistActivity extends Activity {
private static String[] names = new String[] { "功能1", "功能2", "功能3", "功能4",
"功能5" };
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ListView lv_info = (ListView) this.findViewById(R.id.lv_info);
lv_info.setAdapter( new ArrayAdapter<String>(this, R.layout.list_item,
R.id.tv_name, names));
}
}
常用数据适配器SimpleAdapter
public class SimpleAdapterActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ArrayList<HashMap<String, Object>> data = new ArrayList<HashMap<String, Object>>();
HashMap<String, Object> m1 = new HashMap<String, Object>();
m1.put("icon", R.drawable.ic_menu_my_calendar);
m1.put("text", "第一个功能");
HashMap<String, Object> m2 = new HashMap<String, Object>();
m2.put("icon", R.drawable.ic_menu_myplaces);
m2.put("text", "第二个功能");
HashMap<String, Object> m3 = new HashMap<String, Object>();
m3.put("icon", R.drawable.ic_menu_paste);
m3.put("text", "第三个功能");
HashMap<String, Object> m4 = new HashMap<String, Object>();
m4.put("icon", R.drawable.ic_menu_preferences);
m4.put("text", "第四个功能");
HashMap<String, Object> m5 = new HashMap<String, Object>();
m5.put("icon", R.drawable.ic_menu_share_holo_light);
m5.put("text", "第五个功能");
data.add(m1);
data.add(m2);
data.add(m3);
data.add(m4);
data.add(m5);
ListView lv_info = (ListView) this.findViewById(R.id.lv_info);
lv_info.setAdapter(new SimpleAdapter(this, data, R.layout.list_item,
new String[] { "icon", "text" }, new int[] { R.id.iv_icon,
R.id.tv_name }));
}
}