(一)使用ArrayAdapter创建ListView
当我们ListView的列表项只显示文本的时候,我们就可不必再去自定义适配器而是使用ArrayAdapter来作为我们适配器,即方便代码又简洁。
代码如下:
public class SecondActivity extends Activity {
private ListView listView;
private String[] str = {"小杰", "奇犽", "路飞", "索隆"};
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.list_layout);
listView = (ListView) findViewById(R.id.listview);
ArrayAdapter arrayAdapter = new ArrayAdapter(this,android.R.layout.simple_expandable_list_item_1, str);
listView.setAdapter(arrayAdapter);
}
}
list_layout.xml代码:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" tools:context="com.example.simpleadapterdemo.FourActivity">
<ListView
android:id="@+id/listview"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</ListView>
</LinearLayout>
运行结果:
因为列表只显示了文字,所以直接用Android提供的android.R.layout.simple_expandable_list_item_1作为了列表项的组件。
创建一个ArrayAdapter的时候必须指定三个参数:
- Context:这个参数不需要多说。
- resource:一个包含TextView的布局的ID
- 数组或者List:为适配器添加数据源,也就是列表现实的内容。
(二)使用ListActivity创建ListView
当我们的窗体当中只存在一个ListView列表没有其他控件时,就可以让Activity直接继承ListActivity来实现,此时ListActivity的子类不需要调用setContentView()方法来加载布局文件,只需要通过setListAdapter()方法传入一个Adapter即可实现ListView。
代码如下:
public class ThirdActivity extends ListActivity {
private String[] str = {"天明", "少羽", "鸣人", "佐助"};
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ArrayAdapter arrayAdapter = new ArrayAdapter(this,android.R.layout.simple_expandable_list_item_1, str);
setListAdapter(arrayAdapter);
}
}
运行结果:
因为列表同样只显示了文字,所以直接用Android提供的android.R.layout.simple_expandable_list_item_1作为了列表项的组件。
(三)使用SimpleAdapter创建ListView
通过ArrayAdapter实现实现Adapter虽然简单,但是ArrayAdapter的功能比较有限,它的列表项只能是TextVIew,而SimpleAdapter功能比较强大,适用于ListView的大部分场景。如果我们想实现稍稍复杂点的列表项,比如图片加文字描述的那种,我们就可以使用SimpleAdapter来实现我们的ListView。
代码如下:
public class FourActivity extends AppCompatActivity {
private ListView listView;
private String[] names = new String[]{"小明", "小白", "小黑", "小李"};
private String[] descs = new String[]{"中二的小少年", "中二的小白脸", "文艺的小黑脸", "文艺好青年小李"};
private int[] images = new int[]{R.mipmap.image1, R.mipmap.image2, R.mipmap.image3, R.mipmap.image4};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.list_layout);
listView = (ListView) findViewById(R.id.listview);
List<Map<String, Object>> list = new ArrayList<>();
for (int i = 0; i < names.length; i++){
Map<String, Object> map = new HashMap<>();
map.put("header", images[i]);
map.put("personName", names[i]);
map.put("dess", descs[i]);
list.add(map);
}
SimpleAdapter adapter = new SimpleAdapter(this, list, R.layout.item, new String[]{"header", "personName", "dess"}, new int[]{R.id.head, R.id.name, R.id.desc});
listView.setAdapter(adapter);
}
}
新建了item.xml来作为我们的列表项。item.xml代码如下:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<ImageView
android:id="@+id/head"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="10dp"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="@+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20sp"
android:textColor="#f0f"
android:paddingLeft="10dp"/>
<TextView
android:id="@+id/desc"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="14sp"
android:paddingLeft="10dp"/>
</LinearLayout>
</LinearLayout>
运行结果:
因为窗体里面依旧只有一个ListView,所以为了方便我们FourActivity加载的布局依旧是list_layout.xml。SimpleAdapter使用起来也比较简单,创建SimpleAdapter需要指定五个参数:
- context:不需要多说。
- data:该参数是一个集合对象,当中的每一个Map对象生成一个列表项。
- resource:该参数指定一个布局ID。
- String[] from:该参数是一个String的数组,该参数将当做Map的key来去除对应的value来生成列表项。
- int[] to:该参数是一个int数组,传入组建的ID,来决定填充那些组件。
今天主要介绍两种适配器的使用方法,点击事件就不在讨论了。