ListView

在Android中,有三种简单的方法可以使用ListView。

使用ArrayAdapter

ArrayAdapter是最简单的适配器,它将字符串数组的内容显示在ListView中。

ListView listView = (ListView) findViewById(R.id.listView);

String\[\] values = new String\[\] { "Android", "iPhone", "WindowsMobile", "BlackBerry", "WebOS" };

ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, values);

listView.setAdapter(adapter);

使用SimpleAdapter

SimpleAdapter可以用于将更复杂的数据绑定到ListView。

ListView listView = (ListView) findViewById(R.id.listView);

List<Map<String, String>> data = new ArrayList<Map<String, String>>();

Map<String, String> item = new HashMap<String, String>();

item.put("title", "Google");

item.put("description", "Google search");

data.add(item);

SimpleAdapter adapter = new SimpleAdapter(this, data, android.R.layout.simple_list_item_2, new String\[\] {"title", "description"}, new int\[\] {android.R.id.text1, android.R.id.text2});

listView.setAdapter(adapter);

使用自定义Adapter

如果你需要更复杂的布局或行为,你可以创建自定义的Adapter。

public class MyAdapter extends ArrayAdapter<String> {

private Context context;

public MyAdapter(Context context, int resource, List<String> objects) {

super(context, resource, objects);

this.context = context;

}

@Override

public View getView(int position, View convertView, ViewGroup parent) {

LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

View rowView = inflater.inflate(android.R.layout.simple_list_item_1, parent, false);

TextView textView = (TextView) rowView.findViewById(android.R.id.text1);

textView.setText(getItem(position));

return rowView;

}

}

ListView listView = (ListView) findViewById(R.id.listView);

List<String> values = new ArrayList<String>();

values.add("Android");

values.add("iPhone");

values.add("WindowsMobile");

values.add("BlackBerry");

values.add("WebOS");

MyAdapter adapter = new MyAdapter(this, android.R.layout.simple_list_item_1, values);

listView.setAdapter(adapter);

以上三种方法都可以在Android中使用ListView,你可以根据你的具体需求选择合适的方法。

相关推荐
7177773 天前
国产研发管理平台怎么选:从排行榜到落地适配的决策框架
gitee
dreams_dream7 天前
项目想要推送多个平台仓库的方法
gitee·github
71777710 天前
2026年CI/CD工具选哪家比较好:主流方案对比与Gitee选型解析
ci/cd·gitee
71777710 天前
大型企业研发平台选型:Gitee 的一体化方案与关键决策点
gitee
71777713 天前
中国 DevSecOps 领导者:Gitee 一体化平台的能力边界与选型逻辑
gitee
71777713 天前
2026 国产 Jira 替代方案排名:技术决策者选型指南与 Gitee 定位分析
gitee·jira
透明的玻璃杯13 天前
gitee使用方法
gitee
71777716 天前
2026年测试管理选哪家比较好:Gitee 与主流平台选型对比指南
gitee
71777717 天前
2026年制品管理选哪家比较好:企业级制品库选型维度与Gitee Repo能力对照
gitee
创世虚拟世界17 天前
把 3D 虚拟世界拉到本地:vibe coding 的手感,是从看见自己那一行改动开始的
javascript·数据库·人工智能·3d·gitee·虚拟现实