这次的任务是使用ListView和Adapter完成列表信息显示,界面如图所示。
xml代码:
<TextView
android:id="@+id/age"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20sp"/>
<TextView
android:id="@+id/mail"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20sp" />
<TextView
android:id="@+id/address"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20sp" />
</LinearLayout>
其效果图如下:
JAVA代码:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView=(ListView) findViewById(R.id.list);
adapter = new SimpleAdapter(this, getData(),
R.layout.list_item,
new String[]{"name", "age", "mail","address"},
new int[]{ R.id.name,R.id.age,R.id.mail,R.id.address});
listView=(ListView) findViewById(R.id.list);
listView.setAdapter(adapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
String data = (String) adapterView.getItemAtPosition(position);
}
});
}
private List<HashMap<String, Object>> getData() {
List<HashMap<String, Object>> datas = new ArrayList<>();
HashMap<String, Object> data = new HashMap<>();
data.put("name", "姓名:蔡志坤");
data.put("age","年龄:25");
data.put("mail", "邮箱:ffczk86@gmail.com");
data.put("address","地址:厦门");
datas.add(data);
data = new HashMap<>();
data.put("name", "姓名:李华杰");
data.put("age","年龄:25");
data.put("mail", "邮箱:aa@bb.com");
data.put("address","地址:漳州");
datas.add(data);
data = new HashMap<>();
data.put("name", "姓名:张亮");
data.put("age","年龄:25");
data.put("mail", "邮箱:cc@gmail.com");
data.put("address","地址:厦门");
datas.add(data);
data = new HashMap<>();
data.put("name", "姓名:陈旭");
data.put("age","年龄:25");
data.put("mail", "邮箱:ccadd@gmail.com");
data.put("address","地址:厦门");
datas.add(data);
data = new HashMap<>();
data.put("name", "姓名:刘玄德");
data.put("age","年龄:25");
data.put("mail", "邮箱:ffczk@gmail.com");
data.put("address","地址:福州");
datas.add(data);
return datas;
也就差不多完成了。