列表视图(listView)是一个常用的组件,其数据内容以列表形式直观地展示出来,可使用两种方法向屏幕中添加列表视图,一种是直接使用ListView组件创建,另一种是将用Activity继承ListActivity实现。在此只介绍使用ListView组件实现列表的方法。使用ListView组件实现列表的方法也有两种
一、首先是借助字符串数组资源的方法。
activity_main.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<ListView
android:id="@+id/listView1"
android:entries="@array/ctype"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</ListView>
</LinearLayout>
在values下建立新.xml文件
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="ctype">
<item >新世纪影城</item>
<item >大地影城</item>
<item >乐天影城</item>
</string-array>
</resources>
.java:
package com.example.listview;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ListView lv=(ListView)findViewById(R.id.listView1);
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(MainActivity.this, "选择了"+ String.valueOf(position)+"项",Toast.LENGTH_SHORT).show();
}
});
}
}
二、借助适配器实现ListView
1、ArrayAapter:简单、易用。通常用于将数组或List集合的多个值包装成多个列表项。
2、SimpleAdapter:可用于将List集合的多个对象包装成多个列表项。
3、BaseAdapter:扩展BaseAdapter可以对各列表项进行最大限度的定制。
一、ArrayAapter
activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="选择一个电影院:"
android:textSize="30sp"/>
<ListView
android:id="@+id/ListView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"></ListView>
</LinearLayout>
.java:
package com.example.listview;
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.Toast;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ListView listView=(ListView)findViewById(R.id.ListView1);
final String str[]={"乐天电影院","威高电影院","泡泡电影院"};
//配置ArrayAdapter适配器
ArrayAdapter<String> adapter=new ArrayAdapter<String>
(this,android.R.layout.simple_list_item_single_choice,str);
listView.setAdapter(adapter);
//设置选中选项监听
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(MainActivity.this, "选择了"+str[position],Toast.LENGTH_SHORT).show();
}
});
}
}
二、SimpleAdapter
activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ListView
android:id="@+id/ListView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</ListView>
</LinearLayout>
listview.xml:
<?xml version="1.0" encoding="utf-8"?>
<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/ImageView1"
android:layout_width="80sp"
android:layout_height="80sp" />
<TextView
android:id="@+id/TextView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="30sp"/>
</LinearLayout>
.java:
package com.example.listview;
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.Toast;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class MainActivity extends AppCompatActivity {
private int[] imageID={R.drawable.apple,R.drawable.banana,R.drawable.blueberry,R.drawable.lemon,
R.drawable.pear,R.drawable.pineapple,R.drawable.watermelon};
private String[] textID={"苹果","香蕉","蓝莓","柠檬","香梨","菠萝","西瓜"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//创建一个List集合,List集合的元素是map
List<Map<String,Object>>list=new ArrayList<Map<String,Object>>();
{
//利用循环将键和值一一对应放入map集合内。
for(int i = 0; i<imageID.length; i++){
//下面一句放在循环外和里面不一样
Map<String,Object> map=new HashMap<String,Object>();
map.put("image",imageID[i]);
map.put("text",textID[i]);
//将map值存进list
list.add(map);
}
// map.put("image",R.drawable.apple);
// map.put("text","苹果");
// map.put("image",R.drawable.banana);
// map.put("text","香蕉");
// map.put("image",R.drawable.blueberry);
// map.put("text","蓝莓");
}
SimpleAdapter adapter=new SimpleAdapter
(this,list,R.layout.listview,//list 对应建立List的对象,R.layout.listview调用布局名字
new String[]{"image","text"},
new int[]{R.id.ImageView1,R.id.TextView1});
ListView listView=(ListView)findViewById(R.id.ListView1);
//为ListView设置Adapter
listView.setAdapter(adapter);
//设置点击监听
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(MainActivity.this, "选择了"+textID[position],Toast.LENGTH_SHORT).show();
}
});
}
}
三、自定义Adapter
实现步骤:
1.继承BaseAdapter
2.实现getView方法
3.关联ListView
Activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView
android:id="@+id/ListView1"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
layout下面新建xml文件:
listview.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageView
android:id="@+id/iv_image"
android:src="@mipmap/ic_launcher"
android:layout_width="60dp"
android:layout_height="60dp"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="@+id/tv_title"
android:layout_width="match_parent"
android:layout_height="30dp"
android:text="Title"
android:gravity="center"
android:textSize="25sp"/>
<TextView
android:id="@+id/tv_context"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Content"
android:textSize="20sp"/>
</LinearLayout>
</LinearLayout>
布局文件到此完工
下面就是最重要的部分
mainActivity.java:
package com.example.listview;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ListView;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class MainActivity extends AppCompatActivity {
private int[] imageID={R.drawable.apple,R.drawable.banana,R.drawable.blueberry,R.drawable.lemon,
R.drawable.pear,R.drawable.pineapple,R.drawable.watermelon};
private String[] textID={"苹果","香蕉","蓝莓","柠檬","香梨","菠萝","西瓜"};
private String[] contextID={"苹果好吃","香蕉好吃","蓝莓好吃","柠檬好吃","香梨好吃","菠萝好吃","西瓜好吃"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ListView listview=(ListView)findViewById(R.id.ListView1);
//创建一个List集合,List集合的元素是map
List<Map<String,Object>> list=new ArrayList<Map<String,Object>>();
{
//利用循环将键和值一一对应放入map集合内。
for (int i = 0; i < imageID.length; i++) {
//下面一句放在循环外和里面不一样
Map<String, Object> map = new HashMap<String, Object>();
map.put("image", imageID[i]);
map.put("text", textID[i]);
map.put("context", contextID[i]);
//将map值存进list
list.add(map);
}
}
// //数据源
// List<Map<String,Object>> list=new ArrayList<Map<String,Object>>();
// Map<String, Object> map = new HashMap<String, Object>();
// map=new HashMap<String,Object>();
// map.put("image",R.drawable.apple);
// map.put("text","pingg ");
// map.put("context","苹果");
// list.add(map);
//准备适配器
MyAdapter adapter=new MyAdapter(this);
adapter.setList(list);
//将适配器与ListView关联
listview.setAdapter(adapter);
}
}
里面有两种存入数据的方法
然后新建MyAdapter.java
代码:
package com.example.listview;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import java.util.List;
import java.util.Map;
public class MyAdapter extends BaseAdapter {
//数据集合list
List<Map<String,Object>> list;
//添加反射器
LayoutInflater inflater;
//构造器 上下文
public MyAdapter(Context context){
inflater=LayoutInflater.from(context);
}
//传入数据集合
public void setList( List<Map<String,Object>> list){
this.list=list;
}
@Override
public int getCount() {
return list.size();//lisview的长度,如果是null啥也不显示
}
@Override
public Object getItem(int position) {//没什么用
return list.get(position);
}
@Override
public long getItemId(int position) {//没什么用
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
//反射listview布局,后面null先默认如此,没啥用
View view=inflater.inflate(R.layout.listview,null);
//获取控件位置
ImageView logo=(ImageView)view.findViewById(R.id.iv_image);
TextView title=(TextView)view.findViewById(R.id.tv_title);
TextView context=(TextView)view.findViewById(R.id.tv_context);
//填充信息
Map map=list.get(position);
logo.setImageResource((Integer) map.get("image"));
title.setText((String) map.get("text"));
context.setText((String) map.get("context"));
//将含有信息的view返回到ListView
return view;
}
}
效果图为: