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,你可以根据你的具体需求选择合适的方法。

相关推荐
7177772 小时前
软件工程工业化实践:Gitee 智能化软件工厂全链路标准化交付体系解析
大数据·gitee·软件工程
71777720 小时前
数据与专家双驱动:Gitee 开源项目智能推荐体系与选型实践
gitee·开源
7177771 天前
CODING 进入退市周期后,如何对比 Gitee 与云效的 DevSecOps 与研发效能能力
gitee
qq_267612892 天前
Gitee项目管理推荐:从工具链割裂到DevOps全链路贯通的企业级协作实践
运维·gitee·devops
tju23332 天前
从文档存储到研发知识链路:Gitee Wiki 如何嵌入 DevSecOps 流程
gitee
qq_267612892 天前
从工具整合到AI协作:Gitee DevOps平台如何重塑企业研发全流程效能
人工智能·gitee·devops
沫璃染墨4 天前
《从零入门Linux系统篇(十四):系统工具篇·五——Git版本控制:从版本管理到协同开发》
linux·运维·服务器·git·gitee·github
XS0301064 天前
GitHub/Gitee 团队协作笔记
笔记·gitee·github
cakeism8255 天前
Gitee CodePecker 软件供应链恶意投毒监测与治理平台:从被动响应到主动防御的供应链安全体系
安全·gitee
90后小陈老师5 天前
Windows 10 下 Gitee SSH 连接配置详解(附常见问题解决)
windows·gitee·ssh