简单ListView (item_layout.xml)
复杂ListView(item_layout2.xml)
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.example.administrator.demo.MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1">
<ListView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:divider="#F00"
android:dividerHeight="2sp"
android:id="@+id/list1"></ListView>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1">
<ListView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:divider="#0F0"
android:dividerHeight="2sp"
android:id="@+id/list2"></ListView>
</LinearLayout>
</LinearLayout>
MainActivity.java
package com.example.administrator.demo;
import android.app.Activity;
import android.content.Context;
import android.support.annotation.IdRes;
import android.support.annotation.LayoutRes;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
public class MainActivity extends AppCompatActivity{
//数据源
String week[] = {"星期一", "星期二", "星期三", "星期四", "星期五"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//简单ListView
//创建适配器 将week与activity_main关联
//p1:适配器所关联的ListView的上下文
//p2:ListView子项的布局文件
//p3:适配器关联的数据源
// ArrayAdapter myAdapter = new ArrayAdapter(MainActivity.this, R.layout.activity_main, week);
//shiyong 系统自带的子项布
ArrayAdapter myAdapter = new ArrayAdapter(MainActivity.this, android.R.layout.simple_list_item_multiple_choice, week);
//得到ListView对象
ListView listView = (ListView) findViewById(R.id.list1);
/*
//创建表头
TextView header = new TextView(MainActivity.this);
header.setText("表头");
listView.addHeaderView(header);
TextView footer = new TextView(MainActivity.this);
footer.setText("表尾");
listView.addFooterView(footer);*/
//适配器与数据源相关联
listView.setAdapter(myAdapter);
//设置选择模式(多选)
listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
//设置事件监听器
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
//p1:当前数据适配器对象
//p2:view:定义的item_layout或者系统自带的 子项布局
//p3:指向在LIstView中的当前位置
//p4:id:指向在适配器中的位置
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(MainActivity.this, "position:"+position+"id:"+id, Toast.LENGTH_LONG ).show();
}
});//监听器介绍
//复杂ListView
//创建数据源 用数组列表表示
//接口 不能直接new--------------new子类
List<Map<String, Object>>data = new ArrayList<Map<String, Object>>();
Map map = new HashMap();//创建列表中的子项 用map表示
map.put("image", R.mipmap.ic_launcher);
map.put("name", "zhangsan");
map.put("tel", "1234");
//将map加入到数组列表中
data.add(map);
Map map1 = new HashMap();//创建列表中的子项 用map表示
map1.put("image", R.mipmap.ic_launcher);
map1.put("name", "lisi");
map1.put("tel", "100");
//将map加入到数组列表中
data.add(map1);
Map map2 = new HashMap();//创建列表中的子项 用map表示
map2.put("image", R.mipmap.ic_launcher);
map2.put("name", "wangwu");
map2.put("tel", "45");
//将map加入到数组列表中
data.add(map2);
//适配器
//p1:上下文对象
//p2:关联的数据源
//p3:resource:指向布局
//p4:from:数据源的关键字名称数组
//p5:to:显示数据源的各子项值得控件id
/* SimpleAdapter(Context context, List<? extends Map<String, ?>> data,
@LayoutRes int resource, String[] from, @IdRes int[] to)
*/
SimpleAdapter simpleAdapter = new SimpleAdapter(MainActivity.this, data, R.layout.item_layout2, new String[]{"image", "name", "tel"},new int[]{R.id.image,R.id.name, R.id.tel});
//关联
ListView listView2 = (ListView) findViewById(R.id.list2);
listView2.setAdapter(simpleAdapter);
//事件监听器
listView2.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//通过指向布局view获得子布局中的控件
TextView tName = (TextView) view.findViewById(R.id.name);
TextView tTel = (TextView) view.findViewById(R.id.tel);
Toast.makeText(MainActivity.this, "联系人:"+tName.getText().toString() + "\\" + "电话:"+tTel.getText().toString(), Toast.LENGTH_LONG).show();
}
});
}
}
item_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<TextView android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/weedday"
android:textSize="18sp"
xmlns:android="http://schemas.android.com/apk/res/android">
</TextView>
item_layout2.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal" android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/image"
android:layout_marginLeft="10dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/name"
android:paddingTop="12dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/tel"
android:paddingTop="12dp"/>
</LinearLayout>