在Android开发中,ListView是一个非常常用的控件,当然,现在已经有RecycleView来替代它了,但ListView还是有不少人在使用的。用到ListView,就一定会用到适配器。那什么是适配器呢?
适配器:是一个将数据(Data) 填充到 AdapterView(ListView就是一个典型的AdapterView)的中介,通过它能实现数据与AdapterView的分离设置,使AdapterView与数据的绑定更加简便,修改更加方便。
一、使用ArrayAdapter填充ListView
步骤:
(1)定义一个数组来存放ListView中item的内容(数据源);
(2)通过实现ArrayAdapter的构造方法创建一个ArrayAdapter对象;
(3)、通过ListView的setAdapter(...)方法绑定ArrayAdapter。
首先是activity_main.xml
<RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context=".MainActivity" > <ListView android:id="@+id/lv" android:layout_width="match_parent" android:layout_height="wrap_content" > </ListView> </RelativeLayout> 然后是listview_item.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:id="@+id/tv" android:layout_width="match_parent" android:textSize="18sp" android:layout_height="wrap_content"/> </LinearLayout> 最后是MainActivity.java文件 import android.os.Bundle; import android.app.Activity; import android.view.Menu; import android.view.View; import android.widget.AdapterView; import android.widget.AdapterView.OnItemClickListener; import android.widget.ArrayAdapter; import android.widget.ListView; import android.widget.Toast; public class MainActivity extends Activity { private ListView lv; String[] names={"张三","李四","王五","赵六","田七"}; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); lv=(ListView) findViewById(R.id.lv); //1数组 //String[] names={"张三","李四","王五","赵六","田七"}; //2创建适配器 //ArrayAdapter<String> adapter=new ArrayAdapter<String>(this, R.layout.listview_item, R.id.tv, names); ArrayAdapter<String> adapter=new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,names); //3填充 lv.setAdapter(adapter); //4.ListView的监听事件 lv.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { // TODO Auto-generated method stub String item=(String) parent.getItemAtPosition(position); //String item1= names[position]; Toast.makeText(MainActivity.this, "选择是"+item, 0).show(); } }); } }
效果图如下:
二、使用SimpleAdapter实现文本且带图片ListView
首先是activity_main.xml
<RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context=".MainActivity" > <ListView android:id="@+id/lv" android:layout_width="match_parent" android:layout_height="match_parent"></ListView> </RelativeLayout> 然后是listview_item.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/img" android:layout_width="40dp" android:layout_height="40dp" /> <TextView android:id="@+id/name" android:layout_width="0dp" android:layout_height="40dp" android:layout_weight="1" android:gravity="center_vertical" android:text="名字" android:textSize="18sp" /> </LinearLayout> 最后是MainActivity.java文件 import java.util.ArrayList; import java.util.HashMap; import java.util.List; import android.os.Bundle; import android.app.Activity; import android.view.Menu; import android.view.View; import android.widget.AdapterView; import android.widget.AdapterView.OnItemClickListener; import android.widget.BaseAdapter; import android.widget.ListView; import android.widget.SimpleAdapter; import android.widget.Toast; public class MainActivity extends Activity { private ListView lv; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); lv = (ListView) findViewById(R.id.lv); // 1创建数据 List<HashMap<String, Object>> list = new ArrayList<HashMap<String, Object>>(); HashMap<String, Object> map1 = new HashMap<String, Object>(); map1.put("name", "张三"); map1.put("image", R.drawable.ic_launcher); HashMap<String, Object> map2 = new HashMap<String, Object>(); map2.put("name", "李四"); map2.put("image", R.drawable.sg); HashMap<String, Object> map3 = new HashMap<String, Object>(); map3.put("name", "王五"); map3.put("image", R.drawable.mr); list.add(map1); list.add(map2); list.add(map3); // 2创建适配器 SimpleAdapter adapter = new SimpleAdapter(this, list, R.layout.listview_item, new String[] { "name", "image" }, new int[] { R.id.name, R.id.img }); // 3 填充 lv.setAdapter(adapter); // 4添加监听 lv.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { // TODO Auto-generated method stub HashMap<String, Object> map = (HashMap<String, Object>) parent .getItemAtPosition(position); Toast.makeText(MainActivity.this, "选择是:"+map.get("name"), 0).show(); } }); } }
效果图如下:
三、自定义适配器
由于ArrayAdapter和SimpleAdapter是Android中已经提供的适配器,在使用上有一定的局限性,要实现比较复杂、灵活的控制,要使用自定义适配器
自定义适配器步骤
1、编写一个类,继承BaseAdapter
2、实现4个抽象方法
–getCount(); –getItem(intposition); –getItemId(intposition); –getVew(); 首先是activity_main.xml <RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context=".MainActivity" > <ListView android:id="@+id/lv" android:layout_width="match_parent" android:layout_height="match_parent"></ListView> </RelativeLayout> 然后是listview_item.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/img" android:layout_width="40dp" android:layout_height="40dp" /> <TextView android:id="@+id/name" android:layout_width="0dp" android:layout_height="40dp" android:layout_weight="1" android:textSize="18sp" android:gravity="center_vertical" android:text="姓名" /> <CheckBox android:id="@+id/cb" android:layout_width="wrap_content" android:layout_height="40dp" android:focusable="false" android:clickable="false"/> </LinearLayout> 其次是MyAdapter.java文件 import java.util.HashMap; import java.util.List; import android.content.Context; import android.graphics.Color; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.ImageView; import android.widget.TextView; public class MyAdapter extends BaseAdapter { // 数据 List<HashMap<String, Object>> data; Context context; public MyAdapter(Context context,List<HashMap<String, Object>> data) { // TODO Auto-generated constructor stub this.context=context; this.data=data; } // 1 返回数据的个数 @Override public int getCount() { // TODO Auto-generated method stub return data.size(); } // 2获取每一项内容 @Override public Object getItem(int position) { // TODO Auto-generated method stub return data.get(position); } // 3返回数据的id(位置作为id) @Override public long getItemId(int position) { // TODO Auto-generated method stub return position; } /** *4 * 返回值View 项目布局 * position位置 * convertView view * parent 要填充的控件 */ @Override public View getView(int position, View convertView, ViewGroup parent) { // TODO Auto-generated method stub //1 由布局文件生成View对象 //1.1 // LayoutInflater inflater=LayoutInflater.from(context); // inflater.inflate() //1.2 View view=null; TextView textView=null; ImageView iv; if(convertView!=null){//使用conertView来节省对象的创建,从而节省内存空间 view=convertView; }else{ view=View.inflate(context, R.layout.listview_item, null);//父容器不要设置,有系统帮用户填充到ListView } textView=(TextView) view.findViewById(R.id.name);//在布局文件中查找子控件 iv=(ImageView) view.findViewById(R.id.img); //获取图片 Integer image=(Integer)(data.get(position).get("image")); iv.setImageResource(image.intValue()); //设置文本框 String name=(String)(data.get(position).get("name")); textView.setText(name); textView.setTextColor(Color.GRAY); return view; } } 最后是MainActivity.java文件 import java.util.ArrayList; import java.util.HashMap; import java.util.List; import android.os.Bundle; import android.app.Activity; import android.view.Menu; import android.view.View; import android.widget.AdapterView; import android.widget.AdapterView.OnItemClickListener; import android.widget.CheckBox; import android.widget.ListView; import android.widget.SimpleAdapter; public class MainActivity extends Activity { private ListView lv; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); lv=(ListView) this.findViewById(R.id.lv); List<HashMap<String, Object>> names=new ArrayList<HashMap<String, Object>>(); HashMap<String, Object> map1=new HashMap<String, Object>(); map1.put("name", "张三"); map1.put("image",R.drawable.aa); HashMap<String, Object> map2=new HashMap<String, Object>(); map2.put("name", "张三疯"); map2.put("image",R.drawable.bb); HashMap<String, Object> map3=new HashMap<String, Object>(); map3.put("name", "张三风"); map3.put("image",R.drawable.cc4); HashMap<String, Object> map4=new HashMap<String, Object>(); map4.put("name", "张三丰"); map4.put("image",R.drawable.ic_launcher); names.add(map1); names.add(map2); names.add(map3); names.add(map4); // MyAdapter adapter=new MyAdapter(this, names); lv.setAdapter(adapter); //添加监听 lv.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { // TODO Auto-generated method stub CheckBox cb=(CheckBox) view.findViewById(R.id.cb); // boolean b=cb.isChecked(); // if(b==true){ // cb.setChecked(false); // }else{ // cb.setChecked(true); // } cb.setChecked(!cb.isChecked()); } }); } }
效果如下:
这样我就简单的说完了ListView和适配器的简单用法,下一节我将会说一下listview的优化。
demo下载地址:http://download.csdn.net/detail/u014727709/9723650
欢迎start,欢迎评论,欢迎指正