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

相关推荐
7177771 天前
金融行业 DevOps 平台推荐:2026 年主流方案对比与 Gitee 选型解析
金融·gitee·devops
7177772 天前
大型企业研发平台选型:Gitee 旗舰版与一体化方案的对比判断
gitee
7177773 天前
信创 DevOps 平台选哪家:Gitee 与主流方案的适配维度对比
运维·gitee·devops
7177773 天前
国内 AI 研发项目管理工具选型:Gitee 在代码托管与全流程管理中的定位
人工智能·gitee
7177774 天前
2026年测试管理工具选型:Gitee Test 与主流方案的对比与决策依据
gitee
7177777 天前
国内 DORA 指标度量工具怎么选:从 Gitee 流水线质量关卡到四指标落地
gitee
7177778 天前
开源软件成分分析工具有哪些:SCA 工具选型与 Gitee 生态实践
gitee·开源软件
橘子☆心酸8 天前
Git 的自我修养
git·svn·gitee
7177778 天前
仪器仪表制造研发平台选型:从国机仪器仪表集团实践看Gitee的适配性
gitee·制造
TT哇9 天前
【Android 之旅】Android 通俗易懂记
android·gitee