ListView是类似于将一个界面分为一行一行,如下图:
ListView只是一个视图,而如果要在这个列表视图中显示数据,则必须要通过“Adapter”,我们通常有三种Adapter:
(1)ArrayAdapter:每行只能是一个文本;
(2)SimpleAdapter:此种方式不是很好,经过测试,只能显示一行数据;
(3)继承BaseAdapter:此种方式较灵活,可以自定义将每行对应到不同的布局xml中;
(4)SimpleCursorAdapter:此处传入的数据不是List,而是数据库查询返回的Cursor;
这个实例分别实现了三种ListView的使用;
工程的层次结构如下:
ListView的界面如图所示:
ArrayAdapter的显示界面如下:
SimpleAdapter的界面如下:
ExtendBaseAdapter的界面如下:
1.主界面模块:
ListViewActivity.java
package org.xiazdong; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.ArrayAdapter; import android.widget.Button; public class ListViewActivity extends Activity implements OnClickListener{ private Button btn1; private Button btn2; private Button btn3; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); //一定要先setContentView,才findViewById btn1 = (Button) this.findViewById(R.id.arrayAdapter); btn2 = (Button) this.findViewById(R.id.simpleAdapter); btn3 = (Button)this.findViewById(R.id.baseAdapter); btn1.setOnClickListener(this); btn2.setOnClickListener(this); btn3.setOnClickListener(this); } @Override public void onClick(View v) { if(v==btn1){ Intent intent = new Intent(ListViewActivity.this,ArrayAdapterActivity.class); this.startActivity(intent); } else if(v==btn2){ Intent intent = new Intent(ListViewActivity.this,SimpleAdapterActivity.class); this.startActivity(intent); } else if(v==btn3){ Intent intent = new Intent(ListViewActivity.this,ExtendBaseAdapterActivity.class); this.startActivity(intent); } } }
main.xml
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <TextView android:id="@+id/tv" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:text="ListView的使用" android:textSize="50px" /> <Button android:id="@+id/arrayAdapter" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_below="@id/tv" android:text="ArrayAdapter" > </Button> <Button android:id="@+id/simpleAdapter" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_below="@id/arrayAdapter" android:text="SimpleAdapter" > </Button> <Button android:id="@+id/baseAdapter" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_below="@id/simpleAdapter" android:text="ExtendBaseAdapter" > </Button> <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:layout_alignParentRight="true" android:layout_marginBottom="142dp" android:layout_marginRight="16dp" android:text="by xiazdong" android:textSize="20px" /> </RelativeLayout>
2.ArrayAdapter模块
ArrayAdapterActivity.java
package org.xiazdong; import java.util.ArrayList; import java.util.List; import android.app.Activity; import android.os.Bundle; import android.widget.ArrayAdapter; import android.widget.ListView; /* * 每创建一个Activity就要在AndroidManifest.xml中声明 * ArrayAdapter只能显示基本的字符串 * SimpleAdapter能够显示自定义视图 */ public class ArrayAdapterActivity extends Activity { private ListView lv; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); lv = new ListView(this); //ListView继承View List<String> list = new ArrayList<String>(); for (int i = 0; i < 10; i++) { list.add("xiazdong-" + i); } ArrayAdapter<String> sa = new ArrayAdapter<String>(this, //将List赋给ArrayAdapter,采用simple_item_1样式 android.R.layout.simple_list_item_1, list); lv.setAdapter(sa); //将ArrayAdapter的值映射到ListView中 this.setContentView(lv); } }
3.SimpleAdapter模块
一般ListView都是用来显示列表的,一般列表的数据都是来自数据库的,因此我们这里假设前面已经实现了一个DBService类,里面存在pageQuery(int offset,int length);
比如dbservice.pageQuery(3,5);表示跳过3个记录,插入5条记录;
main.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <LinearLayout android:layout_width="wrap_content"//注意一定要wrap_content android:layout_height="wrap_content"//注意一定要wrap_content android:orientation="horizontal" > <TextView android:layout_width="100dp" android:layout_height="wrap_content" android:text="ID" /> <TextView android:layout_width="100dp" android:layout_height="wrap_content" android:text="NAME" /> <TextView android:layout_width="100dp" android:layout_height="wrap_content" android:text="AGE" /> </LinearLayout> <ListView android:id="@+id/listview" android:layout_width="wrap_content" android:layout_height="fill_parent" > </ListView> </LinearLayout>
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="horizontal" > <TextView android:id="@+id/id1" android:layout_width="100dp" android:layout_height="wrap_content" /> <TextView android:id="@+id/name" android:layout_width="100dp" android:layout_height="wrap_content" /> <TextView android:id="@+id/age" android:layout_width="100dp" android:layout_height="wrap_content" /> </LinearLayout>
MainActivity.java
package org.xiazdong.db; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import org.xiazdong.db.domain.Person; import org.xiazdong.db.service.DBService; import android.app.Activity; import android.os.Bundle; import android.util.Log; import android.widget.ListView; import android.widget.SimpleAdapter; public class MainActivity extends Activity { private ListView listView; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); listView = (ListView)this.findViewById(R.id.listview); DBService service = new DBService(this); List<Person> persons = service.pageQuery(0, 10); List<HashMap<String,Object>> data = new ArrayList<HashMap<String,Object>>(); for(Person person:persons){ HashMap<String,Object>map = new HashMap<String,Object>(); map.put("id", person.getId()); map.put("name", person.getName()); map.put("age", person.getAge()); data.add(map); } System.out.println(data); SimpleAdapter adapter = new SimpleAdapter(this, data, R.layout.item, new String[]{"id","name","age"}, new int[]{R.id.id1,R.id.name,R.id.age}); //data表示显示的数据,一个Map为一行,List<Map>表示多行 //R.layout.item表示一个item的布局 //new String[]{"id","name","age"}表示将key="id"的值映射到R.id.id1上 listView.setAdapter(adapter); } }
4.继承BaseAdapter类
ExtendBaseAdapterActivity.java
package org.xiazdong; import java.util.ArrayList; import java.util.List; import android.app.Activity; import android.os.Bundle; import android.widget.ArrayAdapter; import android.widget.ListView; public class ExtendBaseAdapterActivity extends Activity { private ListView lv; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); lv = new ListView(this); MyAdapter ma = new MyAdapter(this, new int[] { R.layout.sub1, R.layout.sub2 }); // 分别在每行中显示sub1的布局和sub2的布局 lv.setAdapter(ma); this.setContentView(lv); } }
MyAdapter.java
package org.xiazdong; import android.content.Context; import android.view.LayoutInflater; import android.view.View; import android.view.View.OnClickListener; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.Button; import android.widget.CheckBox; import android.widget.CompoundButton; import android.widget.CompoundButton.OnCheckedChangeListener; import android.widget.Toast; public class MyAdapter extends BaseAdapter { private Context context; private int layoutID[]; private LayoutInflater inflator; public MyAdapter(Context context, int layoutID[]) { this.context = context; this.layoutID = layoutID; inflator = LayoutInflater.from(context); } @Override public int getCount() { // 一个布局占一行 return layoutID.length; } @Override public Object getItem(int position) { return null; } @Override public long getItemId(int position) { return 0; } @Override // position表示要显示第几行,0表示第一行 // convertView表示要显示的视图 public View getView(int position, View convertView, ViewGroup parent) { convertView = inflator.inflate(layoutID[position], null);//每次将一个布局xml映射到一项中 if (convertView.findViewById(R.id.checkBox1) instanceof CheckBox) { CheckBox cb1 = (CheckBox) convertView.findViewById(R.id.checkBox1); cb1.setOnCheckedChangeListener(new OnCheckedChangeListener() { @Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { Toast t = Toast.makeText(context, "点击了复选框", Toast.LENGTH_SHORT); t.show(); } }); } if (convertView.findViewById(R.id.button1) instanceof Button) { Button b1 = (Button) convertView.findViewById(R.id.button1); b1.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { Toast t = Toast.makeText(context, "点击了按钮", Toast.LENGTH_SHORT); t.show(); } }); } return convertView; } }
sub1.xml
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" > <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignBaseline="@+id/checkBox1" android:layout_alignBottom="@+id/checkBox1" android:layout_alignParentLeft="true" android:text="点击弹出提示" /> <CheckBox android:id="@+id/checkBox1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:layout_alignParentTop="true" android:text="" /> </RelativeLayout>
sub2.xml
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" > <TextView android:id="@+id/textView2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignBaseline="@+id/button1" android:layout_alignBottom="@+id/button1" android:layout_alignParentLeft="true" android:text="TextView" /> <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:layout_alignParentTop="true" android:text="Button" /> </RelativeLayout>
主要步骤:
Cursor cursor = query();
SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, /*Cotnext*/
R.layout.item, /*一个item的布局*/
cursor,
new String[]{"name","age"}, /*name,age分别为数据库中表的属性*/
new int[]{R.id.name,R.id.age});/*R.id.name,R.id.age可以显示数据*/
listview.setAdapter(adapter);
总结:
ListView的应用有很多,比如
(1)在一般的游戏中列出排行榜;
(2)“设置”中列出多种子设置;
(3)字典列出单词;
(4)通讯录列表;