1. ArrayAdapter:简单的listView,只有TextVeiw时使用
2. SimpleAdapter:
ArrayAdapter:
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ListView mListView = (ListView) findViewById(R.id.listview);
String[] textArray = {"功能1","功能2","功能3","功能4","功能5","功能6","功能7","功能8"};
/*
* 定义数据适配器
* android.R.layout.simple_list_item_1 Listview的子条目显示的布局的id
* textArray 显示在ListView列表中的数据
*/
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1,
textArray);
mListView.setAdapter(adapter);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
SimpleAdapter:
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ListView mListView = (ListView) findViewById(R.id.listview);
List<Map<String, Object>> data = new ArrayList<Map<String,Object>>();
Map<String, Object> map = new HashMap<String, Object>();
map.put("name", "张三1");
map.put("icon", R.drawable.f007);
data.add(map);
map = new HashMap<String, Object>();
map.put("name", "张三2");
map.put("icon", R.drawable.f007);
data.add(map);
map = new HashMap<String, Object>();
map.put("name", "张三3");
map.put("icon", R.drawable.f007);
data.add(map);
map = new HashMap<String, Object>();
map.put("name", "张三4");
map.put("icon", R.drawable.f007);
data.add(map);
map = new HashMap<String, Object>();
map.put("name", "张三5");
map.put("icon", R.drawable.f007);
data.add(map);
SimpleAdapter adapter = new SimpleAdapter(
this, // 上下文
data, // listView绑定的数据
R.layout.listview_item, // listview的子条目的布局的id,item中的内容,布局;
new String[]{"name", "icon"}, // data数据中的map集合里的key
new int[]{R.id.tv_name, R.id.iv_icon}); // resource 中的id
mListView.setAdapter(adapter);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}