Android UI学习 - ListView
2010-07-02 14:54:00 来源:WEB开发网接下来看看几个绑定数据的例子:
1. 使用ArrayAdapter
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//参考ArrayAdapter的构造函数
setListAdapter(new ArrayAdapter
android.R.layout.simple_list_item_1,
mStrings));
//在ListView上输入字母,就会自动筛选出以此内容开头的Item
getListView().setTextFilterEnabled(true);
}
private String[] mStrings = {"A", "Android", "机器人", "Google"};
2. 使用SimpleCursorAdapter
这是Sample里面List3例子,通过读取通讯录android.provider.Contacts.Phones的资料,显示出来。
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Get a cursor with all phones
Cursor c = getContentResolver().query(Phones.CONTENT_URI, null, null, null, null);
startManagingCursor(c);
// Map Cursor columns to views defined in simple_list_item_2.xml
ListAdapter adapter = new SimpleCursorAdapter(this,
android.R.layout.simple_list_item_2, c,
new String[] { Phones.NAME, Phones.NUMBER },
new int[] { android.R.id.text1, android.R.id.text2 });
setListAdapter(adapter);
}
3. ListItem为单选按钮
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//参考ArrayAdapter的构造函数
setListAdapter(new ArrayAdapter
android.R.layout.simple_list_item_single_choice,
mStrings));
更多精彩
赞助商链接