ListView&Adapter
主界面布局
新建一个list_item,创建4个TextView
Java实现功能
public class MainActivity extends AppCompatActivity {
private ListView listView;
private SimpleAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView=(ListView) findViewById(R.id.list);
//创建adapter
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=(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<>();
// 给list增加一条数据
HashMap<String, Object> data = new HashMap<>();
// Map映射添加数据
data.put("name", "姓名:蔡志坤");
data.put("age","年龄:25");
data.put("mail", "邮箱:ffczk86@gmail.com");
data.put("address","地址:厦门");
// 将这个map放到list中
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;
}
}
实现图


浙公网安备 33010602011771号